1. 程式人生 > >ssm框架學習---mybatis中動態sql中的if片段

ssm框架學習---mybatis中動態sql中的if片段

1.if判斷,比如多個條件查詢中,如果某個條件不為空,才將查詢內容拼接上去

首先還是編寫mapper.xml檔案如下:

<?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.ajin.mybatis.mapper.StudentMapper">
    <!--使用if判斷條件是否為空,進行查詢sql語句的拼接-->
    <select id="selectStudentMul" parameterType="com.ajin.mybatis.model.StudentVo" resultType="com.ajin.mybatis.model.Student">
        <!-- select * from student where sname=#{student.sname} and ssex =#{student.ssex}
           一定要注意上面這句話是否時真的註釋成功了,就是滿足xml檔案註釋的格式,否則不小心會發現雖然註釋了,但是最終執行會報錯,並沒有被註釋掉
        -->
        select * from student s
           <!-- 使用where標籤可以自動去掉第一個and 很方便-->
           <where>
               <if test="student!=null">
                   <if test="student.sname!=null and student.sname!=''">
                            and s.sname=#{student.sname}
                   </if>
                   <if test="student.ssex!=null and student.ssex!=''">
                       and s.ssex=#{student.ssex}
                   </if>
               </if>

           </where>
    </select>
</mapper>
對應的mapper.java的定義如下:
package com.ajin.mybatis.mapper;

import com.ajin.mybatis.model.Student;
import com.ajin.mybatis.model.StudentVo;

import java.util.List;

/**
 * Created by ajin on 16-12-16.
 */
public interface StudentMapper {

    List<Student> selectStudentMul(StudentVo studentVo);
}
對應的輸入輸出引數的StudentVo和Student定義如下:
package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-16.
 */
public class Student {
    private int id;
    private String sname;
    private String ssex;
    private int sage;
    private String saddress;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    public String getSaddress() {
        return saddress;
    }

    public void setSaddress(String saddress) {
        this.saddress = saddress;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", sname='" + sname + '\'' +
                ", ssex='" + ssex + '\'' +
                ", sage=" + sage +
                ", saddress='" + saddress + '\'' +
                '}';
    }
}
package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-16.
 */
public class StudentVo {
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}
最終的測試程式碼如下:

可以通過是否註釋相應的屬性,來測試我們動態sql語句的執行:

package com.ajin.mybatis.mapper;

import com.ajin.mybatis.model.Student;
import com.ajin.mybatis.model.StudentVo;
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.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by ajin on 16-12-16.
 */
public class StudentMapperTest {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void setUp() throws Exception {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void selectStudentMul() throws Exception {
        StudentVo studentVo= new StudentVo();
       Student student = new Student();
       student.setSname("zcj");
       student.setSsex("female");
       studentVo.setStudent(student);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> list=studentMapper.selectStudentMul(studentVo);
            System.out.print(list);
        }finally {
            sqlSession.close();
        }
    }

}
上面時if判斷的一個簡單示例。使用它時就像我們編寫if語句一樣,一個test用來檢測條件是否滿足,裡面還可以繼續巢狀if語句