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);
    }
    
    
}

欢迎评论,大家一起学习。

原文地址:https://www.cnblogs.com/1998xujinren/p/11211571.html