我可以使用 MyBatis 生成动态 SQL 而不执行它吗?
2022-09-03 01:44:09
我有一些复杂的查询要用许多可选的过滤器来构建,对于这些过滤器,MyBatis似乎是生成动态SQL的理想候选者。
但是,我仍然希望我的查询在与应用程序的其余部分相同的框架中执行(不使用MyBatis)。
因此,我希望做的是严格使用MyBatis来生成SQL,但从那里使用我应用程序的其余部分来实际执行它。这可能吗?如果是这样,如何?
我有一些复杂的查询要用许多可选的过滤器来构建,对于这些过滤器,MyBatis似乎是生成动态SQL的理想候选者。
但是,我仍然希望我的查询在与应用程序的其余部分相同的框架中执行(不使用MyBatis)。
因此,我希望做的是严格使用MyBatis来生成SQL,但从那里使用我应用程序的其余部分来实际执行它。这可能吗?如果是这样,如何?
尽管 MyBatis 被设计为在构建查询后执行查询,但您可以利用它的配置和一些“内部知识”来获得所需的内容。
MyBatis是一个非常好的框架,不幸的是,它在文档方面缺乏,所以源代码是你的朋友。如果你四处挖掘,你应该碰到这些类:以及哪些是构建动态SQL的关键参与者。下面是一个基本的用法示例:org.apache.ibatis.mapping.MappedStatement
org.apache.ibatis.mapping.BoundSql
包含此数据的 MySQL 表:user
name login
----- -----
Andy a
Barry b
Cris c
User
类:
package pack.test;
public class User {
private String name;
private String login;
// getters and setters ommited
}
UserService
接口:
package pack.test;
public interface UserService {
// using a different sort of parameter to show some dynamic SQL
public User getUser(int loginNumber);
}
UserService.xml
映射器文件:
<mapper namespace="pack.test.UserService">
<select id="getUser" resultType="pack.test.User" parameterType="int">
<!-- dynamic change of parameter from int index to login string -->
select * from user where login = <choose>
<when test="_parameter == 1">'a'</when>
<when test="_parameter == 2">'b'</when>
<otherwise>'c'</otherwise>
</choose>
</select>
</mapper>
sqlmap-config.file
:
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false" />
</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/test"/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="pack/test/UserService.xml"/>
</mappers>
</configuration>
AppTester
以显示结果:
package pack.test;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class AppTester {
private static String CONFIGURATION_FILE = "sqlmap-config.xml";
public static void main(String[] args) throws Exception {
Reader reader = null;
SqlSession session = null;
try {
reader = Resources.getResourceAsReader(CONFIGURATION_FILE);
session = new SqlSessionFactoryBuilder().build(reader).openSession();
UserService userService = session.getMapper(UserService.class);
// three users retreived from index
for (int i = 1; i <= 3; i++) {
User user = userService.getUser(i);
System.out.println("Retreived user: " + user.getName() + " " + user.getLogin());
// must mimic the internal statement key for the mapper and method you are calling
MappedStatement ms = session.getConfiguration().getMappedStatement(UserService.class.getName() + ".getUser");
BoundSql boundSql = ms.getBoundSql(i); // parameter for the SQL statement
System.out.println("SQL used: " + boundSql.getSql());
System.out.println();
}
} finally {
if (reader != null) {
reader.close();
}
if (session != null) {
session.close();
}
}
}
}
结果:
Retreived user: Andy a
SQL used: select * from user where login = 'a'
Retreived user: Barry b
SQL used: select * from user where login = 'b'
Retreived user: Cris c
SQL used: select * from user where login = 'c'
每个人都知道如何使用BoundSql.getSql()从MyBatis获取参数化的查询字符串,如下所示:
// get parameterized query
MappedStatement ms = configuration.getMappedStatement("MyMappedStatementId");
BoundSql boundSql = ms.getBoundSql(parameters);
System.out.println("SQL" + boundSql.getSql());
// SELECT species FROM animal WHERE name IN (?, ?) or id = ?
但现在你需要等式的另一半,与问号对应的值列表:
// get parameters
List<ParameterMapping> boundParams = boundSql.getParameterMappings();
String paramString = "";
for(ParameterMapping param : boundParams) {
paramString += boundSql.getAdditionalParameter(param.getProperty()) + ";";
}
System.out.println("params:" + paramString);
// "Spot;Fluffy;42;"
现在,您可以序列化它以发送到其他位置以运行,也可以将其打印到日志中,以便将它们拼接在一起并手动运行查询。
*代码未测试,可能是次要类型问题等