MyBatis 3 - 从映射器获取 SQL 字符串
我只想使用MyBatis3来生成SQL字符串(使用xml映射),但是我获得的SQL无效。
示例,我获取 sql 字符串:
SELECT * FROM USER WHERE NAME = john
在此 sql 不存在字符串值 john 的 ' char 或四周
在mybatis.xml:
...
<mappers>
<mapper resource="sql1.xml"/>
</mappers>
...
sql1.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="sql1">
<select id="select1" parameterType="map" resultType="String" >
SELECT * FROM USERS
WHERE
name LIKE ${name} AND num = ${number}
</select>
</mapper>
在 MyBatisSql.java:
SqlSessionFactory sessionFactory = ConnectionFactory.getSqlSessionFactory();
Configuration configuration = sessionFactory.getConfiguration();
Map pars = new HashMap<String, Object>();
pars.put("name", "john");
pars.put("number", 1345);
MappedStatement ms = configuration.getMappedStatement("sql1.select1);
BoundSql boundSql = ms.getBoundSql(params);
String sql = boundSql.getSql();
System.out.println(sql);
结果是
SELECT * FROM USERS
WHERE
name LIKE john AND num = 12345
在这个SQL中,字符串john没有被' char包围,所以它不是一个有效的SQL(我的目的只是使用myBatis生成有效的SQL字符串)。我希望:
SELECT * FROM USERS
WHERE
name LIKE 'john' AND num = 12345
谢谢