mybatis學習(六)(Mapper 中 trim 的使用)


關於mapper中是有很多的屬性可以靈活使用,這里簡單介紹一下trim的使用,trim可以配合語句動態的生成最終的sql語句,方便靈活

具體mapper.xml配置如下:

<insert id="insert01" parameterType="Emp1">
         insert into emp
         <trim prefix="(" suffix=")"  suffixOverrides=","><!-- prefix:以什么開頭; suffix:以什么結尾; suffixOverrides:以什么分隔,多了會自動刪除,少了會自動補全 -->
            <if test="ename != null"><!-- 動態生成要添加的字段 -->
                ename,
            </if>    
            <if test="empno != 0">
                empno,
            </if>
            <if test="empno != 0">
                deptno,
            </if>            
         </trim>
         values
         <trim prefix="(" suffix=")"  suffixOverrides=",">
             <if test="ename != null"><!-- 動態生成要添加的數據 -->
                #{ename},
            </if>    
            <if test="empno != 0">
                #{empno},
            </if>
            <if test="empno != 0">
                #{deptno},
            </if>     
         </trim>
    </insert>

 

Emp1的實體類如下:

package com.yc.mybatis;

public class Emp1 {
    
    private int empno;
    private int deptno;
    private String ename;
    
    @Override
    public String toString() {
        return "Emp1 [empno=" + empno + ", deptno=" + deptno + ", ename=" + ename + "]";
    }
    
    public int getEmpno() {
        return empno;
    }
    
    public void setEmpno(int empno) {
        this.empno = empno;
    }
    
    public int getDeptno() {
        return deptno;
    }
    
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }
    
    public String getEname() {
        return ename;
    }
    
    public void setEname(String ename) {
        this.ename = ename;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + empno;
        result = prime * result + ((ename == null) ? 0 : ename.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Emp1 other = (Emp1) obj;
        if (deptno != other.deptno)
            return false;
        if (empno != other.empno)
            return false;
        if (ename == null) {
            if (other.ename != null)
                return false;
        } else if (!ename.equals(other.ename))
            return false;
        return true;
    }
    public Emp1(int empno, int deptno, String ename) {
        super();
        this.empno = empno;
        this.deptno = deptno;
        this.ename = ename;
    }
    public Emp1() {
        super();
    }
}
Emp1.java

 

測試代碼如下:

 

package com.yc.mybatis;

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

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;

public class TestTest01 {
    InputStream is = null;
    SqlSessionFactory factory = null;
    SqlSession session = null;
    {
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
            session = factory.openSession();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    @Test
    public void TTest07(){
        Emp1 emp = new Emp1();
        emp.setDeptno(20);
        emp.setEmpno(7340);
        emp.setEname("無語");
        int result = session.update("TTest.insert01", emp);
        session.commit();
        System.out.println(result);
    }
    
    
}

 

歡迎評論,大家一起學習。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM