[mybatis]动态sql_choose_分支选择

Posted 唐火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[mybatis]动态sql_choose_分支选择相关的知识,希望对你有一定的参考价值。

choose

如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个

    public List<Employee> getEmpByConditionChoose(Employee employee);
<!--    public List<Employee> getEmpByConditionChoose(Employee employee);-->

    <select id="getEmpByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tb1_employee
        <where>
        <!--如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个-->
        <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>
             gender =0

            </otherwise>

        </choose>


        </where>


    </select>
    @Test
    public void test04() throws IOException 
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        try
        
            EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);


            Employee employee = new Employee(3,"%e%","jerry@qq.com",null);

            List<Employee> list = mapper.getEmpByConditionChoose(employee);
            for (Employee emp:list)
            
                System.out.println(emp);
            

        finally 

            sqlSession.close();

        
    

以上是关于[mybatis]动态sql_choose_分支选择的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis学习总结_11_MyBatis动态Sql语句

mybatis 动态SQL

mybatis动态sql

[刘阳Java]_MyBatis_动态SQL标签用法_第7讲

MyBatis_动态SQL

[mybatis]动态sql_内置参数_parameter&_databaseid