SSH框架:struts2 + spring + hibernate
web层:struts2+jsp
service层:javaBean
dao层:hibernate
spring:管理Action对象 javaBean对象 sessionFactory session维护以及aop事务
项目结构:
创建步骤:
create new project->左侧选择maven->勾选create from archetype->下拉选择webapp->输入groupId、ArtifactId、version坐标->完成
编辑pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>mySSH</name> <groupId>com.david.mySSH</groupId> <artifactId>mySSH</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- 统一源码的编码方式 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 统一各个框架版本 --> <struts.version>2.5.10</struts.version> <spring.version>4.3.8.RELEASE</spring.version> <hibernate.version>5.1.7.Final</hibernate.version> </properties> <dependencies> <!-- Junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Spring 核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring web依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring整合ORM框架依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- Struts2 核心依赖 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <!-- Struts2和Spring整合依赖 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> <!-- Hibernate 核心依赖 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MySQL 依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> <!-- C3P0 依赖 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5</version> </dependency> <!-- AspectJ依赖 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <!-- SLF4J依赖 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> </dependencies> <build> <finalName>maven_ssh</finalName> <plugins> <!-- 统一源代码编译输出的JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 打包时跳过单元测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!-- 集成Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/${project.artifactId}</path> </configuration> </plugin> </plugins> </build> </project>
编辑web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 配置Struts2过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring配置文件所在路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
编辑struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 默认访问页面 --> <package name="default" extends="struts-default" namespace="/"> <default-action-ref name="default" /> <action name="default"> <result>/index.jsp</result> </action> </package> <!-- 请求转发 --> <!-- Struts2在2.5版本后添加strict-method-invocation(严格方法访问),默认为true,不能使用动态方法调用功能,故需设为false --> <package name="user" extends="struts-default" namespace="/" strict-method-invocation="false"> <action name="login" class="com.david.action.UserAction" method="login"> <result name="success" type="redirectAction"> <param name="actionName">product_list</param> <param name="namespace">/</param> </result> <result name="error">/error.jsp</result> </action> </package> <package name="product" extends="struts-default" namespace="/" strict-method-invocation="false"> <!--登陆拦截器 --> <interceptors> <interceptor name="LoginInterceptor" class="com.david.Interceptor.LoginInterceptor"></interceptor> <interceptor-stack name="loginStack"> <interceptor-ref name="LoginInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="loginStack"></default-interceptor-ref> <global-results> <result name="toLogin" type="redirect">/index.jsp</result> </global-results> <action name="product_*" class="productAction" method="{1}Product"> <result>/product/index.jsp</result> <result name="save" type="redirectAction"> <param name="actionName">product_list</param> <param name="namespace">/</param> </result> </action> </package> </struts>
编辑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:context="http://www.springframework.org/schema/context" 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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启包扫描,并注册注解 --> <context:component-scan base-package="com.david.*"/> <!-- 引入属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据库连接相关信息 --> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置Hibernate属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop><!-- 是否展示SQL --> <prop key="hibernate.hbm2ddl.auto">update</prop><!-- 是否自动创建表结构 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/david2018_db</prop> <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> </props> </property> <!-- 扫描并加载注解过的实体类 --> <property name="packagesToScan" value="com.david.model"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 注入SessionFactory --> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置事务增强 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 配置需要进行事务管理的方法,和事务传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <!-- 配置切入点 com.yjq.ssh.*.service.*+.*(..) * org.ssh.service.*+.*(..) *:表示方法的作用域,*表示所有 org.ssh.service.*:表示org.ssh.service下的任何包 org.ssh.service.*+:表示org.ssh.service下的任何包及其子包 *(..):*表示任何方法,(..)表示方法的任何参数 --> <aop:pointcut id="pointcut" expression="execution(* com.david.service.*+.*(..))"/> <!-- 适配切入点和事务增强 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
编辑jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/david2018_db?useSSL=true&characterEncoding=UTF-8 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=1234
完善项目结构main下新增java 设置为源码文件夹 分别新增 action dao Interceptor model service utils 文件夹 webapp下新增product文件夹addProduct.jsp index.jsp webapp下index.jsp登陆页面 error.jsp
从mysql数据库生成实体->database登陆mysql,右击mySSH项目add Framework Support 选择hibernate->以后每次生成在Persistence菜单中右击mySSH->Generate Persistence Mapping->By Database Schema 勾选要生成的表 完成。生成如下。
package com.david.model; import javax.persistence.*; import java.util.Objects; @Entity @Table(name = "User", schema = "david2018_db", catalog = "") public class UserEntity { private int userId; private String userName; private String passWord; @Id @Column(name = "UserId") public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } @Basic @Column(name = "UserName") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Basic @Column(name = "PassWord") public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserEntity that = (UserEntity) o; return userId == that.userId && Objects.equals(userName, that.userName) && Objects.equals(passWord, that.passWord); } @Override public int hashCode() { return Objects.hash(userId, userName, passWord); } }
package com.david.model; import javax.persistence.*; import java.util.Objects; @Entity @Table(name = "User", schema = "david2018_db", catalog = "") public class UserEntity { private int userId; private String userName; private String passWord; @Id @Column(name = "UserId") public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } @Basic @Column(name = "UserName") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Basic @Column(name = "PassWord") public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserEntity that = (UserEntity) o; return userId == that.userId && Objects.equals(userName, that.userName) && Objects.equals(passWord, that.passWord); } @Override public int hashCode() { return Objects.hash(userId, userName, passWord); } }
LoginInterceptor拦截器
package com.david.Interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import java.util.Map; public class LoginInterceptor extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation actionInvocation) throws Exception { Map<String, Object> session = ActionContext.getContext().getSession(); Object user = session.get("user"); //已登陆 放行 if(user != null){ return actionInvocation.invoke(); }else{ //没登陆 去登陆页面 return "toLogin"; } } }
pageBean
package com.david.utils; import java.util.List; public class pageBean<T> { //当前页 private int curPage = 1; //当前显示条数 private int pageSize = 5; //总页数 private int totalPage; //总条数 private int totalCount; //展示的数据 private List<T> Data; public pageBean(int curPage, int pageSize, int totalCount) { this.curPage = curPage; this.pageSize = pageSize; this.totalCount = totalCount; this.totalPage = (int) Math.ceil(1.0 * totalCount / pageSize); } public int getCurPage() { return curPage; } public void setCurPage(int curPage) { this.curPage = curPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getData() { return Data; } public void setData(List<T> data) { Data = data; } }
dao接口
package com.david.dao; import com.david.model.UserEntity; import java.util.List; public interface IUserDao { List<UserEntity> findByNameAndPass(UserEntity u); } package com.david.dao; import com.david.model.ProductEntity; import java.util.List; public interface IProductDao { void saveProduct(ProductEntity product); ProductEntity getById(int id); int totalCount(String name); List<ProductEntity> getProductList(String name, int page, int pageSize); }
dao实现类
package com.david.dao; import com.david.model.UserEntity; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate4.HibernateTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class UserDaoImpl implements IUserDao{ private HibernateTemplate hbt; @Autowired public UserDaoImpl(SessionFactory sessionFactory){ this.hbt = new HibernateTemplate(sessionFactory); } @Override public List<UserEntity> findByNameAndPass(UserEntity u) { return (List<UserEntity>)hbt.find("from UserEntity u where u.userName = ? and u.passWord = ?",u.getUserName(),u.getPassWord()); } }
package com.david.dao; import com.david.model.ProductEntity; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.orm.hibernate4.HibernateTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class ProductDaoImpl implements IProductDao { private HibernateTemplate hbt; @Autowired public ProductDaoImpl(SessionFactory sessionFactory){ this.hbt = new HibernateTemplate(sessionFactory); } @Override public void saveProduct(ProductEntity product) { hbt.save(product); } @Override public ProductEntity getById(int id) { ProductEntity pro = hbt.get(ProductEntity.class,id); return pro; } @Override public int totalCount(String name) { String hql = "select count(*) from ProductEntity p where p.name like ?"; List<Long> list = (List<Long>)hbt.find(hql,"%"+name+"%"); if(list != null && list.size()>0){ return list.get(0).intValue(); } return 0; } @Override public List<ProductEntity> getProductList(String name, int page, int pageSize) { int limit = (page - 1) * pageSize; String hql = "from ProductEntity p where p.name like ?"; // List<ProductEntity> list = (List<ProductEntity>) hbt.find(hql); List<ProductEntity> list = (List<ProductEntity>) hbt.execute(new HibernateCallback<List<ProductEntity>>() { @Override public List<ProductEntity> doInHibernate(Session session) throws HibernateException { Query query = session.createQuery(hql); query.setParameter(0,"%"+name+"%"); query.setFirstResult(limit); query.setMaxResults(pageSize); return query.list(); } }); return list; } }
service接口
package com.david.service; import com.david.model.ProductEntity; import com.david.utils.pageBean; public interface IProductService { void saveProduct(ProductEntity product); ProductEntity getById(int id); int totalCount(String name); pageBean getProductPage(String name, int page, int pageSize); }
package com.david.service; import com.david.model.UserEntity; public interface IUserService { boolean findByNameAndPass(UserEntity u); }
service实现类
package com.david.service; import com.david.dao.IUserDao; import com.david.model.UserEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements IUserService { @Autowired private IUserDao userDao; @Override public boolean findByNameAndPass(UserEntity u) { return userDao.findByNameAndPass(u).size()>0; } }
package com.david.service; import com.david.dao.IProductDao; import com.david.model.ProductEntity; import com.david.utils.pageBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ProductServiceImpl implements IProductService { @Autowired private IProductDao productDao; @Override public void saveProduct(ProductEntity product) { productDao.saveProduct(product); } @Override public ProductEntity getById(int id) { return productDao.getById(id); } @Override public int totalCount(String name) { return productDao.totalCount(name); } @Override public pageBean getProductPage(String name, int page, int pageSize) { int totalCount = totalCount(name); List<ProductEntity> list = productDao.getProductList(name,page,pageSize); pageBean<ProductEntity> pages = new pageBean<ProductEntity>(page,pageSize,totalCount); pages.setData(list); return pages; } }
action
package com.david.action; import com.david.model.UserEntity; import com.david.service.IUserService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; @Controller @Scope("prototype") public class UserAction extends ActionSupport { @Autowired private IUserService userService; private UserEntity user; public String login(){ if(userService.findByNameAndPass(user)){ ActionContext.getContext().getSession().put("user",user.getUserName()); return SUCCESS; } return ERROR; } public UserEntity getUser() { return user; } public void setUser(UserEntity user) { this.user = user; } }
package com.david.action; import com.david.model.ProductEntity; import com.david.service.IProductService; import com.david.utils.pageBean; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; @Controller @Scope("prototype") public class ProductAction extends ActionSupport { @Autowired private IProductService productService; private String name = ""; private Integer page = 1; private Integer pageSize = 5; public ProductEntity getProduct() { return product; } public void setProduct(ProductEntity product) { this.product = product; } private ProductEntity product = new ProductEntity(); public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public String saveProduct(){ productService.saveProduct(product); return "save"; } public String listProduct(){ pageBean<ProductEntity> pagebean = productService.getProductPage(name,page,pageSize); ActionContext.getContext().put("pageBean", pagebean); System.out.println(pagebean.getData()); return SUCCESS; } }
根目录index。jsp 登陆 和error登陆失败页
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login</title> </head> <body> <form action="/login" method="post"> 用户名:<input name="user.UserName"><br> 密码:<input type="password" name="user.PassWord"><br> <button type="submit">登陆</button> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 登陆失败 </body> </html>
product列表index.jsp和添加页
<%@ page import="com.david.utils.pageBean" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Title</title> <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> <script> $(function(){ $("#btnSearch").click(function(){ var name = $("#productName").val(); if(name == ""){ alert("请输入产品名称"); } location.href='/product_list?name='+name; }); }); </script> </head> <body> 欢迎您:<s:property value="#session.user"></s:property> <div> 按产品名称搜索:<input id="productName"> <button id="btnSearch">搜索</button> <a href="/product/addProduct.jsp">添加产品</a> <table> <th><td>id</td><td>产品名称</td><td>产品价格</td></th> <s:iterator value="#pageBean.Data" var="p"> <tr> <td><s:property value="#p.id"></s:property></td> <td><s:property value="#p.name"></s:property></td> <td><s:property value="#p.price"></s:property></td> </tr> </s:iterator> </table> <div class="page"> <% pageBean pageBean = (pageBean)request.getAttribute("pageBean"); %> <%if(pageBean.getCurPage() != 1){%> <a href="?page=1">首页</a> <a href="?page=<%=pageBean.getCurPage()-1%>">上一页</a> <%}%> <%for(int i = 1;i<=pageBean.getTotalPage();i++){ %> <%if(pageBean.getCurPage() == i){%> <a href="?page=<%=i%>" class="currentPage"><%=i%></a> <%}else{%> <a href="?page=<%=i%>"><%=i%></a> <%}%> <%}%> <%if(pageBean.getCurPage() != pageBean.getTotalPage()){%> <a href="?page=<%=pageBean.getCurPage()+1%>">下一页</a> <a href="?page=<%=pageBean.getTotalPage()%>">尾页</a> <%}%> 共<%=pageBean.getTotalCount()%>条数据,<%=pageBean.getTotalPage()%>页。 </div> <style> .page a{ font-size:12px; text-decoration: none; color:#ccc; } .page .currentPage{ color:#000; font-size:20px; } </style> </div> </body> </html>
<%-- Created by IntelliJ IDEA. User: baidawei Date: 2018/5/25 Time: 下午4:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加商品</title> </head> <body> <form action="product_save" method="post"> 商品名称:<input type="text" name="product.name"><br> 商品价格: <input type="text" name="product.price"><br> <button type="submit">添加</button> </form> </body> </html>