关于后端:Mybatis系列教材-八-动态SQL-mybatis-where-set-trim-标签例子

9次阅读

共计 6552 个字符,预计需要花费 17 分钟才能阅读完成。


步骤 1: 先运行,看到成果,再学习
步骤 2: 模拟和排错
步骤 3: 基于上一个知识点进行
步骤 4: 多条件的矛盾
步骤 5:where 标签
步骤 6:set 标签
步骤 7:trim 标签
步骤 8: 可运行我的项目下载

步骤 1 : 先运行,看到成果,再学习

老规矩,先下载下载区 (点击进入) 的可运行我的项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的成果。

步骤 2 : 模拟和排错

在确保可运行我的项目可能正确无误地运行之后,再严格照着教程的步骤,对代码模拟一遍。
模拟过程不免代码有出入,导致无奈失去冀望的运行后果,此时此刻通过比拟 正确答案 (可运行我的项目) 和本人的代码,来定位问题所在。
采纳这种形式,学习有成果,排错有效率,能够较为显著地晋升学习速度,跨过学习路上的各个槛。

举荐应用 diffmerge 软件,进行文件夹比拟。把你本人做的我的项目文件夹,和我的可运行我的项目文件夹进行比拟。
这个软件很牛逼的,能够晓得文件夹里哪两个文件不对,并且很显著地标记进去
这里提供了绿色装置和应用教程:diffmerge 下载和应用教程

步骤 3 : 基于上一个知识点进行

基于上一个知识点 if 标签进行

步骤 4 : 多条件的矛盾

如果要进行多条件判断,就会写成这样:

<select id="listProduct" resultType="Product">
    select * from product_
    <if test="name!=null">
        where name like concat('%',#{name},'%')
    </if>
    <if test="price!=0">
        and price > #{price}
    </if>
</select>

这么写的问题是:当没有 name 参数,却有 price 参数的时候,执行的 sql 语句就会是:

select * from product_ and price > 10.

这样执行就会报错

  • Product.xml
  • TestMybatis.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.pojo">
    <select id="listProduct" resultType="Product">
        select * from product_
        <if test="name!=null">
            where name like concat('%',#{name},'%')
        </if>
        <if test="price!=0">
            and price > #{price}
        </if>
    </select>
</mapper>

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.how2java.pojo.Product;

public class TestMybatis {public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        System.out.println("多条件查问");
        Map<String,Object> params = new HashMap<>();
        params.put("name","a");
        params.put("price","10");
        List<Product> ps2 = session.selectList("listProduct",params);
        for (Product p : ps2) {System.out.println(p);
        }
        session.commit();
        session.close();}
}

步骤 5 : where 标签

这个问题能够通过 <where> 标签来解决,如代码所示

<select id="listProduct" resultType="Product">
    select * from product_
    <where>
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>
        <if test="price!=null and price!=0">
            and price > #{price}
        </if>
    </where>
</select>

<where> 标签会进行主动判断
如果任何条件都不成立,那么就在 sql 语句里就不会呈现 where 关键字
如果有任何条件成立,会主动去掉多进去的 and 或者 or。

所以在测试代码里

Map<String,Object> params = new HashMap<>();
//params.put("name","a");
params.put("price","10");

这个参数 map,无论是否提供值否都能够失常执行

  • Product.xml
  • TestMybatis.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.pojo">
    <select id="listProduct" resultType="Product">
        select * from product_
        <where>
            <if test="name!=null">
                and name like concat('%',#{name},'%')
            </if>
            <if test="price!=null and price!=0">
                and price > #{price}
            </if>
        </where>
    </select>
</mapper>
package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.how2java.pojo.Product;

public class TestMybatis {public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        System.out.println("多条件查问");
        Map<String,Object> params = new HashMap<>();
//        params.put("name","a");
        params.put("price","10");
        List<Product> ps2 = session.selectList("listProduct",params);
        for (Product p : ps2) {System.out.println(p);
        }
        session.commit();
        session.close();}
}

步骤 6 : set 标签

与 where 标签相似的,在 update 语句里也会碰到多个字段相干的问题。在这种状况下,就能够应用 set 标签:

<set>
    <if test="name != null">name=#{name},</if>
    <if test="price != null">price=#{price}</if>
</set>

其成果与 where 标签相似,有数据的时候才进行设置。

  • Product.xml
  • TestMybatis.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.pojo">
    <select id="listProduct" resultType="Product">
        select * from product_
        <where>
            <if test="name!=null">
                and name like concat('%',#{name},'%')
            </if>
            <if test="price!=null and price!=0">
                and price > #{price}
            </if>
        </where>
    </select>
    <update id="updateProduct" parameterType="Product" >
        update product_
        <set>
            <if test="name != null">name=#{name},</if>
            <if test="price != null">price=#{price}</if>

        </set>
        where id=#{id}
    </update>
</mapper>
package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.how2java.pojo.Product;

public class TestMybatis {public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        Product p = new Product();
        p.setId(6);
        p.setName("product zz");
        p.setPrice(99.99f);
        session.update("updateProduct",p);
        listAll(session);
        session.commit();
        session.close();}
    private static void listAll(SqlSession session) {Map<String,Object> params = new HashMap<>();
//        params.put("name","a");
//        params.put("price","10");
        List<Product> ps2 = session.selectList("listProduct",params);
        for (Product p : ps2) {System.out.println(p);
        }
    }
}

步骤 7 : trim 标签

trim 用来定制想要的性能,比方 where 标签就能够用

<trim prefix="WHERE" prefixOverrides="AND |OR">
    ...
</trim>

来替换

set 标签就能够用

<trim prefix="SET" suffixOverrides=",">
    ...
</trim>

来替换
运行 set 标签中的代码,其成果是一样的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.pojo">
    <select id="listProduct" resultType="Product">
        select * from product_
        <trim prefix="WHERE" prefixOverrides="AND |OR">
            <if test="name!=null">
                and name like concat('%',#{name},'%')
            </if>
            <if test="price!=null and price!=0">
                and price > #{price}
            </if>
        </trim>
    </select>
    <update id="updateProduct" parameterType="Product" >
        update product_
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name=#{name},</if>
            <if test="price != null">price=#{price}</if>

        </trim>
        where id=#{id}
    </update>
</mapper>

步骤 8 : 可运行我的项目下载

在下载区 (点击进入) 有本知识点对应的可运行我的项目下载

更多内容,点击理解:https://how2j.cn/k/mybatis/mybatis-where/1114.html

正文完
 0