Java基础入门day73

day73

mybatis

动态SQL

简介

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

对于以上的页面,上面有四个输入框进行对应的搜索,可能存在的场景是用户什么条件都不输入,则查询所有

用户输入了一个条件,有四种场景

用户输入了连个条件,有六种场景

用户输入了三个条件,有四种场景

用户输入了四个条件,有一种场景

所以所有场景加起来有16种分支,这还仅仅只是四个输入框,当页面存在40,甚至400个输入框,条件肯定更多,SQL语句编写起来,其分支条件非常众多

也可以活用“where 1 = 1”,让场景分支变得稍微少一些

mybatis动态SQL可以很好的解决这个问题

实现
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.saas.dao.IProductDao">
​
​
 <sql id="allPro">
     select * from products
 </sql>
​
 <select id="getAllProducts" resultType="abc">
     <include refid="allPro"/>
 </select>
​
 <select id="getProductByIf" resultType="abc">
     <include refid="allPro"/>
     where
     <if test="title != null">
         title = #{title}
     </if>
     <if test="cdesc != null">
         and cdesc = #{cdesc}
     </if>
 </select>
​
 <select id="getProductByWhere" resultType="abc">
     <include refid="allPro"/>
     <where>
         <if test="title != null">
             title = #{title}
         </if>
         <if test="cdesc != null">
            or cdesc = #{cdesc}
         </if>
     </where>
 </select>
​
 <select id="getProductByForEach" resultType="abc">
     <include refid="allPro"/>
     <where>
         cid in
         <foreach collection="ids" item="id" index="index" separator="," open="(" close=")">
             #{id}
         </foreach>
     </where>
 </select>
​
</mapper>
package com.saas.dao;
​
import com.saas.pojo.Product;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.SqlSession;
import com.saas.util.SessionUtil;
import org.junit.Test;
​
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
​
public class TestDynamicSQL {
​
 private SqlSession session = null;
​
 @Test
 public void testGetAllProducts(){
     session = SessionUtil.getSession();
​
     List<Product> list = session.selectList("com.saas.dao.IProductDao.getAllProducts");
​
     for (Product p : list) {
         System.out.println(p);
     }
​
     SessionUtil.closeSession(session);
 }
​
 @Test
 public void testGetProductByIf(){
     session = SessionUtil.getSession();
​
     Map<String, Object> map = new HashMap<>();
​
//        map.put("title", "小米10 Pro");
     map.put("cdesc", "骁龙845处理器,AI变焦双摄,红外人脸解锁,AI变焦双摄,红外人脸解锁");
​
     List<Product> list = session.selectList("com.saas.dao.IProductDao.getProductByIf", map);
​
     for (Product p : list) {
         System.out.println(p);
     }
​
     SessionUtil.closeSession(session);
 }
​
 @Test
 public void testGetProductByWhere(){
     session = SessionUtil.getSession();
​
     Map<String, Object> map = new HashMap<>();
​
     map.put("title", "小米10 Pro");
     map.put("cdesc", "骁龙845处理器,AI变焦双摄,红外人脸解锁,AI变焦双摄,红外人脸解锁");
​
     List<Product> list = session.selectList("com.saas.dao.IProductDao.getProductByWhere", map);
​
     for (Product p : list) {
         System.out.println(p);
     }
​
     SessionUtil.closeSession(session);
 }
​
 @Test
 public void testGetProductByForEach(){
     session = SessionUtil.getSession();
​
     Map<String, Object> map = new HashMap<>();
​
     List<Integer> ids = new ArrayList<>();
     ids.add(1);
     ids.add(3);
     ids.add(4);
     ids.add(44);
​
     map.put("ids", ids);
​
     List<Product> list = session.selectList("com.saas.dao.IProductDao.getProductByForEach", map);
​
     for (Product p : list) {
         System.out.println(p);
     }
​
     SessionUtil.closeSession(session);
 }
}
  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值