Spring、Struts2、Hibernate框架:
具体三大框架的知识以前的文章写过,在这里整合
Spring框架知识:http://www.cnblogs.com/xuyiqing/category/1164340.html
Struts2框架知识:http://www.cnblogs.com/xuyiqing/category/1164341.html
Hibernate框架知识:http://www.cnblogs.com/xuyiqing/category/1163473.html
整合原理:
Struts2是WEB层的框架,Hibernate框架在DAO层操作数据库,Spring框架管理所有的对象
Struts2将Action对象交给Spring管理、Hibernate将SessionFactory、事务管理交给Spring
其实三大框架整合本质是Struts2框架和Hibernate框架分别把对象交给Spring来维护
这里不能凭空整合,而是整合做一个登录案例:
第一步(关键):导包(缺一不可)
不只是三大框架的包,还有数据库驱动、连接池等包,具体的是这些:
都可以在网上下载到
导包完成!
配置Spring容器:
在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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SSH</display-name> <!-- 让spring随web启动而创建的监听器 --> <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> <!-- 扩大session作用范围 注意: 任何filter一定要在struts的filter之前调用 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <!-- struts2核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
创建配置文件并且导入约束(非必须、建议导入):
导入命名空间(这里不做详细介绍,百度):
如果不清楚,直接复制下边的配置文件即可
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <!-- 读取db.properties文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0连接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property> <property name="driverClass" value="${jdbc.driverClass}" ></property> <property name="user" value="${jdbc.user}" ></property> <property name="password" value="${jdbc.password}" ></property> </bean> <!-- 核心事务管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory" ></property> </bean> <!-- 整合AOP事务:这里采用注解方式,配置文件中这些代码是示例,不起作用 --> <!-- 配置通知 --> <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> --> <!-- 配置将通知织入目标对象 配置切点 配置切面 --> <!-- <aop:config> <aop:pointcut expression="execution(* service.impl.*ServiceImpl.*(..))" id="txPc"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /> </aop:config> --> <!-- ========================================================================================= --> <!-- 开启注解事务(这里采用) --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 将SessionFactory配置到spring容器中 --> <!-- 加载配置方案1(不推荐):仍然使用外部的hibernate.cfg.xml配置信息 --> <!-- <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <property name="configLocation" value="classpath:hibernate.cfg.xml" ></property> </bean> --> <!-- 加载配置方案(这里采用):在spring配置中放置hibernate配置信息 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 --> <property name="dataSource" ref="dataSource" ></property> <!-- 配置hibernate基本信息(四大基本配置已经在dataSource中配置过) --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect" >org.hibernate.dialect.mysqlDialect</prop> <prop key="hibernate.show_sql" >true</prop> <prop key="hibernate.format_sql" >true</prop> <prop key="hibernate.hbm2ddl.auto" >update</prop> </props> </property> <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 --> <property name="mappingDirectoryLocations" value="classpath:domain" ></property> </bean> <!-- action --> <!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 --> <bean name="userAction" class="web.action.UserAction" scope="prototype" > <property name="userService" ref="userService" ></property> </bean> <!-- service --> <bean name="userService" class="service.impl.UserServiceImpl" > <property name="ud" ref="userDao" ></property> </bean> <!-- dao --> <bean name="userDao" class="dao.impl.UserDaoImpl" > <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory" ></property> </bean> </beans>
db.properties:
数据库四大基本配置的键值对:
这里加入jdbc.前缀为了防止读取时候与其他类冲突
jdbc.jdbcUrl=jdbc:mysql:///hibernate
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=xuyiqing
struts2配置文件:
struts.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- # struts.objectFactory = spring 将action的创建交给spring容器 struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性 --> <constant name="struts.objectFactory" value="spring"></constant> <package name="crm" namespace="/" extends="struts-default" > <global-exception-mappings> <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping> </global-exception-mappings> <!-- 整合方案1:class属性上仍然配置action的完整类名 struts2仍然创建action,由spring负责组装Action中的依赖属性 --> <!-- 整合方案2(这里采用):class属性上填写spring中action对象的BeanName 完全由spring管理action生命周期,包括Action的创建 注意:需要手动组装依赖属性 --> <action name="UserAction_*" class="userAction" method="{1}" > <result name="toHome" type="redirect" >/index.htm</result> <result name="error" >/login.jsp</result> </action> </package> </struts>
接下来完成案例:
User实体类:
package domain; public class User { /* * CREATE TABLE `sys_user` ( `user_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘用户id‘, `user_code` varchar(32) NOT NULL COMMENT ‘用户账号‘, `user_name` varchar(64) NOT NULL COMMENT ‘用户昵称‘, `user_password` varchar(32) NOT NULL COMMENT ‘用户密码‘, `user_state` char(1) NOT NULL COMMENT ‘1:正常,0:暂停‘, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; */ private Long user_id; private String user_code; private String user_name; private String user_password; private Character user_state; public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Character getUser_state() { return user_state; } public void setUser_state(Character user_state) { this.user_state = user_state; } @Override public String toString() { return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password=" + user_password + "]"; } }
ORM元数据配置:User.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain" > <class name="User" table="sys_user" > <id name="user_id" > <generator class="native"></generator> </id> <property name="user_code" ></property> <property name="user_name" ></property> <property name="user_password" ></property> <property name="user_state" ></property> </class> </hibernate-mapping>
WEB层:
package web.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import domain.User; import service.UserService; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); private UserService userService ; public void setUserService(UserService userService) { this.userService = userService; } public String login() throws Exception { //1 调用Service执行登陆逻辑 User u = userService.getUserByCodePassword(user); //2 将返回的User对象放入session域 ActionContext.getContext().getSession().put("user", u); //3 重定向到项目首页 return "toHome"; } @Override public User getModel() { return user; } }
SERVICE层(这里注解配置AOP事务):
package service; import domain.User; public interface UserService { //登陆方法 User getUserByCodePassword(User u); //注册用户 void saveUser(User u); }
package service.impl; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import dao.UserDao; import domain.User; import service.UserService; @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true) public class UserServiceImpl implements UserService{ private UserDao ud; @Override public User getUserByCodePassword(User u) { //1 根据登陆名称查询登陆用户 User existU = ud.getByUserCode(u.getUser_code()); //2 判断用户是否存在.不存在=>抛出异常,提示用户名不存在 if(existU==null){ throw new RuntimeException("用户名不存在!"); } //3 判断用户密码是否正确=>不正确=>抛出异常,提示密码错误 if(!existU.getUser_password().equals(u.getUser_password())){ throw new RuntimeException("密码错误!"); } //4 返回查询到的用户对象 return existU; } @Override @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void saveUser(User u) { ud.save(u); } public void setUd(UserDao ud) { this.ud = ud; } }
DAO层:
package dao; import domain.User; public interface UserDao { //根据登陆名称查询user对象 User getByUserCode(String usercode); //保存用户 void save(User u); }
package dao.impl; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Restrictions; import org.springframework.orm.hibernate5.HibernateCallback; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import dao.UserDao; import domain.User; //HibernateDaoSupport 为dao注入sessionFactory public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override public User getByUserCode(final String usercode) { //HQL查询方式 return getHibernateTemplate().execute(new HibernateCallback<User>() { @Override public User doInHibernate(Session session) throws HibernateException { String hql = "from User where user_code = ? "; Query query = session.createQuery(hql); query.setParameter(0, usercode); User user = (User) query.uniqueResult(); return user; } }); //Criteria查询方式 /* DetachedCriteria dc = DetachedCriteria.forClass(User.class); dc.add(Restrictions.eq("user_code", usercode)); List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc); if(list != null && list.size()>0){ return list.get(0); }else{ return null; } */ } @Override public void save(User u) { getHibernateTemplate().save(u); } }
前端JSP登录页面表单:
<FORM action="${pageContext.request.contextPath}/UserAction_login" method=post> <INPUT style="WIDTH: 130px" name="user_code"> <INPUT type="password" name="user_password"> <INPUT type="submit" value="提交"> </FORM>
完成!