首页 > 其他分享 >mybatis动态标签——choose、when、otherwise

mybatis动态标签——choose、when、otherwise

时间:2023-03-03 22:14:30浏览次数:40  
标签:mapper emp list when EmpMapper sqlSession choose mybatis Emp

<?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.xxx.dynamicSQL.mapper.EmpMapper">

    <!--
        choose、when、otherwise相当于if, else if ,else
        when至少设置一个,匹配上条件后,其他的条件不执行
     otherwise最多设置一个,when中的条件都不符合时,执行该条件 --> <select id="getEmp" resultType="emp"> select * from t_emp <where> <choose> <when test="empName != null and empName != ''"> emp_name = #{empName} </when> <when test="age != null and age != ''"> age = #{age} </when> <when test="gender != null and gender != ''"> gender = #{gender} </when> <otherwise> emp_id = 1 </otherwise> </choose> </where> </select> </mapper>

测试代码1:

 @Test
    public void test1(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp(null,"",11,"男");
        List<Emp> list = mapper.getEmp(emp);

        list.forEach(System.out::println);
    }

 

 

 

测试代码2:

@Test
    public void test1(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp(null,"",null,"");
        List<Emp> list = mapper.getEmp(emp);

        list.forEach(System.out::println);
    }

 

标签:mapper,emp,list,when,EmpMapper,sqlSession,choose,mybatis,Emp
From: https://www.cnblogs.com/ixtao/p/17177147.html

相关文章

  • mybatis动态标签——trim
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><m......
  • mybatis动态标签——where、if
    mapper接口publicinterfaceEmpMapper{List<Emp>getEmp(Empemp);}mapper.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//myb......
  • EasyCode mybatis-plus模板 &Live tmpl
    Mapper##导入宏定义$!{define.vm}##设置表后缀(宏定义)#setTableSuffix("Mapper")##保存文件(宏定义)#save("/mapper","Mapper.java")##包路径(宏定义)#setPackageS......
  • java-mybatis
    1、添加依赖<dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><ver......
  • springboot整合mybatis
    1,引入依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version><scope>runtime</scope></depend......
  • MyBatis_10(分页插件)
    主题:分页插件一、分页插件使用步骤:1-添加依赖<!--https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper--><dependency> <groupId>com.github......
  • Mybatis动态SQL语句大全
    读完这篇文章里你能收获到Mybatis动态SQL语句大全Mybatis中如何定义变量Mybatis中如何提取公共的SQL片段1.If语句需求:根据作者名字和博客名字来查询博客!如果作......
  • mybatis处理一对多的映射关系
    实体类packageorg.example.entity;importjava.util.List;publicclassDept{privateIntegerdeptId;privateStringdeptName;privateList<Emp>......
  • MyBatis_09(逆向工程)
    MyBatis的逆向工程正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成......
  • mybatis懒加载
    mybatis懒加载全局配置<settings><settingname="mapUnderscoreToCamelCase"value="true"/><!--开启懒加载(开启延迟加载)--><settingn......