重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Mybatis入门_做测试的喵酱的博客-博客
市北ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!一、解决问题MybatisUtils,封装获取mybatis的sqlSession 的方法。
需要解决2个问题:
1.1、线程安全问题需要为单例模式。一个线程中,get到的sqlSession是同一个。
1.2、事务安全问题(我对事物的理解,就是将几个行为,捆绑在一起,组合成一个事务。
在真正执行事务之前,会先预演一遍。
预演时,只有当所有的行为都执行成功时,才会去执行这个事务。保证每一个步骤都执行成功。
当其中一个行为执行失败,则整个事务执行失败。就不会去真正的执行这个失败的事物,每一个步骤,都没有真正的去执行。
举例:
A 给B 转账100元。数据库一共有2个行为,行为1是A扣款100元。行为2是B的余额中增加100元。
我们必须保证这两个行为同时成功或者同时失败,如果只有其中一个行为成功,另一个行为失败,都会造成事故。
所以我们将A给B转账的这个情景,这2个行为组成一个事务。(线程1上的一个事务)
A与B转账的同时(线程1),C也在给D转账(线程2),C与D 的行为组成一个事务,A与B的行为组成一个事务。
每个线程需要有自己的单独事务,则每个线程需要有自己单独sqlSession。
1.3 我们想要的最终效果每个线程上的sqlSession是单例的。
但是线程与线程之间,每个线程都有一个属于自己线程的sqlSession。
我们这里通过内部静态类与ThreadLocal实现的。
二、代码 2.1 工具类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,每个线程,都使用各自的一个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 是不自动进行commit的,需要我们手动的进行commit。(事务的需要,)
2、ThreadLocal实现了每个线程有一个属于自己的 sqlSession
2.2 方法调用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);
// 调用mapper的方法
Listall = myStudentMapper.findAll();
System.out.println("all = " + all);
MyBatisUtils.closeSqlSession();
}
}
2.3 证明线程内是单例的,多个线程间是不单例的。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);
// 调用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();
}
}
}
打印结果:
/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
同一个线程内的多个 sqlSession 的物理地址是一样的,
多个线程间, sqlSession 的物理地址是不一样的。
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