使用的工具:
- eclipse 4.7.3
- mysql
jar包
实体类
package com.ssh.entity;
public class Product {
private int id; //id
private 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
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-10-25 9:31:37 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.ssh.entity.Product" table="PRODUCT">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="productname" type="java.lang.String">
<column name="PRODUCTNAME" />
</property>
<property name="Descs" type="java.lang.String">
<column name="DESCS" />
</property>
<property name="cid" type="java.lang.Integer">
<column name="CID" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- 映射 -->
<mapping resource="com/ssh/entity/Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
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<Product> findPage(Map<String, Object> 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
*/
@Override
public List<Product> findPage(final Map<String, Object> map) {
//在这里就不用find(),用execute()实现
//调用new HibernateCallback<T>()接口实现里面的方法,<T>是指定findPage的返回类型
return this.getHibernateTemplate().execute(new HibernateCallback<List<Product>>() {
@Override
public List<Product> doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("from Product");
//pageNo是指第几页,pageSize是指当前显示数目
//doInHibernate(Session session)里面是属性是不变的,因此在findPage()里面的参数前面设置final
query.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<Product> findPage(Map<String, Object> 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
*/
@Override
public List<Product> findPage(Map<String, Object> 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,set
private List<Product> list;
private Integer pageNo = 1;
private ProductService productService;
/**
* 分页查询Product
* @return
*/
public String showProduct() {
int pageSize = 4;
Map< String, Object> map=new HashMap<String, Object>();
map.put("pageNo", (pageNo-1)*pageSize);
map.put("pageSize", pageSize);
list = productService.findPage(map);
return "showProductSuccess";
}
public List<Product> getList() {
return list;
}
public void setList(List<Product> 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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!--创建会话工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置Dao -->
<bean id="productDao" class="com.ssh.dao.ProductDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Service -->
<bean id="productService" class="com.ssh.service.ProductServiceImpl">
<property name="productDao" ref="productDao"></property>
</bean>
<!-- Action -->
<bean id="productAction" class="com.ssh.action.ProductAction" >
<property name="productService" ref="productService"></property>
</bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="index" namespace="/" extends="struts-default" >
<action name="*_Action" class="productAction" method="{1}" >
<result name="showProductSuccess">/main.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts2 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- openSessionInView -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>帖子列表</title>
<script type="text/javascript" src="script/jquery-1.8.3.js"></script>
<script type="text/javascript" src="script/jquery.metadata.js"></script>
<script type="text/javascript" src="script/jquery.validate.js"></script>
<script type="text/javascript">
$(function() {
$("tbody.list>tr:even").css("backgroundColor", "#EEEEEE");
});
</script>
<link rel="stylesheet" type="text/css" href="script/main.css">
</head>
<body>
<s:fielderror />
<s:actionerror/>
<div class="menubar"></div>
<table cellspacing="0">
<tbody class="list topicList">
<tr class="title">
<td width="50">商品名称</td>
<td width="60">商品描述</td>
<td width="60">类别</td>
<td width="120" >操作</td>
</tr>
<s:iterator value="list" var="product" >
<tr class="data">
<td><s:property value="#product.productname" /></td>
<td><s:property value="#product.Descs" /></td>
<td><s:property value="#product.cid" /></td>
<td>
<span><a href="${pageContext.request.contextPath }/deleteProduct_Action.action?id=${product.id}">删除</a></span>
<span><a href="${pageContext.request.contextPath }/add.jsp">添加</a></span>
</td>
</tr>
</s:iterator>
<tr>
<td colspan="5" class="num">当前产品数<font color="red"><s:property value="list.size" /></font>个<a href="${pageContext.request.contextPath }/showProduct_Action.action?pageNo=${pageNo-1 }">上一页</a>
<a href="${pageContext.request.contextPath }/showProduct_Action.action?pageNo=${pageNo+1 }">下一页</a></td>
</tr>
</tbody>
</table>
<div style="margin-bottom: 15px"></div>
<script type="text/javascript">
$("form.addNewTopicForm").validate();
</script>
</body>
</html>