Mybatis——动态SQL详解

动态SQL

bean:


public class Department {

    private Integer id;
    private String departmentName;
    private List emps;    

    public Department(Integer id) {
        super();
        this.id = id;
    }
    public Department() {
        super();
        // TODO Auto-generated constructor stub
    }
    public List getEmps() {
        return emps;
    }
    public void setEmps(List emps) {
        this.emps = emps;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
    @Override
    public String toString() {
        return "Department [id=" + id + ", departmentName=" + departmentName + "]";
    }


}
public class Employee {
    private Integer id;
    private String last_name;
    private String email;
    private String gender;
    private Department department;

    public Employee(Integer id, String last_name, String email, String gender, Department department) {
        super();
        this.id = id;
        this.last_name = last_name;
        this.email = email;
        this.gender = gender;
        this.department = department;
    }
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
    public Employee(Integer id, String last_name, String email, String gender) {
        super();
        this.id = id;
        this.last_name = last_name;
        this.email = email;
        this.gender = gender;
    }
    public Employee(){

    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", last_name=" + last_name + ", email=" + email + ", gender=" + gender + "]";
    }
}

接口 EmployeeMapperDynamicSQL


public interface EmployeeMapperDynamicSQL {

    //查询员工,要求,携带了哪个字段查询条件就带上这个字段的值
    public List getEmpsByConditionIf(Employee employee);

    public List getEmpsByConditionTrim(Employee employee);

    public List getEmpsByConditionChoose(Employee employee);

    public void updateEmp(Employee employee);

    public List getEmpsByConditionForeach(@Param("ids")List ids);

    public void addEmps(@Param("emps")List emps);
}

EmployeeMapperDynamicSQL.xml



 <mapper namespace="com.mybatis.dao.EmployeeMapperDynamicSQL">
 
     
    
    <select id="getEmpsByConditionIf" resultType="com.mybatis.bean.Employee">
        select * from tbl_employee 
        <where>
        
        <if test="id!=null">
            and id = #{id}
        if>
        <if test="last_name!=null and last_name !=''">
            and last_name like #{last_name}
        if>
        <if test="email!=null and email.trim()!=''">
            and email = #{email}
        if>
        <if test="gender==0 or gender==1">
            and gender = #{gender}
        if>
        where>
    select>

    
    <select id="getEmpsByConditionTrim" resultType="com.mybatis.bean.Employee">
        select * from tbl_employee 
        
        <trim prefix="where" suffixOverrides="and">
        <if test="id!=null">
            id = #{id} and 
        if>
        <if test="last_name!=null and last_name !=''">
            last_name like #{last_name} and 
        if>
        <if test="email!=null and email.trim()!=''">
            email = #{email} and 
        if>
        <if test="gender==0 or gender==1">
            gender = #{gender}
        if>
        trim>
    select>

    
    <select id="getEmpsByConditionChoose" resultType="com.mybatis.bean.Employee">
        select * from tbl_employee
        <where>
            
            <choose>
                <when test="id!=null">
                    id=#{id}
                when>
                <when test="last_name!=null">
                    last_name like #{last_name}
                when>
                <when test="email!=null">
                    email = #{email}
                when>
                <otherwise>
                    gender = 0
                otherwise>
            choose>
        where>
    select>

    
    <update id="updateEmp">
        update tbl_employee 
        <set>
            <if test="last_name!=null">
                last_name=#{last_name},
            if>
            <if test="email!=null">
                email=#{email},
            if>
            <if test="gender!=null">
                gender=#{gender}
            if>
        set>
        where id=#{id} 
        
    update>

    
     <select id="getEmpsByConditionForeach" resultType="com.mybatis.bean.Employee">
        select * from tbl_employee
        
        <foreach collection="ids" item="item_id" separator=","
            open="where id in(" close=")">
            #{item_id}
        foreach>
     select>


        
    <insert id="addEmps">
        insert into tbl_employee(last_name,email,gender,d_id)
        values
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.last_name},#{emp.email},#{emp.gender},#{emp.department.id})
        foreach>
    insert> 
    
     
 mapper>

测试类


public class MybatisTest {

    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testDynamicSQL() throws IOException{
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
            Employee employee = new Employee(3,"Admin",null,null);
            /*List emps = mapper.getEmpsByConditionIf(employee);
            for (Employee employee2 : emps) {
                System.out.println(employee2);
            }*/

            //测试 Trim
            /*List emps2 = mapper.getEmpsByConditionTrim(employee);
            for (Employee employee2 : emps2) {
                System.out.println(employee2);
            }*/
            //测试Choose
            /*List emps3 = mapper.getEmpsByConditionChoose(employee);
            for (Employee employee2 : emps3) {
                System.out.println(employee2);
            }*/

            //测试 set 更新
            /*mapper.updateEmp(employee);
            openSession.commit();*/

            /*List emp2 = mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4));
            for (Employee employee2 : emp2) {
                System.out.println(employee2);
            }*/


        } finally {
            openSession.close();
        }
    }
    @Test
    public void testBatchSave() throws IOException{
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
            List emps = new ArrayList();
            emps.add(new Employee(null, "smith", "[email protected]", "1",new Department(1)));
            emps.add(new Employee(null, "alien", "[email protected]", "0",new Department(2)));
            emps.add(new Employee(null, "TTT", "[email protected]", "1",new Department(1)));
            emps.add(new Employee(null, "hhh", "[email protected]", "1",new Department(1)));
            mapper.addEmps(emps);
            openSession.commit();
        }finally {
            openSession.close();
        }
    }

}

你可能感兴趣的:(mybatis,mybatis,sql,动态SQL)