03 Lombok+AOP思想+注解进化SqlSession工具类03

Lombok

实体类中除了定义好的属性外,我们常常还要写set/get构造方法等,用Lombok可以用一条注解帮忙生成。

  • 一个@Data会同时包含
    @Getter/@Setter
    @ToString
    @EqualsAndHashCode
    @RequiredArgsConstructor

使用方法:需要同时添加Lombok插件和Lombok依赖。

在之前,因为每次获取一个sqlSession对象太麻烦,我们在02节封装了方法进SqlSessionUtil类中:

java 复制代码
package com.sunsplanter.mybatis.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;

public class SqlSessionUtil {
    //工具类的构造方法一般都是私有化的,目的是防止创建对象
    //工具类中的方法一般都是静态,目的是采用类名即可直接调用
    private static SqlSessionFactory sqlSessionFactory;

    /**
     * sqlSessionFactory与数据库是一对一的关系,因此类加载时初始化sqlSessionFactory对象
     * 而后长期存在,以后需要的时候仅仅是通过openSession获取一个会话对象
     */
    static {
        try {
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 每调用一次openSession()可获取一个新的会话,该会话支持自动提交。
     * @return 新的会话对象
     */
    public static SqlSession openSession() {
        return sqlSessionFactory.openSession(true);
    }
}

此后每个测试方法中代码得以简化为:

java 复制代码
    @Test
    public void testInsertCar(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        // 执行SQL
        int count = sqlSession.insert("insertCar");
        System.out.println("插入了几条记录:" + count);
        sqlSession.close();
    }

但这是比较老的方法。

我们可以利用AOP+注解进化,现在不再需要新建一个工具类,直接将抽象的方法放在测试类中,配合BeforeEach注解使用。

一个特别注意的点是,@BeforeEach仅在Junit5,注意引对依赖:

xml 复制代码
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
        </dependency>

测试类中代码如下:

java 复制代码
    private SqlSession session;
   
    // junit会在每一个@Test方法前执行@BeforeEach方法
    @BeforeEach
    public void init() throws IOException {
        session = new SqlSessionFactoryBuilder()
                .build(
                        Resources.getResourceAsStream("mybatis-config.xml"))
                .openSession();
    }

    @Test
    public void testInsertCar() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        // 执行SQL
        int count = sqlSession.insert("insertCar");
        System.out.println("插入了几条记录:" + count);
        sqlSession.close();
    }
相关推荐
Flynn Liang35 分钟前
保姆级|Spring AI 2.0 第一个 Demo:Spring Boot 4 接入大模型(含国内可用方案 + 完整 pom)
人工智能·spring·ollama·springai·deepseek·springai入门·java接入大模型
会编程的吕洞宾1 小时前
Spring AI 语义缓存:重复提问不重复花钱
java·人工智能·后端·spring
郭小李2 小时前
spring @RequestMapping注解
java·后端·spring
CarIise12 小时前
SpringMVC 异常处理与拦截器
大数据·spring
知守观13 小时前
设计模式实战:从真实老项目中看标准模板方法与工厂的落地
java·spring·设计模式
\光辉岁月/15 小时前
3.spring5-AOP
spring·代理模式·aop·spring5
东华果汁哥16 小时前
【Spring AI 实战】从零开始调用大模型接口,构建 Java AI 应用
java·人工智能·spring
java_nnnn20 小时前
Spring MVC小案例(1)
java·后端·spring·mvc
七夜zippoe1 天前
Spring AI + Ollama 本地模型:数据不出内网的合规方案(落地全流程)
java·人工智能·spring·ai·ollama
captain3761 天前
Spring Web MVC(2)
java·前端·spring boot·后端·spring·mvc