您好,欢迎访问一九零五行业门户网

一起聊聊MySQL动态SQL拼接

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于动态sql拼接的相关内容,实际业务开发中,我们的sql语句通常是动态拼接而成的,比如条件搜索功能的sql语句等等,下面一起来看一下,希望对大家有帮助。
推荐学习:mysql视频教程
一、动态sql拼接目标能够使用mybatis的标签实现动态sql拼接分析我们在前边的学习过程中,使用的sql语句都非常简单。而在实际业务开发中,我们的sql语句通常是动态拼接而成的,比如:条件搜索功能的sql语句。
# 提供了一个功能:用户可以在页面上根据username、sex、address进行搜索# 用户输入的搜索条件:可以是一个条件,也可能是两个、三个# 只输入一个条件:姓名是王select * from user where username like '%王%'# 只输入一个条件:性别是“男”select * from user where sex = '男'# 输入两个条件:姓名“王”,性别“男”select * from user where username like '%王%' and sex = '男'# 输入三个条件:姓名“王”,性别“男”,地址“北京”select * from user where username like '%王%' and sex = '男' and address like '%北京%';
在mybatis中,sql语句是写在映射配置的xml文件中的。mybatis提供了一些xml的标签,用来实现动态sql的拼接。
常用的标签有:
<if></if>:用来进行判断,相当于java里的if判断<where></where>:通常和if配合,用来代替sql语句中的where 1=1<foreach></foreach>:用来遍历一个集合,把集合里的内容拼接到sql语句中。例如拼接:in (value1, value2, ...)<sql></sql>:用于定义sql片段,达到重复使用的目的讲解1. 准备mybatis环境创建java项目,导入jar包;准备javabean
创建映射器接口userdao
创建映射配置文件userdao.xml
创建全局配置文件sqlmapconfig.xml
创建日志配置文件log4j.properties
2. <if>标签:语法介绍<if test="判断条件,使用ognl表达式进行判断"> sql语句内容, 如果判断为true,这里的sql语句就会进行拼接</if>
使用示例根据用户的名称和性别搜索用户信息。把搜索条件放到user对象里,传递给sql语句
映射器接口userdao上加方法
package com.demo.dao;import com.demo.domain.user;import java.util.list;public interface userdao {    /**     * 根据username和sex搜索用户     * @param user 封装了搜索条件的user对象     * @return 搜索的结果     */    list<user> search1(user user);}
映射文件userdao.xml里配置statement
<?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.itheima.dao.userdao">    <!-- if标签:用于条件判断 语法:<if test="用ognl表达式判断"> 如果判断为true,这里的内容会拼接上去 </if>        注意:标签里写ognl表达式,不要再加#{}、${}        常用的ognl表达式:            比较:>, <, >=, <=, ==, != 或者 gt, lt, gte, lte, eq, neq 逻辑:&&,||,! 或者 and, or, not 调用方法:username.length(), list.size() -->    <select id="search1" resulttype="user">        select * from user where 1=1        <if test="username != null and username.length()>0>            and username like %#{username}%        </if>        <if test="sex != null and sex.length()>0>            and sex = #{sex}        </if>    </select></mapper>
功能测试,在测试类里加测试方法
package com.demo;import com.demo.dao.userdao;import com.demo.domain.user;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 org.junit.after;import org.junit.before;import org.junit.test;import java.io.ioexception;import java.io.inputstream;import java.util.list;public class sqltest {    private userdao userdao;    private sqlsession session;    private inputstream is;    /**     * 要求:根据username和sex搜索用户     *      搜索条件放到user对象里     */    @test    public void testsearch(){        user user = new user();        // user.setusername(王);        // user.setsex(男);        list<user> userlist = userdao.search1(user);        userlist.foreach(system.out::println);    }    @before    public void init() throws ioexception {        //1. 读取全局配置文件        is = resources.getresourceasstream(sqlmapconfig.xml);        //2. 得到一个sqlsession对象        sqlsessionfactory factory = new sqlsessionfactorybuilder().build(is);        session = factory.opensession();        userdao = session.getmapper(userdao.class);    }    @after    public void destroy() throws ioexception {        session.close();        is.close();    }}
3. <where>标签语法介绍在刚刚的练习的sql语句中,我们写了where 1=1。如果不写的话,sql语句会出现语法错误。mybatis提供了一种代替where 1=1的技术:<where></where>标签。
代码示例把上一章节的实现代码进行优化,使用<where></where>标签代替where 1=1
映射器userdao的search1方法:已有,不用修改
/** * 根据username和sex搜索用户 * @param user 封装了搜索条件的user对象 * @return 搜索的结果 */list<user> search1(user user);
在映射文件userdao.xml里修改sql语句
<!-- where标签:让mybatis帮我们生成一个where关键字 mybatis会智能判断: 如果一个条件都没有,就不生成where关键字 如果有条件,会判断是否有多余的and关键字,把多余的and去掉 注意:建议把所有的where条件都放到where标签里边 --><select id="search1" resulttype="user">    select * from user    <where>        <if test="username != null and username.length()>0>            and username like %#{username}%        </if>        <if test="sex != null and sex.length()>0>            and sex = #{sex}        </if>    </where></select>
在测试类里进行功能测试:测试方法不需要修改
@testpublic void testsearch(){    user user = new user();    // user.setusername(王);    // user.setsex(男);    list<user> userlist = userdao.search1(user);    userlist.foreach(system.out::println);}
4. <foreach>标签语法介绍foreach标签,通常用于循环遍历一个集合,把集合的内容拼接到sql语句中。例如,我们要根据多个id查询用户信息,sql语句:
select * from user where id = 1 or id = 2 or id = 3;select * from user where id in (1, 2, 3);
假如我们传参了id的集合,那么在映射文件中,如何遍历集合拼接sql语句呢?可以使用foreach标签实现。
<!--foreach标签: 属性: collection:被循环遍历的对象,使用ognl表达式获取,注意不要加#{} open:循环之前,拼接的sql语句的开始部分 item:定义变量名,代表被循环遍历中每个元素,生成的变量名 separator:分隔符 close:循环之后,拼接sql语句的结束部分 标签体: 使用#{ognl}表达式,获取到被循环遍历对象中的每个元素--><foreach collection="" open="id in(" item="id" separator="," close=")">    #{id}</foreach>
使用示例有搜索条件类queryvo如下:
package com.itheima.domain;public class queryvo {    private integer[] ids;    public integer[] getids() {        return ids;    }    public void setids(integer[] ids) {        this.ids = ids;    }}
在映射器userdao里加方法
/**     * queryvo里有一个integer[] ids     * 要求:根据ids查询对应的用户列表     */list<user> search2(queryvo vo);
在映射文件userdao.xml里配置statement
    <!-- foreach标签:用于循环遍历 collection:被循环的集合/数组 item:定义一个变量 separator:定义拼接时的分隔符 open:拼接字符串时的开始部分 close:拼接字符串时的结束部分 相当于 for(integer id: ids){} select * from user where id in(41, 42, 45) -->    <select id="search2" resulttype="user">        <!--select * from user where id in(41, 42, 45)-->        select * from user where        <foreach collection="ids" open="id in(" item="id" separator="," close=")">            #{id}        </foreach>    </select>
功能测试
    @test    public void testsearch2(){        queryvo vo = new queryvo();        vo.setids(new integer[]{41,42,43,44,45});        list<user> userlist = userdao.search2(vo);        userlist.foreach(system.out::println);    }
5. <sql>标签在映射文件中,我们发现有很多sql片段是重复的,比如:select * from user。mybatis提供了一个<sql>标签,把重复的sql片段抽取出来,可以重复使用。
语法介绍在映射文件中定义sql片段:
<sql id="唯一标识">sql语句片段</sql>
在映射文件中引用sql片段:
<include refid="sql片段的id"></include>
使用示例在查询用户的sql中,需要重复编写:select * from user。把这部分sql提取成sql片段以重复使用
要求:queryvo里有ids,user对象。根据条件进行搜索修改queryvo,增加成员变量user
package com.itheima.domain;/** * @author liuyp * @date 2021/09/07 */public class queryvo {    private integer[] ids;    private user user;    //get/set方法……}
在映射器userdao里加方法
    /**     * 动态sql拼接的综合应用:if、where、foreach     * 要求:queryvo里有ids、username、sex值,根据这些值进行搜索     */    list<user> search3(queryvo vo);
在映射文件userdao.xml里配置statement
<select id="search3" resulttype="user">    <!--select * from user-->    <include refid="seluser"/>    <where>        <if test="ids != null and ids.length > 0>            <foreach collection="ids" open="and id in(" item="id" separator="," close=")">                #{id}            </foreach>        </if>        <!--<if test="user != null">                <if test="user.username != null and user.username.length() > 0>                    and username like %#{user.username}%                </if>                <if test="user.sex != null and user.sex.length() > 0>                    and sex = #{user.sex}                </if>            </if>-->        <include refid="usercondition"/>    </where></select><!-- sql标签:用于定义一个sql片段 include标签:什么时候要引用某个sql片段,就使用include标签 注意:引入sql片段之后,最终的sql语句必须要完全符合语法 --><sql id="seluser">select * from user</sql><sql id="usercondition">    <if test="user != null">        <if test="user.username != null and user.username.length() > 0>            and username like %#{user.username}%        </if>        <if test="user.sex != null and user.sex.length() > 0>            and sex = #{user.sex}        </if>    </if></sql>
在测试类里加测试方法
    @test    public void testsearch3(){        queryvo vo = new queryvo();        vo.setids(new integer[]{41,42,43,44,45});        // user user = new user();        // user.setusername(王);        // user.setsex(男);        // vo.setuser(user);        list<user> userlist = userdao.search3(vo);        userlist.foreach(system.out::println);    }
推荐学习:mysql视频教程
以上就是一起聊聊mysql动态sql拼接的详细内容。
其它类似信息

推荐信息