Spring
- ==Spring整合MyBatis==
- 配置資料源
- 1.引入jdbc.properties組態檔
- 2.整合Spring組態檔和properties組態檔
- 3.Druid連接池可選引數
- 4.Druid監控中心
- 5.測驗監控中心
- 整合MyBatis
- 1.匯入依賴
- 2.配置SqlSessionFactory
- 3.配置MapperScannerConfigurer
- 4.配置Service
- ==事務==
- 1.配置DataSourceTransactionManager
- 2.配置事務通知
- 3.事務屬性
- 3.1隔離級別
- 3.2傳播行為
- 3.3讀寫性
- 3.4事務超時
- 3.5事務回滾
- 4.編織
Spring整合MyBatis
配置資料源
1.引入jdbc.properties組態檔
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.init=1
jdbc.minIdle=1
jdbc.maxActive=3
2.整合Spring組態檔和properties組態檔
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context.spring-context.xsd">
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!--基本配置-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--配置初始化大小,最小,最大-->
<property name="initialSize" value="${jdbc.init}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<!--配置獲取連接等待超時的時間-->
<property name="maxWait" value="60000"/>
<!--配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒-->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!--配置一個連接在池中最小生存的時間,單位是毫秒-->
<property name="minEvictableIdleTimeMillis" value="300000"/>
</bean>
</beans>
3.Druid連接池可選引數
<!--配置初始化大小,最小,最大-->
<property name="initialSize" value="${jdbc.init}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<!--配置獲取連接等待超時的時間-->
<property name="maxWait" value="60000"/>
<!--配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒-->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!--配置一個連接在池中最小生存的時間,單位是毫秒-->
<property name="minEvictableIdleTimeMillis" value="300000"/>
4.Druid監控中心
5.測驗監控中心
整合MyBatis
1.匯入依賴
<!--Spring整合MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
2.配置SqlSessionFactory
<!--生產SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入連接池-->
<property name="dataSource" ref="dataSource"/>
<!--注冊mapper檔案資訊,如果映射檔案和dao介面 同包且同名,則此配置可以省略-->
<property name="mapperLocations">
<list>
<value>classpath:cn/ozl/dao/*.xml</value>
</list>
</property>
<!--為mapper檔案中的物體定義預設包路徑-->
<property name="typeAliasesPackage" value="cn.ozl.entity"></property>
</bean>
3.配置MapperScannerConfigurer
管理DAO實作類的創建,并創建DAO物件,存入工廠管理
1.掃描所有DAO介面,去構建DAO實作
2.將DAO實作存入工廠管理
3.DAO實作物件在工廠的id是:“首字母小寫的-介面額類名”,例如UserDao=》userDao
<bean id="mapperScannerConfigurer9" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.ozl.dao"/>
<!--只有一個SqlSessionFactory的bean可以省略-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
4.配置Service
<bean id="userService" class="cn.ozl.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
事務
applicationContext.xml
<!--引入一個事務管理器,其中依賴DataSource,借以獲得連接,進而控制事務邏輯-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事務通知-->
<tx:advice id="txManager" transaction-manager="tx">
<!--事務屬性-->
<tx:attributes>
<!--<tx:method name="queryUser" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" timeout="-1" rollback-for="Exception"/>-->
<tx:method name="queryUser" propagation="SUPPORTS" />
<!--<tx:method name="insertUser" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" rollback-for="Exception"/>-->
<tx:method name="*User" rollback-for="Exception"/>
<!--剩余的所有方法-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--編織 宣告式事務控制-->
<aop:config>
<aop:pointcut id="pzl_tx" expression="execution(* cn.ozl.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="txManager" pointcut-ref="pzl_tx"/>
</aop:config>
UserServiceImpl
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List<User> queryUsers() {
return userDao.queryUsers();
}
public void insertUser(User user) {
userDao.insertUser(user);
}
public void updateUser(User user) {
userDao.updateUser(user);
}
public Integer deleteUser(Integer id) {
Integer integer=userDao.deleteUser(id);
System.out.println(integer);
if (1==1){
try {
throw new SQLException("test 事務");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("test 事務!!");
}
}
return integer;
}
}
test測驗
@Test
public void testtx(){
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.deleteUser(1002);
}
1.配置DataSourceTransactionManager
事務管理器,其中持有DataSoure,可以控制事務功能(rollback,commit等)
<!--引入一個事務管理器,其中依賴DataSource,借以獲得連接,進而控制事務邏輯-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
注意:DataSourceTransactionManager和SqlSessionFactoryBean要注入同一個DataSource的bean,否則事務控制會失敗
2.配置事務通知
基于事務管理,進一步控制,生成一個額外的功能:advice
此Advice可以切入任何的需要事務的方法,通過事務管理器為方法控制事務
<!--事務通知-->
<tx:advice id="txManager" transaction-manager="tx">
<!--事務屬性-->
<tx:attributes>
<!--<tx:method name="queryUser" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" timeout="-1" rollback-for="Exception"/>-->
<tx:method name="queryUser" propagation="SUPPORTS" />
<!--<tx:method name="insertUser" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" rollback-for="Exception"/>-->
<tx:method name="*User" rollback-for="Exception"/>
<!--剩余的所有方法-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
3.事務屬性
3.1隔離級別
isolation
1.default (默認值)(采取資料庫的默認設定)(建議)
2.read-uncommited 讀未提交
3.read-commited 讀提交
4.repeatable-read 可重復讀
5.serialized-read 序列化讀
隔離級別由低到高:read-uncommited<read-commited<repeatable-read<serialized-read
安全性:級別越高,多事務并發時,越安全,因為共享的資料越來越少,事務彼此之間干擾減少
并發性:級別越高,多事務并發時,并發越差,因為共享的資料越來越少,事務間阻塞情況增多
事務并發時的安全問題
1.臟讀:一個事務讀取到另一個事務還未提交的資料
大于read-uncommited可防止
2.不可重復讀:一個事務內多次讀取一行資料的相同內容,其結果不一致
大于read-commited可防止
3,幻讀:一個事務內多次讀取一張表中相同的內容,其結果不一致
大于repeatale-read可防止
<!--<tx:method name="insertUser" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" rollback-for="Exception"/>-->
3.2傳播行為
propagation
當涉及到事務的嵌套(Service呼叫Service)中,可能存在的問題
1.SUPPORTS=不存在外部事務,則不開啟新的事務,存在外部事務,則合并到外部事務中(適合查詢)
2.REQUIRED=不存在外部事務,則開啟事務,存在外部事務,則合并到外部事務中(默認值)(適合增刪改)
<!--<tx:method name="insertUser" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" rollback-for="Exception"/>-->
3.3讀寫性
readonly
1.true:只讀,可提交查詢效率(適合查詢)
2.false:可讀可寫 (默認值)(適合增刪改)
<!--<tx:method name="insertUser" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" rollback-for="Exception"/>-->
3.4事務超時
timeout
當事務所需操作的資料被其他事務占用,則等待
1.100:自定義等待時間100(秒)
2.-1:由資料庫指定等待時間,默認值(建議)
<!--<tx:method name="insertUser" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" rollback-for="Exception"/>-->
3.5事務回滾
rollback-for
1.如果事務中拋出 RuntimeException,則自動回滾
2.如果事務中拋出 CheckException(非運行時例外 Exception),不會自動回滾,而是默認提交事務
3.處理方案:將CheckException轉換成RuntimeException上拋,或設定rollback-for=“Exception”
<tx:method name="*User" rollback-for="Exception"/>
public Integer deleteUser(Integer id) {
Integer integer=userDao.deleteUser(id);
System.out.println(integer);
if (1==1){
try {
throw new SQLException("test 事務");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("test 事務!!");
}
}
return integer;
}
4.編織
將管理的advice切入需要事務的業務中
<!--編織 宣告式事務控制-->
<aop:config>
<aop:pointcut id="pzl_tx" expression="execution(* cn.ozl.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="txManager" pointcut-ref="pzl_tx"/>
</aop:config>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/181001.html
標籤:其他
上一篇:學習記錄
下一篇:hibernate
