第一步
在test目录下的resource文件夹下面创建mybatis配置文件,文件内容如下图
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql" />
</databaseIdProvider>
<mappers>
<mapper resource="com/dao/mapper/user/ExUserMapper.xml"/>
<mapper resource="com/dao/mapper/test/UserMapper.xml"/>
</mappers>
</configuration>
第二步
在配置文件中增加需要测试的 xxxMapper.xml
?
第三步
编写单元测试类
1、读取配置文件
public class BaseMapperFactory {
public static SqlSession getSession(){
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
return sqlSessionFactory.openSession();
}
}
2、获取SqlSession及事务
public class BaseMapperTest {
public SqlSession sqlSession;
@Before
public void setUp(){
sqlSession = BaseMapperFactory.getSession();
}
// 提交事务
public void commit(){
sqlSession.commit();
}
// 回滚事务
public void rollBack(){
sqlSession.rollback();
}
}
3、进行方法测试
public class ExUserMapperTest extends BaseMapperTest {
@Test
public void batchInsert() {
User user = new User();
user.setEmail("1");
ExUserMapper mapper = sqlSession.getMapper(ExUserMapper.class);
int i = mapper.batchInsert(Lists.newArrayList(user));
assert i > 0;
}
}
测试代码结构如下
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="soul.dao.org.user.support.UserMapper">
<resultMap id="BaseResultMap" type="soul.dao.org.user.User">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
</resultMap>
<!-- 省略方法 -->
</mapper>
ExUserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="soul.dao.org.user.support.ExUserMapper">
</mapper>
UserMapper.java
public interface UserMapper {
int countByExample(UserExample example);
}
ExUserMapper.java
public interface ExUserMapper extends UserMapper{
int batchInsert(List<User> newlyIncreasedUsers);
}
需要注意的点
Ex扩展类在测试的时候,如果不需要父类的ResultMap字段映射(不需要对象返回的),可以不配置,如果需要ResultMap字段映射,则需要把Ex扩展类和父类都写在配置文件中(为了能找到Ex扩展类使用的字段映射关系,如不配置:会提示找不到 resultMap),Ex扩展类如有自己的ResultMap,也可不配置父Mapper.xml
|