zoukankan      html  css  js  c++  java
  • Mybatis学习笔记12

    choose (when, otherwise):分支选择;带了break的swtich-case

    示例代码:

    接口定义:
    package com.mybatis.dao;
    
    import com.mybatis.bean.Employee;
    
    import java.util.List;
    
    public interface EmployeeMapper {
        public List<Employee> getEmpsByConditionChoose(Employee employee);
    }
    
    
    mapper定义:
    <?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.mybatis.dao.EmployeeMapper">
        <!--
            choose (when, otherwise):分支选择;带了break的swtich-case
    	    如果带了id就用id查,如果带了lastName就用lastName查,只会进入其中一个
    	 -->
        <select id="getEmpsByConditionChoose" resultType="com.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>
                        gender=0
                    </otherwise>
                </choose>
            </where>
        </select>
    </mapper>
    
    测试代码:
    package com.mybatis.demo;
    
    import com.mybatis.bean.Employee;
    import com.mybatis.dao.EmployeeMapper;
    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 java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    public class MyTest {
        public SqlSessionFactory getSqlSessionFactory() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            return new SqlSessionFactoryBuilder().build(inputStream);
        }
    
        @Test
        public void test() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession(true);
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                List<Employee> emps = mapper.getEmpsByConditionChoose(new Employee(1, "%t%", null, null));
                for (Employee emp : emps) {
                    System.out.println(emp);
                }
            } finally {
                openSession.close();
            }
        }
    }
    
  • 相关阅读:
    [原]如何在Android用FFmpeg+SDL2.0之同步视频
    鹤山市五泉酒厂 (“侨乡情”酒 )
    买酒的网站(转)
    泥鳅、黄鳝有关技术。
    国产 冰葡萄酒 主要生产地、品牌
    对症治疗过敏性鼻炎,依巴斯汀比氯雷他定更有效
    魔筎精粉制作 ----新化联系人
    拉肚子使用的常见药物
    一些WCF DS 的资料(参考)
    SHAREPOINT 2013 + PROJECT 2013 资料网站
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10351630.html
Copyright © 2011-2022 走看看