dyn*_*yng 7 java h2 mybatis spring-boot spring-mybatis
我正在使用mybatis-spring 1.2.3和Spring4来创建一个Web应用程序.主数据存储是MySQL在生产环境中,但我也在内存数据库H2中进行单元测试.
MyBatis在测试和生产中都能很好地兼容MySQL和H2,但是我遇到了一个问题,有一天我需要force index(idx1)
在MySQL的查询中使用它,这将导致单元测试中的语法错误,因为H2不支持force index
.结果,单元测试完全被打破.
我想知道MyBatis有什么方法可以处理这种情况吗?(数据库类型在测试和生产方面有所不同,它们对SQL语法的支持也不尽相同.)
这是我的映射文件:
<?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="myproject.mapper.UserMapper">
<select id="getGameUsersForDate" resultType="myproject.dao.domain.GameUser">
select
*
from game_user
force index(idx1)
where
game_id in
<choose>
<when test="gameIds.size() > 0">
<foreach item="gameId" collection="gameIds" open="(" separator="," close=")">
#{gameId}
</foreach>
</when>
<otherwise>
(null)
</otherwise>
</choose>
and uid in
<choose>
<when test="uids.size() > 0">
<foreach item="uid" collection="mids" open="(" separator="," close=")">
#{mid}
</foreach>
</when>
<otherwise>
(null)
</otherwise>
</choose>
and `date` = #{date}
</select>
</mapper>
Run Code Online (Sandbox Code Playgroud)
MyBatis 提供多数据库供应商支持,允许您根据使用的数据库供应商以不同方式构建 SQL。因此,您可以将有问题的代码包装在测试中,例如:
<if test="_databaseId == 'mysql'">
force index(idx1)
</if>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2062 次 |
最近记录: |