4 使用原有的Dao方式开发

张创琦 2022.03.12

  1. 建立 ThreadLocal

    1. 测试案例1:

threadLocal 是线程安全的,而 list 是线程不安全的

  1. package com.kkb.test;
  2. import org.apache.ibatis.session.SqlSession;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class ThreadLocalTest {
  6. // 可以理解为一个容器,特殊点:只能盛放一个数据
  7. private ThreadLocal<String> threadLocal = new ThreadLocal<>();
  8. private List<String> list = new ArrayList<String>();
  9. public static void main(String[] args) {
  10. ThreadLocalTest test = new ThreadLocalTest();
  11. // 添加数据
  12. test.threadLocal.set("zcq");
  13. test.threadLocal.set("jby"); // 再次添加添加会覆盖前面的值
  14. // 取出数据
  15. String s = test.threadLocal.get();
  16. System.out.println(s);
  17. }
  18. }

运行结果:

  1. jby
  1. 2. 测试案例2
  1. package com.kkb.test;
  2. import org.apache.ibatis.session.SqlSession;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class ThreadLocalTest {
  6. // 可以理解为一个容器,特殊点:只能盛放一个数据
  7. private ThreadLocal<String> threadLocal = new ThreadLocal<>();
  8. private List<String> list = new ArrayList<String>();
  9. class MyThread1 extends Thread {
  10. @Override
  11. public void run() {
  12. threadLocal.set("jiabaoyu1"); // ThreadLocal 线程安全
  13. list.add("aaaa"); // 线程不安全
  14. System.out.println("MyThread1---threadLocal---"+threadLocal.get());
  15. System.out.println("MyThread1---list---"+list.get(0));
  16. }
  17. }
  18. class MyThread2 extends Thread {
  19. @Override
  20. public void run() {
  21. threadLocal.set("lindaiyu2");
  22. list.add("bbbb");
  23. System.out.println("MyThread2---threadLocal---"+threadLocal.get());
  24. System.out.println("MyThread2---list---"+list.get(0));
  25. }
  26. }
  27. public static void main(String[] args) {
  28. ThreadLocalTest test = new ThreadLocalTest();
  29. MyThread1 t1 = test.new MyThread1();
  30. MyThread2 t2 = test.new MyThread2();
  31. t1.start();
  32. t2.start();
  33. }
  34. }

