Mybatis in-depth and shallow learning three (dynamic sql sentence analysis of Mybatis)

The basic syntax for splicing Mybatis dynamic sql depends on < if > tags and < Where > tags.

The < foreach > tag is used in traversal

  • Demand one
  • Query object is wrapped by wrapping class, and sql statement is stitched dynamically according to the attribute of object.
  • Packaging class, wrapping user object
public class QueryVo {
    private User user;
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "QueryVo{" +
                "user=" + user +
                '}';
    }
    
}
  • Class User
public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

In mapping configuration file sql statement writing, <where> tag is equivalent to adding where 1 =1 after the statement being written. When judging with <if> tag, you only need to add and XXX = #{xxx x}.

<mapper namespace="com.itheima.dao.QueryVoDao">
    <!--Implementation dynamics sql Compiling-->
    <select id="QueryUserByVo" resultType="user" parameterType="QueryVo">
        SELECT * from user
        <where>
            <if test="user.username !=null ">
                and username like #{user.username}
            </if>
            <if test="user.sex !=null ">
                and sex = #{user.sex}
            </if>
        </where>
    </select>
  • Demand two
  • Query through all elements of list collection in wrapped object
 <select id="QueryUserByList" resultType="user" parameterType="QueryVo">
        select * from USER
        <where>
            <if test="integers!=null and integers.size()>0">
                <foreach collection="integers" item="id" open="and id in (" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
  • mybatis parses objects through OGNL expressions only by matching the attributes in the corresponding javabean with the fields corresponding to the OGNL expressions.
  • Collection in the < foreach > tag: traversal collection item: traversal object open: traversal start string close: traversal end string separator: traversal element separator

Keywords: Programming SQL Mybatis Attribute

Added by lisa3711 on Sat, 05 Oct 2019 17:46:58 +0300