Hibernate+Spring+Struts2整合
导入jar包,可使用maven管理
Spring+Struts2
创建struts2核心配置文件
1.位置在src下,名称是struts.xml
2.引入约束
<?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>
<package name="demo1" namespace="/" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="loginSuccess" >/index.htm</result>
<result name="login" >/login.jsp</result>
</action>
<action name="customer_*" class="com.llc.action.CustomerAction" method="{1}">
<result name="toAddPage">/jsp/customer/add.jsp</result>
<!-- 添加之后的操作 -->
<result name="add" type="redirectAction">customer_list.action</result>
<!-- 列表页面 -->
<result name="list">/jsp/customer/list.jsp </result>
<!-- 删除之后 -->
<result name="delete" type="redirectAction">customer_list.action</result>
</action>
</package>
</struts>
配置Struts2的过滤器+配置spring监听器,让启动时候只加载一次配置文件,并且指定spring配置文件的位置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 让启动时候只加载一次配置文件 -->
<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>
<!-- 配置Struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
在spring配置action对象,让struts.xml中应用action对象
Spring+Hibernate
在Spring配置c3p0连接池
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SSH_CRM?characterEncoding=UTF-8"></property>
<property name="user" value="root"></property>
<property name="password" value="root3306"></property>
</bean>
把hibernate的sessionFactory交给sping管理
1.服务器启动时候,加载spring配置文件,把配置文件中对象创建
2.把sessionFactory对象创建在spring配置
3.因为创建sessionFactory代码不是new出来的,而是多行代码实现的
4.spring里面针对上面情况,封装类,配置类对象可以创建sessionFactorydataSource是连接池,告诉他我需要的数据库是哪个
<!--sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--引入hibernate其他除了连接池之外的配置-->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
hibernate除了连接池以外的配置
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/llc/entity/User.hbm.xml"></mapping>
<mapping resource="com/llc/entity/Customer.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
第二种配置,弃用hibernate.cfg.xml文件,完全交给spring管理
<!-- sessionFactory创建交给spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<!-- 配置映射文件引入 -->
<property name="mappingResources">
<list>
<value>com/llc/entity/User.hbm.xml</value>
</list>
</property>
</bean>
在dao里面使用hibernateTemplate
方法一 直接继承 HibernateTemplate 就可以使用
public class UserDAOImpl extends HibernateTemplate implements UserDAO {
//登陆方法
@Override
public User loginUser(User user) {
//登陆查询的操作
List<User> list = (List<User>) find("from User where username=? and password = ?", user.getUsername(), user.getPassword());
if (list != null && list.size()!=0) {
return list.get(0);
}
return null;
}
}
方法二 继承 HibernateDaoSupport,Spring帮完成hibernateTemplate注入
只要在需要使用的Spring里面配置sessionFactory
<bean id="userDAOImpl" class="com.llc.dao.impl.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
在dao里面得到 HibernateTemplate 对象
dao里面使用
public class UserDAOImpl implements UserDAO {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
在spring配置里面注入
<bean id="userDAOImpl" class="com.llc.dao.impl.UserDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
配置事物操作
需要开启事物管理器,并且开启事物管理器注解
**使用的时候只要在service里面加注解 @Transactional
**
<!-- 事物管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事物注解 -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager"></tx:annotation-driven>