mybatis usage skills general mapper configuration

Problems encountered in practical applications

After each time the mapper XML file is generated by the tool, if you need to extend the custom method, it is usually added in the generated mapper file, but if the table fields are added or removed later, then you need to manually modify the mapper one by one, or else Regenerate the mapper, and then copy the original extended code into it. In short, these two methods are prone to problems and troublesome.

Solution:
The mapperXML file generated by the tool does not change each time, and the Mapper.xml inheritance mechanism is used to extend the custom method, so that the subsequent regeneration of the mapper file will not affect the extended code.

The mapper file produced by the tool

  • TuserMapper.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.zsx.dao.UserMapper">
    <resultMap id="BaseResultMap" type="com.zsx.entity.User">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="nickname" jdbcType="VARCHAR" property="nickname"/>
    </resultMap>
    <sql id="Base_Column_List">
    id, username, password, email, mobile, nickname
  </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_user
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from t_user
    where id = #{id,jdbcType=BIGINT}
  </delete>
    <insert id="insert" parameterType="com.zsx.entity.User">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into t_user (username, password, email,
        mobile, nickname)
        values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
        #{mobile,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" parameterType="com.zsx.entity.User">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into t_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null">
                username,
            </if>
            <if test="password != null">
                password,
            </if>
            <if test="email != null">
                email,
            </if>
            <if test="mobile != null">
                mobile,
            </if>
            <if test="nickname != null">
                nickname,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null">
                #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                #{password,jdbcType=VARCHAR},
            </if>
            <if test="email != null">
                #{email,jdbcType=VARCHAR},
            </if>
            <if test="mobile != null">
                #{mobile,jdbcType=VARCHAR},
            </if>
            <if test="nickname != null">
                #{nickname,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.zsx.entity.User">
        update t_user
        <set>
            <if test="username != null">
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="email != null">
                email = #{email,jdbcType=VARCHAR},
            </if>
            <if test="mobile != null">
                mobile = #{mobile,jdbcType=VARCHAR},
            </if>
            <if test="nickname != null">
                nickname = #{nickname,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.zsx.entity.User">
    update t_user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      mobile = #{mobile,jdbcType=VARCHAR},
      nickname = #{nickname,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>

</mapper>
  • UserMapper.java
package com.zsx.dao;

import com.zsx.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {

  User selectByPrimaryKey(Long id);

  int deleteByPrimaryKey(Long id);

  int insert(User user);

  int insertSelective(User user);

  int updateByPrimaryKeySelective(User user);

  int updateByPrimaryKey(User user);

}

Extension class, mapper inheritance file

  • UserDao.java
package com.zsx.dao;

import com.zsx.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserDao extends UserMapper {

    List<User> selectByParams(@Param("search") Object search);

}
  • TuserMapperDao.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.zsx.dao.UserDao">
    <resultMap id="BaseResultMap" type="com.zsx.entity.User">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="nickname" jdbcType="VARCHAR" property="nickname"/>
    </resultMap>


    <sql id="Base_Column_List">
        username, password
    </sql>

    <sql id="Base_Where_Clause">
        <where>
            1=1
            <if test="search.username != null">
                and username = #{search.username,jdbcType=VARCHAR}
            </if>
            <if test="search.isDel != null">
                and is_del = #{search.isDel,jdbcType=INTEGER}
            </if>
        </where>
    </sql>

    <select id="selectByParams" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_user
        <include refid="Base_Where_Clause"/>
    </select>
</mapper>
  1. The Mapper.xml inheritance mechanism is only valid for statement, and is invalid for sql and resultMap.
  2. The rules can be summed up as:

    1). In ParentMapper.xml, but not in ChildMapper.xml, ChildMapper follows the definition in ParentMapper.xml

    2). In ParentMapper.xml, also in ChildMapper.xml, ChildMapper uses the definition in ChildMapper.xml

    3). Not in ParentMapper.xml, but in ChildMapper.xml, ChildMapper uses the definition in ChildMapper.xml

  3. After subsequent operations such as field modification, you can use tools to directly replace the two files, UserMapper.java and TuserMapper.xml, to minimize file comparisons and resolve conflicts

public mapper file

We can also extract a mapper file with public methods, so that we don't need to write custom methods with name extensions every time, and there is no problem that these methods are not implemented in mapper.xml.

  • BaseDao.java
package com.zsx.dao;

import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * Created by highness on 2018/3/9 0009.
 */
public interface BaseDao<E> {
    /**mybatis-generator:generate begin***/
    E selectByPrimaryKey(Long id);

    int deleteByPrimaryKey(Long id);

    int insert(E entity);

    int insertSelective(E entity);

    int updateByPrimaryKeySelective(E entity);

    int updateByPrimaryKey(E entity);
    /**mybatis-generator:generate end***/

    /**extended definition begin***/
    int insertBatch(List<E> entityList);

    int deleteBatchByPrimaryKey(@Param("search") Object search);

    int updateByParamsSelective(@Param("entity") E entity, @Param("search") Object search);

    int countByParams(@Param("search") Object search);

    List<Long> selectForPrimaryKey(@Param("search") Object search);

    List<E> selectByParams(@Param("search") Object search);
    /**extended definition end***/
}

When using it, you only need to inherit baseDao in the corresponding mapper, as follows:

@Mapper
public interface UserMapper extends BaseDao<User>{
}

The specific code can go to clone on github, the address is:

https://github.com/zhaoshuxue/springBoot/tree/master/example/springboot-mybatis-xml-extends


Reference article:
Mybatis Mapper.xml inheritance mechanism


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324861825&siteId=291194637