Mybatis入門(mén)_做測(cè)試的喵醬的博客-博客
市北ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!一、解決問(wèn)題MybatisUtils,封裝獲取mybatis的sqlSession 的方法。
需要解決2個(gè)問(wèn)題:
1.1、線程安全問(wèn)題需要為單例模式。一個(gè)線程中,get到的sqlSession是同一個(gè)。
1.2、事務(wù)安全問(wèn)題(我對(duì)事物的理解,就是將幾個(gè)行為,捆綁在一起,組合成一個(gè)事務(wù)。
在真正執(zhí)行事務(wù)之前,會(huì)先預(yù)演一遍。
預(yù)演時(shí),只有當(dāng)所有的行為都執(zhí)行成功時(shí),才會(huì)去執(zhí)行這個(gè)事務(wù)。保證每一個(gè)步驟都執(zhí)行成功。
當(dāng)其中一個(gè)行為執(zhí)行失敗,則整個(gè)事務(wù)執(zhí)行失敗。就不會(huì)去真正的執(zhí)行這個(gè)失敗的事物,每一個(gè)步驟,都沒(méi)有真正的去執(zhí)行。
舉例:
A 給B 轉(zhuǎn)賬100元。數(shù)據(jù)庫(kù)一共有2個(gè)行為,行為1是A扣款100元。行為2是B的余額中增加100元。
我們必須保證這兩個(gè)行為同時(shí)成功或者同時(shí)失敗,如果只有其中一個(gè)行為成功,另一個(gè)行為失敗,都會(huì)造成事故。
所以我們將A給B轉(zhuǎn)賬的這個(gè)情景,這2個(gè)行為組成一個(gè)事務(wù)。(線程1上的一個(gè)事務(wù))
A與B轉(zhuǎn)賬的同時(shí)(線程1),C也在給D轉(zhuǎn)賬(線程2),C與D 的行為組成一個(gè)事務(wù),A與B的行為組成一個(gè)事務(wù)。
每個(gè)線程需要有自己的單獨(dú)事務(wù),則每個(gè)線程需要有自己?jiǎn)为?dú)sqlSession。
1.3 我們想要的最終效果每個(gè)線程上的sqlSession是單例的。
但是線程與線程之間,每個(gè)線程都有一個(gè)屬于自己線程的sqlSession。
我們這里通過(guò)內(nèi)部靜態(tài)類(lèi)與ThreadLocal實(shí)現(xiàn)的。
二、代碼 2.1 工具類(lèi)MyBatisUtilsMyBatisUtils
package utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
// ThreadLocal,每個(gè)線程,都使用各自的一個(gè)SqlSession
private static ThreadLocalthreadLocal = new ThreadLocal<>();
static {
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e){
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in,"test");
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = threadLocal.get();
if(sqlSession==null){
sqlSession= sqlSessionFactory.openSession(false);
threadLocal.set(sqlSession);
}
return sqlSession;
}
public static void commit(){
SqlSession sqlSession = threadLocal.get();
if (sqlSession != null){
sqlSession.commit();
}
}
public static void closeSqlSession(){
SqlSession sqlSession = threadLocal.get();
if (sqlSession != null){
sqlSession.close();
threadLocal.remove();
}
}
}
注意:
1、sqlSession= sqlSessionFactory.openSession(false);
false 是不自動(dòng)進(jìn)行commit的,需要我們手動(dòng)的進(jìn)行commit。(事務(wù)的需要,)
2、ThreadLocal實(shí)現(xiàn)了每個(gè)線程有一個(gè)屬于自己的 sqlSession
2.2 方法調(diào)用package main;
import com.google.common.collect.Lists;
import entity.MyStudent;
import mapper.MyStudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import utils.MyBatisUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class App3 {
public static void main(String[] args) throws IOException {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// 得到MyStudent表的mapper
MyStudentMapper myStudentMapper = sqlSession.getMapper(MyStudentMapper.class);
// 調(diào)用mapper的方法
Listall = myStudentMapper.findAll();
System.out.println("all = " + all);
MyBatisUtils.closeSqlSession();
}
}
2.3 證明線程內(nèi)是單例的,多個(gè)線程間是不單例的。package main;
import entity.MyStudent;
import mapper.MyStudentMapper;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtils;
import java.io.IOException;
import java.util.List;
public class App4 {
public static void main(String[] args) throws IOException {
for (int i=0;i<3;i++){
new Thread(){
@Override
public void run() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// 得到MyStudent表的mapper
MyStudentMapper myStudentMapper = sqlSession.getMapper(MyStudentMapper.class);
// 調(diào)用mapper的方法
Listall = myStudentMapper.findAll();
System.out.println("all = " + all);
System.out.println(this.getId()+"sqlSession = " + sqlSession);
SqlSession sqlSession2 = MyBatisUtils.getSqlSession();
System.out.println(this.getId()+"sqlSession2 = " + sqlSession2);
MyBatisUtils.closeSqlSession();
}
}.start();
}
}
}
打印結(jié)果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=61163:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/lib/tools.jar:/Users/zhaohui/IdeaProjects/MyTest/YourBatiesStudy/target/classes:/Users/zhaohui/.m2/repository/org/mybatis/mybatis/3.5.6/mybatis-3.5.6.jar:/Users/zhaohui/.m2/repository/mysql/mysql-connector-java/8.0.20/mysql-connector-java-8.0.20.jar:/Users/zhaohui/.m2/repository/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar:/Users/zhaohui/.m2/repository/com/google/guava/guava/30.1.1-jre/guava-30.1.1-jre.jar:/Users/zhaohui/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/zhaohui/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/zhaohui/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar:/Users/zhaohui/.m2/repository/org/checkerframework/checker-qual/3.8.0/checker-qual-3.8.0.jar:/Users/zhaohui/.m2/repository/com/google/errorprone/error_prone_annotations/2.5.1/error_prone_annotations-2.5.1.jar:/Users/zhaohui/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/zhaohui/.m2/repository/org/projectlombok/lombok/1.18.18/lombok-1.18.18.jar main.App4
all = [MyStudent(id=201215121, name=李勇, age=20), MyStudent(id=201215122, name=劉晨, age=19), MyStudent(id=201215123, name=王敏, age=18), MyStudent(id=201215127, name=張三, age=19), MyStudent(id=201215128, name=張三, age=19), MyStudent(id=201215129, name=張三, age=19), MyStudent(id=201215130, name=張三, age=19), MyStudent(id=201215131, name=張三, age=19), MyStudent(id=201215132, name=張三, age=19), MyStudent(id=201215133, name=張三, age=19), MyStudent(id=201215134, name=張三三, age=19), MyStudent(id=201215135, name=李思思, age=19), MyStudent(id=201215136, name=張三, age=19), MyStudent(id=201215139, name=張三, age=19), MyStudent(id=201215140, name=張三三, age=19), MyStudent(id=201215141, name=李思思, age=19), MyStudent(id=201215142, name=張三, age=19), MyStudent(id=201215143, name=張三三, age=19), MyStudent(id=201215144, name=李思思, age=19)]
13sqlSession = org.apache.ibatis.session.defaults.DefaultSqlSession@1e57f15
13sqlSession2 = org.apache.ibatis.session.defaults.DefaultSqlSession@1e57f15
all = [MyStudent(id=201215121, name=李勇, age=20), MyStudent(id=201215122, name=劉晨, age=19), MyStudent(id=201215123, name=王敏, age=18), MyStudent(id=201215127, name=張三, age=19), MyStudent(id=201215128, name=張三, age=19), MyStudent(id=201215129, name=張三, age=19), MyStudent(id=201215130, name=張三, age=19), MyStudent(id=201215131, name=張三, age=19), MyStudent(id=201215132, name=張三, age=19), MyStudent(id=201215133, name=張三, age=19), MyStudent(id=201215134, name=張三三, age=19), MyStudent(id=201215135, name=李思思, age=19), MyStudent(id=201215136, name=張三, age=19), MyStudent(id=201215139, name=張三, age=19), MyStudent(id=201215140, name=張三三, age=19), MyStudent(id=201215141, name=李思思, age=19), MyStudent(id=201215142, name=張三, age=19), MyStudent(id=201215143, name=張三三, age=19), MyStudent(id=201215144, name=李思思, age=19)]
11sqlSession = org.apache.ibatis.session.defaults.DefaultSqlSession@2f73d49b
11sqlSession2 = org.apache.ibatis.session.defaults.DefaultSqlSession@2f73d49b
all = [MyStudent(id=201215121, name=李勇, age=20), MyStudent(id=201215122, name=劉晨, age=19), MyStudent(id=201215123, name=王敏, age=18), MyStudent(id=201215127, name=張三, age=19), MyStudent(id=201215128, name=張三, age=19), MyStudent(id=201215129, name=張三, age=19), MyStudent(id=201215130, name=張三, age=19), MyStudent(id=201215131, name=張三, age=19), MyStudent(id=201215132, name=張三, age=19), MyStudent(id=201215133, name=張三, age=19), MyStudent(id=201215134, name=張三三, age=19), MyStudent(id=201215135, name=李思思, age=19), MyStudent(id=201215136, name=張三, age=19), MyStudent(id=201215139, name=張三, age=19), MyStudent(id=201215140, name=張三三, age=19), MyStudent(id=201215141, name=李思思, age=19), MyStudent(id=201215142, name=張三, age=19), MyStudent(id=201215143, name=張三三, age=19), MyStudent(id=201215144, name=李思思, age=19)]
12sqlSession = org.apache.ibatis.session.defaults.DefaultSqlSession@4596a997
12sqlSession2 = org.apache.ibatis.session.defaults.DefaultSqlSession@4596a997
Process finished with exit code 0
同一個(gè)線程內(nèi)的多個(gè)?sqlSession 的物理地址是一樣的,
多個(gè)線程間,?sqlSession 的物理地址是不一樣的。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
文章題目:封裝MybatisUtils解決線程安全與事務(wù)問(wèn)題-創(chuàng)新互聯(lián)
地址分享:http://aaarwkj.com/article28/cchscp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、電子商務(wù)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容