归档
总说明
- 源码仓库: 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);
Configuration configuration = builder.parse();
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.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()</