作者:挚爱—fruit | 来源:互联网 | 2023-09-14 20:23
使用的工具:eclipse4.7.3mysqljar包实体类packagecom.ssh.entity;publicclassProduct{privatein
使用的工具:
- eclipse 4.7.3
- mysql
jar包
实体类
package com.ssh.entity;public class Product {private int id; //idprivate String productname; //商品名称private String Descs; //商品描述private Integer cid; //类别编号public int getId() {return id;}public void setId(int id) {this.id = id;}public String getProductname() {return productname;}public void setProductname(String productname) {this.productname = productname;}public String getDescs() {return Descs;}public void setDescs(String descs) {Descs = descs;}public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}}
hibernate.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
hibernate.cfg.xml
com.mysql.jdbc.Driver
123456
jdbc:mysql://127.0.0.1:3306/hibernate
root
org.hibernate.dialect.MySQLDialect
true
thread
Dao层
package com.ssh.dao;
import java.util.List;
import java.util.Map;
import com.ssh.entity.Product;public interface ProductDao {/*** 分页查询* @param map* @return*/public List findPage(Map map);}
DaoImpl
package com.ssh.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.ssh.entity.Product;public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {/*** 分页查询* @param map* @return*/@Overridepublic List findPage(final Map map) {//在这里就不用find(),用execute()实现//调用new HibernateCallback()接口实现里面的方法,是指定findPage的返回类型return this.getHibernateTemplate().execute(new HibernateCallback>() {@Overridepublic List doInHibernate(Session session) throws HibernateException, SQLException {Query query = session.createQuery("from Product");//pageNo是指第几页,pageSize是指当前显示数目//doInHibernate(Session session)里面是属性是不变的,因此在findPage()里面的参数前面设置finalquery.setFirstResult(Integer.parseInt(map.get("pageNo").toString()) );query.setMaxResults(Integer.parseInt(map.get("pageSize").toString()));return query.list();}});}}
Service层
package com.ssh.service;import java.util.List;
import java.util.Map;import com.ssh.entity.Product;public interface ProductService {/*** 分页查询* @param map* @return*/public List findPage(Map map);}
ServiceImpl
package com.ssh.service;
import java.util.List;
import java.util.Map;
import com.ssh.dao.ProductDao;
import com.ssh.entity.Product;public class ProductServiceImpl implements ProductService {private ProductDao ProductDao;public ProductDao getProductDao() {return ProductDao;}public void setProductDao(ProductDao productDao) {ProductDao = productDao;}/*** 分页查询* @param map* @return*/@Overridepublic List findPage(Map map) {return ProductDao.findPage(map);}}
Action层
package com.ssh.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ssh.service.ProductService;public class ProductAction {//get,setprivate List list;private Integer pageNo = 1;private ProductService productService;/*** 分页查询Product* @return*/public String showProduct() {int pageSize = 4;Map map=new HashMap();map.put("pageNo", (pageNo-1)*pageSize);map.put("pageSize", pageSize);list = productService.findPage(map);return "showProductSuccess";}public List getList() {return list;}public void setList(List
list) {this.list = list;}public Integer getPageNo() {return pageNo;}public void setPageNo(Integer pageNo) {this.pageNo = pageNo;}public ProductService getProductService() {return productService;}public void setProductService(ProductService productService) {this.productService = productService;}}
applicationContext.xml
struts.xml
/main.jsp
web.xml
sshindex.jspcontextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListenerstrutsorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterstruts*.actionopenSessionInVieworg.springframework.orm.hibernate3.support.OpenSessionInViewFilteropenSessionInView/*
main.jsp
<%@ page language="java" cOntentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
商品名称 | 商品描述 | 类别 | 操作 |
| | | 删除添加 |
当前产品数个上一页下一页 |
效果图