Spring3.2+mybatis3.2+Struts2.3整合
1.Spring3.2不能用于JDK1.8,只能用于JDK1.7。JDK1.8用spring4.0.
2.导入的jar包
3.目录结构:
4.配置Spring
配置数据库信息:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 3 4 <!-- 0.连接池属性设置读取指定的properties文件 --> 5 <context:property-placeholder location="classpath:db.properties" /> 6 7 <!-- 1.将连接池放入spring容器 --> 8 <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 9 <property name="jdbcUrl" value="${jdbc.url}"></property> 10 <property name="driverClass" value="${jdbc.driver}"></property> 11 <property name="user" value="${jdbc.username}"></property> 12 <property name="password" value="${jdbc.password}"></property> 13 </bean> 14 15 16 17 <!--2. 配置 Mybatis的会话工厂 --> 18 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 19 <!-- 数据源 --> 20 <property name="dataSource" ref="dataSource" /> 21 <!-- 配置Mybatis的核心 配置文件所在位置 --> 22 <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> 23 </bean> 24 25 26 27 <!-- 3.1 mapper代理配置方法一 这种方法需要大量重复的配置代理对象 28 MapperFactoryBean:根绝mapper接口生成代理对象 29 30 <bean id="selectUser" class="org.mybatis.spring.mapper.MapperFactoryBean"> 31 <property name="mapperInterface" value="cn.qlq.core.dao.SelectUser"></property> 32 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 33 </bean> 34 --> 35 36 37 38 39 <!-- 3.2通过MapperScannerConfigurer扫描进行批量生成代理对象 40 遵循规范:mapper.java和mapper.xml名字一样且在同一个目录下 41 自动扫描出来的代理对象的id为mapper类类名(首字母小写) 42 --> 43 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 44 <!-- 指定扫描的包名,如果有多个,用半角逗号分隔 --> 45 <property name="basePackage" value="cn.xm.mapper"></property> 46 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 47 </bean> 48 49 50 <!-- 4.配置事务管理器 --> 51 <!-- 事务核心管理器,封装了事务操作,依赖于连接池 --> 52 <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 53 <property name="dataSource" ref="dataSource"></property> 54 </bean> 55 56 <!-- 5.开启注解管理aop事务 --> 57 <tx:annotation-driven/> 58 59 60 61 <!-- 事务模板对象,依赖于事务核心管理器 --> 62 <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> 63 <property name="transactionManager" ref="transactionManager"></property> 64 </bean> 65 66 <!-- ················开始使用XML管理事务························ --> 67 <!-- 配置事务通知(无论哪种方式都要用到事务的核心管理器)--> 68 <tx:advice transaction-manager="transactionManager" id="firstTx"> 69 <tx:attributes> 70 <!--以方法为单位,指定方法应用事务什么属性 71 isolation:隔离级别 72 read-only:只读属性 73 propagation:传播行为 74 --> 75 <!-- 企业中运用通配符命名规则。两套增删改查(8种) --> 76 <tx:method name="save*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/> 77 <tx:method name="persist*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/> 78 <tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/> 79 <tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/> 80 <tx:method name="update*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/> 81 <tx:method name="modify*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/> 82 <tx:method name="get*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/> 83 <tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/> 84 </tx:attributes> 85 </tx:advice> 86 87 <!-- 配置织入 --> 88 <aop:config> 89 <!-- 配置切点表达式 --> 90 <aop:pointcut expression="execution(* cn.qlq.Service.*ServiceImpl.*(..))" id="texPc"/> 91 <!-- 配置切面:切点+通知 92 advice-ref:通知名称 93 pointcut-ref:切点名称 94 --> 95 <aop:advisor advice-ref="firstTx" pointcut-ref="texPc"/> 96 </aop:config> 97 </beans>
配置service
<?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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--4.注解扫描service --> <!-- 4.开启组件自动扫描,也就是启用注解。前提是导入spring-context-3.2.xsd约束和引入新的命名空间,注解扫描service出错了 --> <context:component-scan base-package="cn.xm.service"></context:component-scan> <!-- userService配置 --> <!-- <bean name="userService" class="cn.xm.service.impl.UserServiceImpl"></bean> --> </beans>
配置Action
<?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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 与struts2整合的配置 --> <!-- Action配置 --> <bean name="userAction" class="cn.xm.web.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> </beans>
4.配置Struts
<?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的名字必须唯一!只能拦截.action请求 默认值:class:com.opensymphony.xwork2.ActionSupport method:execute result的name属性:success 方法中返回success即可跳转到结果对应的页面 --> <!-- ### if specified, the default object factory can be overridden here ### Note: short-hand notation is supported in some cases, such as "spring" ### Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here # struts.objectFactory = spring ### specifies the autoWiring logic when using the SpringObjectFactory. ### valid values are: name, type, auto, and constructor (name is the default) struts.objectFactory.spring.autoWire = name --> <constant name="struts.custom.i18n.resources" value="errors"></constant> <!-- struts.objectFactory = spring :将Struts创建对象工厂改为Spring struts.objectFactory.spring.autoWire = name spring自动装配Struts的依赖属性(默认开启) --> <constant name="struts.objectFactory" value="spring"></constant> <!-- 第一个package命名空间 --> <package name="user" namespace="/" extends="struts-default"> <action name="user_*" class="userAction" method="{1}"> <result>/index.jsp</result> </action> </package> </struts>
5.配置mybatis
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 只需要定义个别名,这个应该有--> <typeAliases > <package name="cn.xm.pojo"/> </typeAliases> <!-- 动态代理也不需要配置这个扫描,留着也行 --> <mappers> <!-- 原始DAO开发使用这个手动加载xml --> <package name="cn.xm.mapper"/> </mappers> </configuration>
6.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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>xm</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <!-- 处理POST提交乱码问题 --> <filter> <filter-name>encoding</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> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>cn.xm.web.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </web-app>
7.总结:
要想使用sring扫描需要在配置文件中开启自动扫描service
<context:component-scan base-package="cn.xm.service"></context:component-scan>
要想在Service装配动态代理对象
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】