代理模式
代理模式,可以为目标类添加额外功能。
Spring 动态代理
增强类型
切入点表达式
execution(返回值类型 包名.类名.方法名(参数表))
execution(* com.bcl.service.*.*(..));
args(参数表)
within(全类名)
@annotation(自定义注解)
事务的隔离级别
到目前为止web.xml中出现的标签:servlet
filter
listener
context-param
。
加载顺序,从前到后:
context-param 定义一对键值对数据,通常为listener使用
listener ContextLoaderListener:在web应用启动时执行,创建Spring工厂
filter Struts2Filter:过滤所有请求
servlet
Servlet的url-pattern有4种配置格式:
精确匹配 /book/showAllBooks
路径匹配 /* /book/*
后缀名匹配 *.action *.do *.jsp
缺省匹配 / 在上面3种都无法匹配的情况下,做默认匹配
优先级从高到低:
1 精确匹配 2 路径匹配 3后缀名匹配 4缺省匹配
注意点:
搭建开发环境
新建项目
导入依赖
数据库驱动依赖:ojdbcx.jar mysql-connector-java.jar
MyBatis相关的依赖
servlet+jsp+jstl的依赖
struts2的依赖
junit+hutool+druid
配置文件+工具类
jdbc.properties
log4j.properties
xxxMapper.xml
mybatis-config.xml
struts.xml
配置文件初始化
web.xml 配置Struts2Filter
mybatis-config.xml 初始化配置
建表
实体
mapper
service
接口
实现:
SqlSession sqlSession = MyBatisUtils.openSession();
XxxMapper mapper = sqlSession.getMapper(XxxMapper.class);
test
action+jsp
集成测试
准备工作,添加 mybatis-spring
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.4version>
dependency>
初版配置:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="typeAliasesPackage" value="com.bcl.entity"/>
<property name="mapperLocations">
<list>
<value>classpath:com/bcl/mapper/*Mapper.xmlvalue>
list>
property>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionTemplate" ref="sqlSession"/>
<property name="mapperInterface" value="com.bcl.mapper.UserMapper"/>
bean>
<bean id="userService" class="com.bcl.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
bean>
最终配置:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="typeAliasesPackage" value="com.bcl.entity"/>
<property name="mapperLocations">
<list> <value>classpath:com/bcl/mapper/*Mapper.xmlvalue>
list>
property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bcl.mapper"/>
bean>
<bean id="userService" class="com.bcl.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
bean>
搭建开发环境
新建web项目
导入依赖
jdbc驱动依赖
mybatis依赖
struts2依赖
spring依赖
spring整合mybaits依赖
spring整合struts2依赖
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.26.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.48version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.4version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.30version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.26.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.3.26.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.26.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.3version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-coreartifactId>
<version>2.3.16.3version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.4version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.14version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.3.26.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-spring-pluginartifactId>
<version>2.3.16.3version>
dependency>
导入配置文件
jdbc.properties
log4j.properties
xxxMapper.xml
mybatis-config.xmlstruts.xml
applicationContext.xml
配置文件初始化
web.xml
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<filter>
<filter-name>Struts2Filterfilter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>Struts2Filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
applicationContext.xml
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="typeAliasesPackage" value="com.bcl.entity"/>
<property name="mapperLocations">
<list>
<value>classpath:com/bcl/mapper/*Mapper.xmlvalue>
list>
property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bcl.mapper"/>
bean>
建表
实体
mapper
service
接口
实现
class XxxServiceImpl implements XxxService{
private XxxMapper xxxMapper;
public void setXxxMapper(XxxMapper mapper){
this.xxxMapper = mapper;
}
业务方法...
}
test
action+jsp
XxxAction{
private XxxService xxxService;
public void setXxxService(XxxService service){
this.xxxService = service;
}
服务方法...
}
集成测试
Spring提供2种控制方式:
Spring AOP声明式事务控制的步骤:
定义原始类对象(service对象)
<bean id="userService" class="com.bcl.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
bean>
定义增强类,Spring内置有事务控制的增强
DataSourceTransactionManager事务增强类
配置增强类
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"/>
bean>
<tx:advice transaction-manager="txManager" id="txAdvice">
<tx:attributes>
<tx:method name="login" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
定义切点
编织组装
<aop:config>
<aop:pointcut id="servicePointCut" expression="execution(* com.bcl.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut"/>
aop:config>
read-only="true"
只读事务,不能执行增删改,但对于查询有性能优化。
timeout 超时机制
如果超过执行时间,事务自动回滚。默认值-1,表示跟数据库的配置有关。
rollback-for和no-rollback-for
rollback-for 定义异常类型:事务遇到这种异常会回滚
no-rollback-for 定义异常类型:事务遇到这种异常不会回滚
Spring中,rollback-for的默认值是RuntimeException,no-rollback-for的默认值是Exception。
自定义异常时,发生该异常需要回滚事务,继承RuntimeException
自定义异常时,发生该异常不需要回滚事务,继承Exception
propagation(传播机制)
企业开发时,业务复杂时,会出现业务方法调用业务方法的情况。propagation定义了一个业务方法被另外一个业务方法调用时,事务的传播方式。
常见的事务传播机制:
REQUIRED 如果外部有事务,加入外部事务,如果没有则新建
SUPPORTS 如果外部有事务,加入外部事务,如果没有则以无事务的状态运行
REQUIRES_NEW 无论是否有外部事务,都会新建一个事务
isolation(隔离级别)
READ_UNCOMMITTED 读未提交
READ_COMMITTED 读提交 实战时使用
REPEATABLE_READ 可重复读
SERIALIZABLE 序列化读
如果你觉得这篇内容对你挺有有帮助的话: