mybatis动态sql之分支选择(学习choose、when、otherwise标签)

2020-08-26 11:37:50 浏览数 (1)

EmployeeMapperDynamicSql.java

代码语言:javascript复制
package com.gong.mybatis.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;

import com.gong.mybatis.bean.Employee;

public interface EmployeeMapperDynamicSql {
    public List<Employee> getEmpByConditionIf(Employee employee);
}

EmployeeMapperDynamicSql.xml

代码语言:javascript复制
<?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.gong.mybatis.mapper.EmployeeMapperDynamicSql">
    <!-- 查询,要查那个就带上那个条件 -->
    <select id="getEmpByConditionIf" resultType="com.gong.mybatis.bean.Employee">
        select * from tbl_employee 
        <where>
            <choose>
                <when test="id!=null">
                    id=#{id}
                </when>
                <when test="lastName!=null">
                    last_name like #{lastName}
                </when>
                <when test="email!=null">
                    email=#{email}
                </when>
                <otherwise>
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>

说明:当传入了id,就会根据id来查询,传入了lastName,就根据last_name来查询,传入了email,就根据email查询。否则就执行otherwise标签里面的。

测试:

代码语言:javascript复制
package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
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 org.junit.Test;

import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

public class TestMybatis3 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapperDynamicSql mapper = openSession.getMapper(EmployeeMapperDynamicSql.class);
            Employee employee = new Employee();
//            employee.setId(3);
//            employee.setLastName("%小%");
//            employee.setEmail("xiaoming@qq.com");
            List<Employee> es = mapper.getEmpByConditionIf(employee);
            for(Employee e:es) {
                System.err.println(e);
            }
            openSession.commit();
        } finally {
            openSession.close();
        }
        
    }
    
}

首先根据id=3进行查询:

代码语言:javascript复制
DEBUG 01-21 13:45:17,288 ==>  Preparing: select * from tbl_employee WHERE id=?   (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:45:17,339 ==> Parameters: 3(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:45:17,410 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]

根据lastName="%小%"进行模糊查询:

代码语言:javascript复制
DEBUG 01-21 13:46:04,385 ==>  Preparing: select * from tbl_employee WHERE last_name like ?   (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:46:04,417 ==> Parameters: %小%(String)  (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:46:04,502 <==      Total: 2  (BaseJdbcLogger.java:145) 
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]
Employee [id=4, lastName=小明, gender=0, email=xiaoming@qq.com, dept=null]

根据email=xiximayou@qq.com进行查询:

代码语言:javascript复制
DEBUG 01-21 13:46:43,284 ==>  Preparing: select * from tbl_employee WHERE email=?   (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:46:43,347 ==> Parameters: xiximayou@qq.com(String)  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=xiximayou, gender=1, email=xiximayou@qq.com, dept=null]
DEBUG 01-21 13:46:43,394 <==      Total: 1  (BaseJdbcLogger.java:145) 

根据id=3,lastName=%小%进行查询:

代码语言:javascript复制
DEBUG 01-21 13:47:30,447 ==>  Preparing: select * from tbl_employee WHERE id=?   (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:47:30,522 ==> Parameters: 3(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-21 13:47:30,553 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]

不输入任何参数,默认查询:

代码语言:javascript复制
DEBUG 01-21 14:18:25,159 ==>  Preparing: select * from tbl_employee   (BaseJdbcLogger.java:145) 
DEBUG 01-21 14:18:25,243 ==> Parameters:   (BaseJdbcLogger.java:145) 
DEBUG 01-21 14:18:25,274 <==      Total: 4  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=xiximayou, gender=1, email=xiximayou@qq.com, dept=null]
Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null]
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]
Employee [id=4, lastName=小明, gender=0, email=xiaoming@qq.com, dept=null]

此时就会查询出所有的数据。

0 人点赞