SpringMVC+Spring+hibernate整合及分页
1. 新建web project
2. 引入jar,
3. 创建包com.tgb.web.controller, 下面创建包(dao,entity,service, config,spring,hibernate)
config-> spring-core.xml,spring-servlet.xml,spring-hibernate.xml
1) config下 springmvc配置文件 spring-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 注解扫描包 --> <context:component-scan base-package="com.tgb.web.controller" /> <!-- 开启注解 --> <mvc:annotation-driven/> <!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <!-- ViewResolver 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="1048576000"/> <property name="maxInMemorySize" value="40960"/> </bean> </beans>
2) spring-hibernate.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/test_ssh"/> <property name="username" value="root"/> <property name="password" value="linda0213"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="configLocations"> <list> <value> classpath*:com/tgb/web/controller/hibernate/hibernate.cfg.xml </value> </list> </property> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager"></property> <!-- 配置事务管理器 --> <property name="transactionAttributes"> <props> <!-- 配置事务传播特性 --> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="get*">PROPAGATION_NEVER</prop> </props> </property> </bean> </beans>
3) spring配置文件:spring-core.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <import resource="classpath*:com/tgb/web/controller/spring/spring-import.xml"/> </beans>
spring-> spring-import.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="userDao" class="com.tgb.web.controller.dao.UserDAO"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userManagerBase" class="com.tgb.web.controller.service.UserManager"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userManager" parent="transactionBase"> <property name="target" ref="userManagerBase"></property> </bean> </beans>
hibernate->hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping class ="com.tgb.web.controller.entity.User" /> </session-factory> </hibernate-configuration>
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置Session --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4. 建实体类
entity->User.java
package com.tgb.web.controller.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="T_USER") public class User { @Id @GeneratedValue @Column(length=32) private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(length=32) private String userName; @Column(length=32) private String age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
5. 建DAO层
dao->IUserDao.java, UserDao.java
1) IUserDao.java接口文件
package com.tgb.web.controller.dao; import java.util.List; import com.tgb.web.controller.entity.User; public interface IUserDAO { public void addUser(User user); public List<User> getAllUser(); public boolean delUser(Long id); public User getUser(Long id); public boolean updateUser(User user); public List<User> getForPage(int offset, int length); public Long getAllRowCount(); }
2) UserDao.java 实体类
package com.tgb.web.controller.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import com.tgb.web.controller.entity.User; public class UserDAO implements IUserDAO { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public void addUser(User user) { sessionFactory.getCurrentSession().save(user); } @Override public List<User> getAllUser() { String hql = "FROM User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); return query.list(); } @Override public boolean delUser(Long id) { String hql = "DELETE User u WHERE u.id=?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setString(0, String.valueOf(id)); return (query.executeUpdate()>0); } @Override public User getUser(Long id) { String hql = "FROM User u WHERE u.id=?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setString(0, String.valueOf(id)); return (User)query.uniqueResult(); } @Override public boolean updateUser(User user) { String hql = "UPDATE User u SET u.userName=? ,u.age=? WHERE u.id=?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setString(0, String.valueOf(user.getUserName())); query.setString(1, String.valueOf(user.getAge())); query.setString(2, String.valueOf(user.getId())); return (query.executeUpdate()>0); } @Override public List<User> getForPage(int offset, int length) { List<User> userList=null; String hql = "FROM User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setFirstResult(offset); query.setMaxResults(length); userList=query.list(); return userList; } @Override public Long getAllRowCount() { String hql = "SELECT COUNT(*) FROM User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); return Long.parseLong(query.uniqueResult().toString()); } }
6. 建service层
service->IUserManager.java, UserManager.java
1) IUserManager.java接口
package com.tgb.web.controller.service; import java.util.List; import com.tgb.web.controller.entity.Page; import com.tgb.web.controller.entity.User; public interface IUserManager { public void addUser(User user); public List<User> getAllUser(); public boolean delUser(Long id); public User getUser(Long id); public boolean updateUser(User user); public Page getForPage(int offset, int length); public Long getAllRowCount(); }
2) UserManager.java实现类
package com.tgb.web.controller.service; import java.util.List; import com.tgb.web.controller.dao.IUserDAO; import com.tgb.web.controller.entity.Page; import com.tgb.web.controller.entity.User; public class UserManager implements IUserManager{ private IUserDAO userDao; public IUserDAO getUserDao() { return userDao; } public void setUserDao(IUserDAO userDao) { this.userDao = userDao; } @Override public void addUser(User user) { userDao.addUser(user); } @Override public List<User> getAllUser() { return userDao.getAllUser(); } @Override public boolean delUser(Long id) { return userDao.delUser(id); } @Override public User getUser(Long id) { return userDao.getUser(id); } @Override public boolean updateUser(User user) { return userDao.updateUser(user); } @Override public Page getForPage(int currentPage, int pageSize) { Page page = new Page(); int allRow = userDao.getAllRowCount().intValue(); int offset = page.countOffset(currentPage, pageSize); List<User> list= userDao.getForPage(offset, pageSize); page.setPageNo(currentPage); page.setPageSize(pageSize); page.setTotalRecords(allRow); page.setList(list); return page; } @Override public Long getAllRowCount() { return userDao.getAllRowCount(); } }
7. 建controller
controller包下新建controller文件
UserController.java
package com.tgb.web.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.tgb.web.controller.entity.Page; import com.tgb.web.controller.entity.User; import com.tgb.web.controller.service.IUserManager; @Controller @RequestMapping("/user") public class UserController { @Resource(name="userManager") private IUserManager userManager; @RequestMapping("/addUser") public String addUser(User user){ userManager.addUser(user); return "redirect:/user/getAllUsers"; } @RequestMapping("/toAddUser") public String toAddUser(){ return "/addUser"; } @RequestMapping("/delUser") public void delUser(Long id,HttpServletResponse response){ String result = "{\"result\":\"error\"}"; if(userManager.delUser(id)){ result = "{\"result\":\"success\"}"; } PrintWriter out = null; response.setContentType("application/json"); try { out=response.getWriter(); out.write(result); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping("/getAllUser") public String getAllUser(HttpServletRequest request){ List<User> users = userManager.getAllUser(); request.setAttribute("users", users); return "/userManager"; } @RequestMapping("/getAllUsers") public String getAllUsers(HttpServletRequest request){ try { String pageNo = request.getParameter("pageNo"); if (pageNo == null) { pageNo = "1"; } Page page = userManager.getForPage(Integer.valueOf(pageNo), 10); request.setAttribute("page", page); List<User> pageusers = page.getList(); request.setAttribute("pageusers", pageusers); } catch (Exception e) { e.printStackTrace(); } return "/userManagerPage"; } @RequestMapping("/getUser") public String getUser(Long id,HttpServletRequest request){ User user = userManager.getUser(id); request.setAttribute("user", user); return "/editUser"; } @RequestMapping("/updateUser") public String updateUser(User user,HttpServletRequest request, HttpServletResponse response){ if(userManager.updateUser(user)){ return "redirect:/user/getAllUsers"; } else{ return "error"; } } }
8. 建显示层jsp文件
userManager.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script> <title>My JSP 'addUser.jsp' starting page</title> <script type="text/javascript"> function del(id){ $.get("/springMVCSSH2/user/delUser?id="+id,function(data){ alert(data.result); if("success"==data.result){ alert("删除成功"); window.location.reload(); } else{ alert("删除失败"); } }); } </script> </head> <body> <a href="/springMVCSSH2/user/toAddUser">添加用户</a> <table border="1"> <tbody> <tr> <th>姓名</th> <th>年龄</th> <th>编辑</th> </tr> <c:if test="${!empty users }"> <c:forEach items="${users }" var="u"> <tr> <td>${u.userName}</td> <td>${u.age}</td> <td> <a href="/springMVCSSH2/user/getUser?id=${u.id}">编辑</a> <a href="javascript:del('${u.id }')">删除</a> </td> </tr> </c:forEach> </c:if> </tbody> </table> </body> </html>
addUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script> <title>My JSP 'addUser.jsp' starting page</title> <script type="text/javascript"> function addUser(){ var form=document.forms[0]; form.action="/springMVCSSH2/user/addUser"; form.method="post"; form.submit(); } </script> </head> <body> <h> 添加用户 </h> <br> <form action=""> 姓名: <input type="text" name="userName"> 年龄: <input type="text" name="age"> <input type="button" value="添加" onClick="addUser()"> </form> </body> </html>
editUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script> <title>My JSP 'addUser.jsp' starting page</title> <script type="text/javascript"> function editUser(){ var form=document.forms[0]; form.action="/springMVCSSH2/user/updateUser"; form.method="post"; form.submit(); } </script> </head> <body> <h> 修改用户 </h> <br> <form name="userForm" action="/springMVCSSH2/user/updateUser" method="post"> <%-- <form action="">--%> <input type="hidden" name="id" value="${user.id }"> 姓名: <input type="text" name="userName" value="${user.userName }"> 年龄: <input type="text" name="age" value="${user.age }"> <input type="submit" value="编辑"> </form> </body> </html>
IE端访问:
http://localhost:8080/springMVCSSH2/user/getAllUser
分页的话, 在entity里新建Page.java
package com.tgb.web.controller.entity; import java.util.List; public class Page<E> { // 结果集 private List<E> list; // 查询记录总数 private int totalRecords; // 每页多少条记录 private int pageSize; // 第几页 private int pageNo; /** * @return 总页数 * */ public int getTotalPages(){ return (totalRecords+pageSize-1)/pageSize; } /** * 计算当前页开始记录 * @param pageSize 每页记录数 * @param currentPage 当前第几页 * @return 当前页开始记录号 */ public int countOffset(int currentPage,int pageSize){ int offset = pageSize*(currentPage-1); return offset; } /** * @return 首页 * */ public int getTopPageNo(){ return 1; } /** * @return 上一页 * */ public int getPreviousPageNo(){ if(pageNo<=1){ return 1; } return pageNo-1; } /** * @return 下一页 * */ public int getNextPageNo(){ if(pageNo>=getBottomPageNo()){ return getBottomPageNo(); } return pageNo+1; } /** * @return 尾页 * */ public int getBottomPageNo(){ return getTotalPages(); } public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } }
在UserDao.java里加入新方法
@Override public List<User> getForPage(int offset, int length) { List<User> userList=null; String hql = "FROM User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setFirstResult(offset); query.setMaxResults(length); userList=query.list(); return userList; } @Override public Long getAllRowCount() { String hql = "SELECT COUNT(*) FROM User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); return Long.parseLong(query.uniqueResult().toString()); }
UserManage.java里调用dao方法
@Override public Page getForPage(int currentPage, int pageSize) { Page page = new Page(); int allRow = userDao.getAllRowCount().intValue(); int offset = page.countOffset(currentPage, pageSize); List<User> list= userDao.getForPage(offset, pageSize); page.setPageNo(currentPage); page.setPageSize(pageSize); page.setTotalRecords(allRow); page.setList(list); return page; } @Override public Long getAllRowCount() { return userDao.getAllRowCount(); }
UserController里调用
@RequestMapping("/getAllUsers") public String getAllUsers(HttpServletRequest request){ try { String pageNo = request.getParameter("pageNo"); if (pageNo == null) { pageNo = "1"; } Page page = userManager.getForPage(Integer.valueOf(pageNo), 10); request.setAttribute("page", page); List<User> pageusers = page.getList(); request.setAttribute("pageusers", pageusers); } catch (Exception e) { e.printStackTrace(); } return "/userManagerPage"; }
新建显示层jsp文件 UserManagerPage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script> <title>My JSP 'addUser.jsp' starting page</title> <script type="text/javascript"> function del(id){ $.get("/springMVCSSH2/user/delUser?id="+id,function(data){ alert(data.result); if("success"==data.result){ alert("删除成功"); window.location.reload(); } else{ alert("删除失败"); } }); } </script> </head> <body> <a href="/springMVCSSH2/user/toAddUser">添加用户</a> <table border="1"> <tbody> <tr> <th>姓名</th> <th>年龄</th> <th>编辑</th> </tr> <c:if test="${!empty pageusers }"> <c:forEach items="${pageusers }" var="u"> <tr> <td>${u.userName}</td> <td>${u.age}</td> <td> <a href="/springMVCSSH2/user/getUser?id=${u.id}">编辑</a> <a href="javascript:del('${u.id }')">删除</a> </td> </tr> </c:forEach> </c:if> <tr> <td colspan="6" align="center">共${page.totalRecords}条记录 共${page.totalPages}页 当前第${page.pageNo}页<br> <a href="/springMVCSSH2/user/getAllUsers?pageNo=${page.topPageNo }"><input type="button" name="fristPage" value="首页" /></a> <c:choose> <c:when test="${page.pageNo!=1}"> <a href="/springMVCSSH2/user/getAllUsers?pageNo=${page.previousPageNo }"><input type="button" name="previousPage" value="上一页" /></a> </c:when> <c:otherwise> <input type="button" disabled="disabled" name="previousPage" value="上一页" /> </c:otherwise> </c:choose> <c:choose> <c:when test="${page.pageNo != page.totalPages}"> <a href="/springMVCSSH2/user/getAllUsers?pageNo=${page.nextPageNo }"><input type="button" name="nextPage" value="下一页" /></a> </c:when> <c:otherwise> <input type="button" disabled="disabled" name="nextPage" value="下一页" /> </c:otherwise> </c:choose> <a href="/springMVCSSH2/user/getAllUsers?pageNo=${page.bottomPageNo }"><input type="button" name="lastPage" value="尾页" /></a> </td> </tr> </tbody> </table> </body> </html>