
*** 作。具体的步骤如下:
获取 MyBatis 中的 MappedStatement 对象。可以通过 SqlSession 的 getConfiguration() 方法获取 Configuration 对象,然后再通过 Configuration 对象的 getMappedStatement() 方法获取 MappedStatement 对象。
从 MappedStatement 对象中获取 BoundSql 对象,即 SQL 语句绑定的参数对象。
从 BoundSql 对象中获取 SQL 语句字符串。可以通过调用 getSql() 方法获取 SQL 语句字符串。
对 SQL 语句进行相应的 *** 作。例如,可以对 SQL 语句进行修改、输出等 *** 作。
Java 通过反射获取 MyBatis 中的 SQL 语句的代码示例:
SqlSession sqlSession = sqlSessionFactoryopenSession();
try {
// 获取 MappedStatement 对象
MappedStatement mappedStatement = sqlSessiongetConfiguration()getMappedStatement("comexamplemapperselectUser");
// 获取 BoundSql 对象
BoundSql boundSql = mappedStatementgetBoundSql(paramObject);
// 获取 SQL 语句字符串
String sql = boundSqlgetSql();
// 对 SQL 语句进行相应的 *** 作
//
} finally {
sqlSessionclose();
}
需要注意的是,在使用反射获取 SQL 语句时,要注意保护用户隐私和安全,以免发生 SQL 注入等问题。
这里有if判断,生成的sql也随条件,不是固定的。
如果没有 if 生成的SQL是这样的
select uc_user_workbusy_type as busyType,count( uc_user_workbusy_type ) as num,
uc_permperm_name as permName
from uc_user_work left outer join uc_perm on uc_user_workbusy_type=uc_permperm_id
left outer join uc_work on uc_workwork_id=uc_user_workwork_id
where uc_user_workuc_id = #{ucId,jdbcType=INTEGER} and uc_workstatus!=3
and uc_workstatus!=4
and uc_user_workbind_status != #{bindStatus,jdbcType=INTEGER}
and uc_user_workstatus != #{status,jdbcType=INTEGER} 等等后面的就不写了
#{ }里面的值是你传过去的。
<where></where> 标签起始就相当于对标签里面的内容进行条件选择 相当于SQL里的 where and 。你写的这个<if></if>标签里面的and 可以去掉,应为本身就在where标签里了
sqlSessionFactorygetConfiguration()getMappedStatement("comdaoResourceDaosave")getBoundSql(null)getSql()
comdaoResourceDao为namespace
save为id
以上就是关于java通过反射拿到mybatis中的sql语句并 *** 作怎么用什么时候用全部的内容,包括:java通过反射拿到mybatis中的sql语句并 *** 作怎么用什么时候用、Java里的mapper.xml语句 求各位哥哥翻译成SQL语句 谢谢!!、请教Mybatis中如何在程序中获取Mapper中定义的SQL语句等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)