SSM框架之MyBatis模糊查询动态SQL(详细使用案例)

14 篇文章 0 订阅
3 篇文章 0 订阅

目录

一、什么是动态SQL

 二、动态SQL用法

 三、模糊查询


一、什么是动态SQL

sql语句根据条件而发生改变。

 

 二、动态SQL用法

以题为例

CREATE TABLE `student` ( `s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `s_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '', `s_birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '', `s_sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '', PRIMARY KEY (`s_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

 测试类

import com.exy.dao.StudentDao;
import com.exy.entity.Student;
import com.github.pagehelper.PageHelper;
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.Before;
import org.junit.Test;

import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: mybatis07
 * @description:
 * @author: jdy
 * @create: 2021-12-02 22:09
 **/

public class STest {
    private SqlSession session;
    @Before
    public void before() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        session=sessionFactory.openSession();
    }

    @Test
    public void test() throws Exception{
        StudentDao studentDao = session.getMapper(StudentDao.class);
        PageHelper.startPage(0,2);
        List<Student> list = studentDao.selectAll();
        System.out.println(list);
    }
    @Test
    public void test01() throws Exception{
        StudentDao studentDao = session.getMapper(StudentDao.class);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name","");
        map.put("sex","女");
        List<Student> list = studentDao.selectna(map);
        System.out.println(list);
    }
    @Test
    public void test02() throws Exception{
        StudentDao studentDao = session.getMapper(StudentDao.class);
        Student student = new Student(01,"赵雷","1999","女");
        int list = studentDao.update(student);
        session.commit();
        System.out.println(list);
    }
    @Test
    public void test03() throws Exception{
        StudentDao studentDao = session.getMapper(StudentDao.class);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name","赵雷");
        map.put("sex","女");
        List<Student> list = studentDao.selectns(map);
        System.out.println(list);
    }
    @Test
    public void test04(){
        StudentDao studentDao = session.getMapper(StudentDao.class);
        int [] ids ={01,02,03};
        List<Student> list = studentDao.selectid(ids);
        System.out.println(list);
    }
    @Test
    public void test05() throws Exception{
        StudentDao studentDao = session.getMapper(StudentDao.class);
        List<Student> list = studentDao.selectlike("雷");
        System.out.println(list);
    }
    
}

 

 第一题

 

 

 第二题

 第三题

第四题

 代码块

<?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.exy.dao.StudentDao">
    <select id="selectAll" resultType="com.exy.entity.Student">
        select * from student
    </select>


    <select id="selectlike" resultType="com.exy.entity.Student">
        select * from student where s_name like concat('%',#{s_name},'%')
    </select>

    <select id="selectna" resultType="com.exy.entity.Student">
        select * from student
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    and s_name=#{name}
                </when>
                <when test="sex!=null and sex!=''">
                    and s_sex=#{sex}
                </when>
                <otherwise>

                </otherwise>
            </choose>
        </where>

    </select>

    <update id="update">
        update student
        <set>
            <if test="s_name!=null and s_name!=''">
                s_name=#{s_name},
            </if>
            <if test="s_birth!=null and s_birth!=''">
                s_birth=#{s_birth},
            </if>
            <if test="s_sex!=null and s_sex!=''">
                s_sex=#{s_sex},
            </if>

        </set>
        where s_id=#{s_id}
    </update>


    <select id="selectns" resultType="com.exy.entity.Student">
        select * from student
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    and s_name=#{name}
                </when>
                <when test="sex!=null and sex!=''">
                    and s_sex=#{sex}
                </when>
            </choose>
        </where>

    </select>

    <select id="selectid"  resultType="com.exy.entity.Student">
        select * from student where s_id in
        <foreach collection="ids" item="s_id" open="(" close=")" separator=",">
            #{s_id}
        </foreach>
    </select>



</mapper>

 三、模糊查询

根据部分值得出全部like和%配合 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值