MyBatis-Plus 源码解说

归档

总说明

  • 源码仓库: https://github.com/baomidou/mybatis-plus
  • 克隆:git clone https://github.com/baomidou/mybatis-plus.git
  • 切分支(tag):git checkout 3.0
  • JDK: 21

单元测试

添加方法-UT

  • 参考:com.baomidou.mybatisplus.core.MybatisXMLConfigBuilderTest
    @Test
    void parse() throws IOException {
   
        ResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource("classpath:/MybatisXMLConfigBuilderTest.xml");
        MybatisXMLConfigBuilder builder = new MybatisXMLConfigBuilder(resource.getInputStream(), null); // ref: sign_c_110 | sign_cm_110
        Configuration configuration = builder.parse(); // ref: sign_m_110
        MappedStatement mappedStatement = configuration.getMappedStatement(..."EntityMapper.selectCount");
    }

    interface EntityMapper extends BaseMapper<Entity> {
    }

    @TableName(autoResultMap = true)
    static class Entity {
   
        private Long id;
        private String name;
    }
  • MybatisXMLConfigBuilderTest.xml
<configuration>
    <mappers>
        <mapper class="com.baomidou.mybatisplus.core.MybatisXMLConfigBuilderTest$EntityMapper"/>
    </mappers>
</configuration>

完整测试

  • 参考:com.baomidou.mybatisplus.test.MybatisTest
    private static SqlSessionFactory sqlSessionFactory;

    @BeforeAll
    public static void init() throws IOException, SQLException {
   
        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
        sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(reader);    // 关键部分

        Configuration configuration = sqlSessionFactory.getConfiguration();
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        typeHandlerRegistry.register(AgeEnum.class, MybatisEnumTypeHandler.class);  // 注册枚举类型处理器

        DataSource dataSource = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource();
        Connection connection = dataSource.getConnection();                         // 获取连接执行脚本
        ScriptRunner scriptRunner = new ScriptRunner(connection);
        scriptRunner.runScript(Resources.getResourceAsReader("h2/user.ddl.sql"));
    }

    @Test
    void test() {
   
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
   
            H2UserMapper mapper = sqlSession.getMapper(H2UserMapper.class); // 添加 Mapper, ref: sign_m_130
            mapper.myInsertWithNameVersion("test", 2);
            H2User h2User = new H2User(...);
            mapper.insert(h2User);
        }
    }

Spring 测试

  • 参考:com.baomidou.mybatisplus.test.h2.H2StudentMapperTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = {
   "classpath:h2/spring-test-h2.xml"})
class H2StudentMapperTest {
   
    @Resource
    protected H2StudentMapper studentMapper;

    @Test
    void testIn() {
   
        LambdaQueryWrapper<H2Student> wrapper = Wrappers.<H2Student>lambdaQuery()</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值