运行结果:

  1. MyThread1---threadLocal---jiabaoyu1
  2. MyThread2---threadLocal---lindaiyu2
  3. MyThread2---list---aaaa
  4. MyThread1---list---aaaa // 解释: 值被覆盖了
  1. 创建 utils 类
  1. package com.kkb.utils;
  2. import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession;
  3. import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  4. import java.io.IOException;
  5. import java.io.Reader;
  6. /**
  7. * ClassName: SessionUtil
  8. * 连接操作的工具类
  9. * @author wanglina
  10. * @version 1.0
  11. */
  12. public class MybatisUtil {
  13. private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
  14. private static SqlSessionFactory SqlSessionFactory;
  15. /**
  16. *
  17. * 加载配置文件
  18. */ static{
  19. try{
  20. Reader reader = Resources.getResourceAsReader("mybatis.xml");
  21. SqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  22. }catch(IOException e){ e.printStackTrace();
  23. throw new RuntimeException(e);
  24. }
  25. }
  26. /**
  27. *获取SqlSession
  28. *@return
  29. */
  30. public static SqlSession getSqlSession(){
  31. //从当前线程获取
  32. SqlSession sqlSession = threadLocal.get();
  33. if(sqlSession == null){
  34. // 创建sqlSession
  35. sqlSession = SqlSessionFactory.openSession();
  36. //将sqlSession与当前线程绑定
  37. threadLocal.set(sqlSession);
  38. }
  39. return sqlSession;
  40. }
  41. /**
  42. *关闭Session
  43. */
  44. public static void closeSqlSession(){
  45. //从当前线程获取
  46. SqlSession sqlSession = threadLocal.get();
  47. if(sqlSession != null){
  48. sqlSession.close(); threadLocal.remove();
  49. }
  50. }
  51. }
  1. 编写 Dao 接口和 Dao 的实现类
  1. package com.kkb.dao;
  2. import com.kkb.pojo.Team;
  3. import java.util.List;
  4. // 原有的Dao的写法
  5. public interface TeamDao {
  6. List<Team> queryAll();
  7. Team queryById(Integer teamId);
  8. int add(Team team);
  9. int update(Team team);
  10. int del(Integer teamId);
  11. }
  1. package com.kkb.dao;
  2. import com.kkb.pojo.Team;
  3. import com.kkb.utils.MybatisUtil;
  4. import org.apache.ibatis.session.SqlSession;
  5. import java.util.List;
  6. public class TeamDaoImpl implements TeamDao {
  7. // 查询所有球队
  8. @Override
  9. public List<Team> queryAll() {
  10. SqlSession sqlSession = MybatisUtil.getSqlSession();
  11. List<Team> teamList = sqlSession.selectList("com.kkb.pojo.Team.findAll");
  12. return teamList;
  13. }
  14. // 根据id查询单个球队
  15. @Override
  16. public Team queryById(Integer teamId) {
  17. SqlSession sqlSession = MybatisUtil.getSqlSession();
  18. Team team = sqlSession.selectOne("com.kkb.pojo.Team.queryById", teamId);
  19. return team;
  20. }
  21. // 添加球队
  22. @Override
  23. public int add(Team team) {
  24. SqlSession sqlSession = MybatisUtil.getSqlSession();
  25. int num = sqlSession.insert("com.kkb.pojo.Team.add", team);
  26. sqlSession.commit();
  27. return num;
  28. }
  29. //更新球队
  30. @Override
  31. public int update(Team team) {
  32. SqlSession sqlSession = MybatisUtil.getSqlSession();
  33. int num = sqlSession.insert("com.kkb.pojo.Team.update", team);
  34. sqlSession.commit();
  35. return num;
  36. }
  37. // 根据id删除球队
  38. @Override
  39. public int del(Integer teamId) {
  40. SqlSession sqlSession = MybatisUtil.getSqlSession();
  41. int num = sqlSession.insert("com.kkb.pojo.Team.del", teamId);
  42. sqlSession.commit();
  43. return num;
  44. }
  45. }
  1. 编写测试类
  1. package com.kkb.test;
  2. import com.kkb.dao.TeamDao;
  3. import com.kkb.dao.TeamDaoImpl;
  4. import com.kkb.pojo.Team;
  5. import org.junit.Test;
  6. import java.util.Date;
  7. import java.util.List;
  8. /**
  9. *ClassName: TestTeam
  10. *测试类
  11. *@author wanglina
  12. *@version 1.0
  13. */
  14. public class TestTeamDao {
  15. private TeamDao teamDao=new TeamDaoImpl();
  16. // 成功
  17. @Test
  18. public void testDel(){
  19. int num = teamDao.del(1058);
  20. System.out.println(num);
  21. }
  22. // 成功
  23. @Test
  24. public void testUpdate(){
  25. Team team = teamDao.queryById(1054);
  26. team.setTeamName("lina");
  27. team.setLocation("bj");
  28. int num = teamDao.update(team);
  29. System.out.println(num);
  30. }
  31. // 会乱码
  32. @Test
  33. public void testAdd(){
  34. Team team=new Team();
  35. team.setTeamName("lina的球队");
  36. team.setLocation("北京");
  37. team.setCreateTime(new Date());
  38. int num = teamDao.add( team);
  39. System.out.println(num);
  40. }
  41. // 成功
  42. @Test
  43. public void test02(){
  44. Team team = teamDao.queryById(1001);
  45. System.out.println(team);
  46. }
  47. // 成功
  48. @Test
  49. public void test01(){
  50. List<Team> teams = teamDao.queryAll();
  51. teams.forEach(team -> System.out.println(team));
  52. }
  53. }