关于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

相关文章:

  • 2022-12-23
  • 2021-07-16
  • 2022-01-28
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-01-25
  • 2021-05-02
相关资源
相似解决方案