4 使用原有的Dao方式开发
张创琦 2022.03.12
建立 ThreadLocal
- 测试案例1:
threadLocal 是线程安全的,而 list 是线程不安全的
package com.kkb.test;
import org.apache.ibatis.session.SqlSession;
import java.util.ArrayList;
import java.util.List;
public class ThreadLocalTest {
// 可以理解为一个容器,特殊点:只能盛放一个数据
private ThreadLocal<String> threadLocal = new ThreadLocal<>();
private List<String> list = new ArrayList<String>();
public static void main(String[] args) {
ThreadLocalTest test = new ThreadLocalTest();
// 添加数据
test.threadLocal.set("zcq");
test.threadLocal.set("jby"); // 再次添加添加会覆盖前面的值
// 取出数据
String s = test.threadLocal.get();
System.out.println(s);
}
}
运行结果:
jby
2. 测试案例2:
package com.kkb.test;
import org.apache.ibatis.session.SqlSession;
import java.util.ArrayList;
import java.util.List;
public class ThreadLocalTest {
// 可以理解为一个容器,特殊点:只能盛放一个数据
private ThreadLocal<String> threadLocal = new ThreadLocal<>();
private List<String> list = new ArrayList<String>();
class MyThread1 extends Thread {
@Override
public void run() {
threadLocal.set("jiabaoyu1"); // ThreadLocal 线程安全
list.add("aaaa"); // 线程不安全
System.out.println("MyThread1---threadLocal---"+threadLocal.get());
System.out.println("MyThread1---list---"+list.get(0));
}
}
class MyThread2 extends Thread {
@Override
public void run() {
threadLocal.set("lindaiyu2");
list.add("bbbb");
System.out.println("MyThread2---threadLocal---"+threadLocal.get());
System.out.println("MyThread2---list---"+list.get(0));
}
}
public static void main(String[] args) {
ThreadLocalTest test = new ThreadLocalTest();
MyThread1 t1 = test.new MyThread1();
MyThread2 t2 = test.new MyThread2();
t1.start();
t2.start();
}
}
运行结果:
MyThread1---threadLocal---jiabaoyu1
MyThread2---threadLocal---lindaiyu2
MyThread2---list---aaaa
MyThread1---list---aaaa // 解释: 值被覆盖了
- 创建 utils 类
package com.kkb.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.Reader;
/**
* ClassName: SessionUtil
* 连接操作的工具类
* @author wanglina
* @version 1.0
*/
public class MybatisUtil {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
private static SqlSessionFactory SqlSessionFactory;
/**
*
* 加载配置文件
*/ static{
try{
Reader reader = Resources.getResourceAsReader("mybatis.xml");
SqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(IOException e){ e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
*获取SqlSession
*@return
*/
public static SqlSession getSqlSession(){
//从当前线程获取
SqlSession sqlSession = threadLocal.get();
if(sqlSession == null){
// 创建sqlSession
sqlSession = SqlSessionFactory.openSession();
//将sqlSession与当前线程绑定
threadLocal.set(sqlSession);
}
return sqlSession;
}
/**
*关闭Session
*/
public static void closeSqlSession(){
//从当前线程获取
SqlSession sqlSession = threadLocal.get();
if(sqlSession != null){
sqlSession.close(); threadLocal.remove();
}
}
}
- 编写 Dao 接口和 Dao 的实现类
package com.kkb.dao;
import com.kkb.pojo.Team;
import java.util.List;
// 原有的Dao的写法
public interface TeamDao {
List<Team> queryAll();
Team queryById(Integer teamId);
int add(Team team);
int update(Team team);
int del(Integer teamId);
}
package com.kkb.dao;
import com.kkb.pojo.Team;
import com.kkb.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class TeamDaoImpl implements TeamDao {
// 查询所有球队
@Override
public List<Team> queryAll() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
List<Team> teamList = sqlSession.selectList("com.kkb.pojo.Team.findAll");
return teamList;
}
// 根据id查询单个球队
@Override
public Team queryById(Integer teamId) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
Team team = sqlSession.selectOne("com.kkb.pojo.Team.queryById", teamId);
return team;
}
// 添加球队
@Override
public int add(Team team) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
int num = sqlSession.insert("com.kkb.pojo.Team.add", team);
sqlSession.commit();
return num;
}
//更新球队
@Override
public int update(Team team) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
int num = sqlSession.insert("com.kkb.pojo.Team.update", team);
sqlSession.commit();
return num;
}
// 根据id删除球队
@Override
public int del(Integer teamId) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
int num = sqlSession.insert("com.kkb.pojo.Team.del", teamId);
sqlSession.commit();
return num;
}
}
- 编写测试类
package com.kkb.test;
import com.kkb.dao.TeamDao;
import com.kkb.dao.TeamDaoImpl;
import com.kkb.pojo.Team;
import org.junit.Test;
import java.util.Date;
import java.util.List;
/**
*ClassName: TestTeam
*测试类
*@author wanglina
*@version 1.0
*/
public class TestTeamDao {
private TeamDao teamDao=new TeamDaoImpl();
// 成功
@Test
public void testDel(){
int num = teamDao.del(1058);
System.out.println(num);
}
// 成功
@Test
public void testUpdate(){
Team team = teamDao.queryById(1054);
team.setTeamName("lina");
team.setLocation("bj");
int num = teamDao.update(team);
System.out.println(num);
}
// 会乱码
@Test
public void testAdd(){
Team team=new Team();
team.setTeamName("lina的球队");
team.setLocation("北京");
team.setCreateTime(new Date());
int num = teamDao.add( team);
System.out.println(num);
}
// 成功
@Test
public void test02(){
Team team = teamDao.queryById(1001);
System.out.println(team);
}
// 成功
@Test
public void test01(){
List<Team> teams = teamDao.queryAll();
teams.forEach(team -> System.out.println(team));
}
}