1.Spring3.2不能用于JDK1.8,只能用于JDK1.7。JDK1.8用spring4.0.
2.导入的jar包
3.目录结构:
4.配置Spring
配置数据库信息:
<?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 "> <!-- 0.连接池属性设置读取指定的properties文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!--2. 配置 Mybatis的会话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置Mybatis的核心 配置文件所在位置 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean> <!-- 3.1 mapper代理配置方法一 这种方法需要大量重复的配置代理对象
MapperFactoryBean:根绝mapper接口生成代理对象 <bean id="selectUser" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.qlq.core.dao.SelectUser"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
--> <!-- 3.2通过MapperScannerConfigurer扫描进行批量生成代理对象
遵循规范:mapper.java和mapper.xml名字一样且在同一个目录下
自动扫描出来的代理对象的id为mapper类类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包名,如果有多个,用半角逗号分隔 -->
<property name="basePackage" value="cn.xm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- 4.配置事务管理器 -->
<!-- 事务核心管理器,封装了事务操作,依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 5.开启注解管理aop事务 -->
<tx:annotation-driven/> <!-- 事务模板对象,依赖于事务核心管理器 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean> <!-- ················开始使用XML管理事务························ -->
<!-- 配置事务通知(无论哪种方式都要用到事务的核心管理器)-->
<tx:advice transaction-manager="transactionManager" id="firstTx">
<tx:attributes>
<!--以方法为单位,指定方法应用事务什么属性
isolation:隔离级别
read-only:只读属性
propagation:传播行为
-->
<!-- 企业中运用通配符命名规则。两套增删改查(8种) -->
<tx:method name="save*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="persist*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="update*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="modify*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="get*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- 配置织入 -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* cn.qlq.Service.*ServiceImpl.*(..))" id="texPc"/>
<!-- 配置切面:切点+通知
advice-ref:通知名称
pointcut-ref:切点名称
-->
<aop:advisor advice-ref="firstTx" pointcut-ref="texPc"/>
</aop:config>
</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装配动态代理对象