整合步骤
1.在项目中加入对应的jar文件
2. 配置对应的application-mybatis.xml配置文件
3. (可选)为项目加入事务
1.项目中加入对应的jar文件
项目需要mybatis的jar和spring的jar文件,和整合jar文件
1.aoplliance
2.aspectjweaver
3.commons-logging
4.log4j
5.mybatis
6.mysql-connector-java
7.spring-aop
8.spring-beans
9.spring-context
10.spring-core
11.spring-expression
12.spring-jdbc
13.spring-tx
14commons-dbcp
15.commons-pool
2.配置对应的配置文件
在resources下创建一个application-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull]]></value>
</property>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations">
<list>
<value>classpath:dao/**/*.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="dao"/>
</bean>
扫描包
<context:component-scan base-package="service"/>
</beans>
数据源可以使用外置的database.properties文件。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
对应的database.properties
driver=com.mysql.cj.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
user=root
password=root
然后对应的dao层的接口使用@repository注解,对应的service使用@service注解即可实现对应的功能。
3.为项目添加事务。
同样在对应的application-mybatis.xml中进行添加
<!-- 事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--通过标签指定的事务管理器设置事务属性-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!--声明事务规则-->
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<x:method>标签中的name属性是必需的,用于指定匹配的方法,这里需要对方法名进行约定,可以使用通配符(*),其他属性均为可选,用于指定事物规则
Propagation:事务传播机制
- required:默认值,表示如果存在一个事务,则支持当前事务,如果当前没有事务,则开启一个新事务
- requires_new:表示总是开启一个新的事务,如果一个事务已经存在 则将这个存在的事务挂起,开启新事务执行该方法
- mandatory:表示存在 一个事务,则支持当前事务,如果当前没有一个活动的事务,则抛出异常
- nested:表示如果当前存在一个活动的事务,则创建一个事务作为当前事务的嵌套事务运行,如果没有当前事务,该即值与required相同
- supports:表示如果存在 一个事务,则支持当前事务,如果没有事务,则按非事务方式执行
- not——supported:表示总是以非事务方式执行,如果一个事务已经存在,则将这个存在的事务,然后执行方法
- never:表示总是以非事务方式执行,如果当前存在一个活动的事务,则抛出异常
一般使用required满足大多数事务需求
Isolation:事务隔离等级,即当前事务和其他事务的隔离程序,在并发事务处理的情况下需要考虑它的设置。 - default:默认值 ,表示使用数据库默认的事务隔离级别
- read_uncommitted:未提交读
- read_committed:提交读
- repeatable_read:可重复读
- serlalizable:串行读
timeout:事务超时时间。允许事务运行的最长时间。以秒为单位,,超过给定的时间自动回滚,防止事务执行时间过长而影响系统性能
read-only:事务是否为只读。默认值为falsel;对于只执行查询功能的事务,把它设置为true
能提高事务处理的性能
Rollback-for:设定能够触发回滚的异常类型,spring中默认RuntimeException
No-rollback-for:设定不回滚的异常类型
同样可以使用注解实现 只要在service层添加一个@Transactional
然后只要在配置文件 中添加即可
<tx:annotation-driven transaction-manager="txManager"/>