JAVA全系列 教程
3762个小节阅读:7095.4k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
MyBatis注解开发中有两种方式构建动态Sql:
将Sql嵌套在<script>
内即可使用动态Sql标签:
xxxxxxxxxx
// 根据任意条件查询
@Select("<script>" +
" select * from user\n" +
" <where>\n" +
" <if test=\"username != null and username.length() != 0\">\n" +
" username like #{username}\n" +
" </if>\n" +
" <if test=\"sex != null and sex.length() != 0\">\n" +
" and sex = #{sex}\n" +
" </if>\n" +
" <if test=\"address != null and address.length() != 0\">\n" +
" and address = #{address}\n" +
" </if>\n" +
" </where>" +
"</script>")
List<User> findByCondition(User user);
在MyBatis中有@SelectProvider
、@UpdateProvider
、@DeleteProvider
、@InsertProvider
注解。当使用这些注解时将不在注解中直接编写SQL,而是调用某个类的方法来生成SQL。
xxxxxxxxxx
// 生成根据任意条件查询的Sql语句
public String findByConditionSql(User user){
StringBuffer sb = new StringBuffer("select * from user where 1=1 ");
if (user.getUsername() != null && user.getUsername().length() != 0){
sb.append(" and username like #{username} ");
}
if (user.getSex() != null && user.getSex().length() != 0){
sb.append(" and sex = #{sex} ");
}
if (user.getAddress() != null && user.getAddress().length() != 0){
sb.append(" and address = #{address} ");
}
return sb.toString();
}
1.在MyBatis中,使用注解定义Sql时想使用动态Sql标签,需要使用嵌套Sql语句
A <SelectKey>
B <provider>
C <sql>
D <script>
2.在MyBatis中,使用注解可以调用某个类的方法生成Sql
A @SelectProvider
B @Select
C @SelectKey
D @SelectOne
1=>D 2=>A