一.配置文件
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>
<!-- 开启注解事务 -->
<tx:annotation-driven />
<context:component-scan base-package="包"></context:component-scan>
<context:annotation-config></context:annotation-config>
<!-- 将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> -->
<!-- 加载配置方案2:在spring配置中放置hibernate配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<!-- 必选配置 -->
<!-- <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop>
<prop key="hibernate.connection.username" >root</prop>
<prop key="hibernate.connection.password" >1234</prop> -->
<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:cn/itcast/domain" ></property>
</bean>
</beans>
自动装配注解
@Autowired
action层注解
@Controller
@Scope(“prototype”)
service层注解
@Service
dao层注解
@Repository
注意
dao层必须配置sessionFactory
@Resource
public void setMysessionFactory(SessionFactory sf) {
super.setSessionFactory(sf);
}
配置版
<?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>
<!-- 配置通知 -->
<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:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置将通知织入目标对象
配置切点
配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.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> -->
<!-- 加载配置方案2:在spring配置中放置hibernate配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<!-- 必选配置 -->
<!-- <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop>
<prop key="hibernate.connection.username" >root</prop>
<prop key="hibernate.connection.password" >1234</prop> -->
<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:cn/itcast/domain" ></property>
</bean>
<!-- action -->
<!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
<bean name="userAction" class="cn.itcast.web.action.UserAction" scope="prototype" >
<property name="userService" ref="userService" ></property>
</bean>
<bean name="customerAction" class="cn.itcast.web.action.CustomerAction" scope="prototype" >
<property name="cs" ref="customerService" ></property>
</bean>
<bean name="baseDictAction" class="cn.itcast.web.action.BaseDictAction" scope="prototype" >
<property name="baseDictService" ref="baseDictService" ></property>
</bean>
<bean name="linkManAction" class="cn.itcast.web.action.LinkManAction" scope="prototype" >
<property name="lms" ref="linkManService" ></property>
</bean>
<bean name="saleVisitAction" class="cn.itcast.web.action.SaleVisitAction" scope="prototype" >
<property name="svs" ref="saleVisitService" ></property>
</bean>
<!-- service -->
<bean name="userService" class="cn.itcast.service.impl.UserServiceImpl" >
<property name="ud" ref="userDao" ></property>
</bean>
<bean name="customerService" class="cn.itcast.service.impl.CustomerServiceImpl" >
<property name="cd" ref="customerDao" ></property>
</bean>
<bean name="baseDictService" class="cn.itcast.service.impl.BaseDictServiceImpl" >
<property name="bdd" ref="baseDictDao" ></property>
</bean>
<bean name="linkManService" class="cn.itcast.service.impl.LinkManServiceImpl" >
<property name="lmd" ref="linkManDao" ></property>
</bean>
<bean name="saleVisitService" class="cn.itcast.service.impl.SaleVisitServiceImpl" >
<property name="svd" ref="saleVisitDao" ></property>
</bean>
<!-- dao -->
<bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<bean name="customerDao" class="cn.itcast.dao.impl.CustomerDaoImpl" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<bean name="baseDictDao" class="cn.itcast.dao.impl.BaseDictDaoImpl" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<bean name="linkManDao" class="cn.itcast.dao.impl.LinkManDaoImpl" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<bean name="saleVisitDao" class="cn.itcast.dao.impl.SaleVisitDaoImpl" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
</beans>
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" >
<interceptors>
<!-- 注册拦截器 -->
<interceptor name="privilegeInterceptor" class="cn.itcast.web.interceptor.PrivilegeInterceptor"></interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="privilegeInterceptor">
<param name="excludeMethods">login,regist</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 指定默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- 全局结果集配置 -->
<global-results>
<result name="toLogin" type="redirect" >/login.jsp</result>
</global-results>
<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>
<result name="regist" >/regist.jsp</result>
</action>
<action name="CustomerAction_*" class="customerAction" method="{1}" >
<result name="industryCount" >/jsp/customer/industryCount.jsp</result>
<result name="edit" >/jsp/customer/add.jsp</result>
<result name="list" >/jsp/customer/list.jsp</result>
<result name="toList" type="redirectAction" >
<param name="namespace">/</param>
<param name="actionName">CustomerAction_list</param>
</result>
</action>
<!-- 数据字典Action -->
<action name="BaseDictAction" class="baseDictAction" method="execute" ></action>
<!-- 联系人Action配置 -->
<action name="LinkManAction_*" class="linkManAction" method="{1}" >
<result name="add" >/jsp/linkman/add.jsp</result>
<result name="error" >/jsp/linkman/list.jsp</result>
<result name="toList" type="redirectAction" >
<param name="namespace">/</param>
<param name="actionName">LinkManAction_list</param>
</result>
<result name="list" >/jsp/linkman/list.jsp</result>
</action>
<!-- 客户拜访记录Action配置 -->
<action name="SaleVisitAction_*" class="saleVisitAction" method="{1}" >
<result name="toList" type="redirectAction" >
<param name="namespace">/</param>
<param name="actionName">SaleVisitAction_list</param>
</result>
<result name="list" >/jsp/salevisit/list.jsp</result>
<result name="add" >/jsp/salevisit/add.jsp</result>
</action>
</package>
</struts>
db.properties
jdbc.jdbcUrl=jdbc:mysql:///数据库
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=密码
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_crm</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>