[英]MyBatis how can I generate different sql for different database backend
我将mybatis-spring 1.2.3和Spring4一起使用来创建Web应用程序。 主要数据存储是生产环境中的MySQL,但我还在单元测试中使用内存数据库H2。
MyBatis在测试和生产中都可以与MySQL和H2很好地兼容,但是遇到一个问题,有一天我需要在对MySQL的查询中使用force index(idx1)
,这将导致单元测试中出现语法错误,因为H2 hasn不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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.