[mybatis]mybatis动态sql的标签使用详解
2022/7/24 2:22:45
本文主要是介绍[mybatis]mybatis动态sql的标签使用详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
动态sql
根据不同的环境生成不同的sql
-
if
-
choose (when, otherwise)
-
trim (where, set)
-
foreach
if
- 使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分。
如果传入name就根据name查询,如果输入age就根据age查询,如果不输入就全部查询。
这里还使用了一对标签
主要作用是在判断条件时自动去掉一个或多个end,确保程序正常运行!
<select id="getPerpleByIf" parameterType="Map" resultType="pojo.perple"> select * from perple <where> <if test="name!=null" > name=#{name} </if> <if test="age!=null"> and age=#{age} </if> </where>
@Test public void test(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); prepleMapper mapper = sqlSession.getMapper(prepleMapper.class); Map map= new HashMap(); // map.put("name","tata"); map.put("age","18"); List<perple> perpleByIf = mapper.getPerpleByIf(map); for(perple perple:perpleByIf){ System.out.println(perple); } } }
choose、when、otherwise
不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
<select id="getPerpleByChoose" parameterType="Map" resultType="pojo.perple"> select * from perple <where> <choose> <when test="name!=null"> name=#{name} </when> <when test="age!=null"> age=#{age} </when> <otherwise> birth=#{birth} </otherwise> </choose> </where> </select>
@Test public void test1(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); prepleMapper mapper = sqlSession.getMapper(prepleMapper.class); Map map= new HashMap(); // map.put("name","tata"); map.put("age",13); map.put("birth","2022-1"); List<perple> perpleByChoose = mapper.getPerpleByChoose(map); for(perple perple:perpleByChoose){ System.out.println(perple); } }
trim、where、set
set跟where标签作用相似,是为了在修改的时候根据传递的参数去掉多余的逗号!
<update id="updateSet" parameterType="Map"> update perple <set> <if test="id!=null"> id=#{id}, </if> <if test="name!=null"> name=#{name}, </if> <if test="age!=null"> age=#{age} , </if> <if test="birth!=null"> birth=#{birth}, </if> </set> where id=#{id} </update>
@Test public void test2(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); prepleMapper mapper = sqlSession.getMapper(prepleMapper.class); Map map= new HashMap(); // map.put("name","tata"); map.put("age",22); map.put("id",3); // map.put("birth","2022-1"); mapper.updateSet(map); sqlSession.commit(); }
sql脚本抽取公共部分进行复用
1.使用sql标签进行抽取
<sql id="if"> <if test="name!=null"> name=#{name} </if> <if test="age!=null"> and age=#{age} </if> </sql>
2.使用include插入
<select id="getPerple" parameterType="String" resultType="perple"> select * from perple <where> <include refid="if"></include> </where> </select>
foreach
动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)。
sql语句
<select id="getPerpleByForeach" parameterType="Map" resultType="pojo.perple"> select * from perple <where> <foreach collection="ids" item="id" open="and (" close= ")" separator="or"> id=#{id} </foreach> </where> </select>
测试类
@Test public void test3(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); prepleMapper mapper = sqlSession.getMapper(prepleMapper.class); Map map= new HashMap(); ArrayList<Integer> ids = new ArrayList<Integer>(); ids.add(2); ids.add(3); map.put("ids",ids); List<perple> foreach = mapper.getPerpleByForeach(map); for(perple f:foreach){ System.out.println(f); } }
动态sql测试的所有接口方法
//通过if语句的查询 List<perple> getPerpleByIf(Map map); //通过choose语句的查询 List<perple> getPerpleByChoose(Map map); //通过set标签更新 int updateSet(Map map); //通过foreach查询123号 List<perple> getPerpleByForeach(Map map);
这篇关于[mybatis]mybatis动态sql的标签使用详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?