MybatisCodeHelpPro生成持久层代码

  |   0 评论   |   0 浏览

MybatisCodeHelpPro生成持久层代码

链接:https://pan.baidu.com/s/18-ZMVW6WjW8oUbCUlzJI9A
提取码:8ruy

1.链接数据库

image-20220518182446695

2.详细配置

生成方式

image-20220518182506205

文件生成目录配置

image-20220518182538590

生成注解、方法配置

image-20220518182600219

image-20220518182620516

File-Settings设置选项中配置:接口后缀名,设置为Dao 则生成的即可名为MytestDao ;Mapper则为MyTestMapper

3.生成后的代码

提示:这里我建的表名为mytest 生成的规则是首字母大写,如果想要达到MyTest这种效果,则在建表示已下划线分割即可 my_test ——> MyTest

1.接口—Mytest

package com.zds.tbl.dao;

import com.zds.tbl.po.Mytest;
import com.zds.tbl.po.MytestExample;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MytestDao {
    /**
     * 根据条件查询总数
     * @param example
     * @return
     */
    long countByExample(MytestExample example);

    /**
     * 根据条件进行删除
     * @param example 可以组装条件
     * @return
     */
    int deleteByExample(MytestExample example);

    /**
     * delete by primary key 根据主键进行删除
     * @param id primaryKey
     * @return deleteCount
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * 全量插入实体类字段值到表中
     * insert record to table
     * @param record the record
     * @return insert count
     */
    int insert(Mytest record);

    /**
     *主键或唯一索引在现有表中已经存在,则会执行更新操作
     * @param record
     * @return
     */
    int insertOrUpdate(Mytest record);

    /**
     * 主键或唯一索引在现有表中已经存在,则会执行更新操作
     * 选择性的插入或者更新,如果字段值!=null则进行更新或者插入
     * @param record
     * @return
     */
    int insertOrUpdateSelective(Mytest record);

    /**
     * 选择性的插入,如果字段值!=null则插入
     * insert record to table selective
     * @param record the record
     * @return insert count
     */
    int insertSelective(Mytest record);

    /**
     * 根据条件进行查询
     * @param example
     * @return
     */
    List<Mytest> selectByExample(MytestExample example);

    /**
     * 根据主键进行查询
     * select by primary key
     * @param id primary key
     * @return object by primary key
     */
    Mytest selectByPrimaryKey(Integer id);

    /**
     * 根据条件进行更新
     * @param record  实体,字段值!=null的进行更新
     * @param example 更新条件
     * @return
     */
    int updateByExampleSelective(@Param("record") Mytest record, @Param("example") MytestExample example);

    /**
     * 根据条件全量表更新
     * @param record 实体,全量表更新即使字段值为null也会进行更新
     * @param example 更新条件
     * @return
     */
    int updateByExample(@Param("record") Mytest record, @Param("example") MytestExample example);

    /**
     * 根据主键进行选择性更新
     * update record selective
     * @param record the updated record
     * @return update count
     */
    int updateByPrimaryKeySelective(Mytest record);

    /**
     * 根据主键进行表全量更新
     * update record
     * @param record the updated record
     * @return update count
     */
    int updateByPrimaryKey(Mytest record);

    /**
     * 根据主键集,批量全表更新
     * @param list
     * @return
     */
    int updateBatch(List<Mytest> list);

    /**
     * 根据主键集,选择性批量更新
     * @param list 字段值!=null的进行更新
     * @return
     */
    int updateBatchSelective(List<Mytest> list);

    /**
     * 批量插入
     * @param list
     * @return
     */
    int batchInsert(@Param("list") List<Mytest> list);
}

2.实体—Mytest

package com.zds.tbl.po;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@ApiModel(value="com-zds-tbl-po-Mytest")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Mytest {
    /**
    * id
    */
    @ApiModelProperty(value="id")
    private Integer id;

    /**
    * 名称
    */
    @ApiModelProperty(value="名称")
    private String name;
}

3.xml—MytestDao.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.zds.tbl.dao.MytestDao">
  <resultMap id="BaseResultMap" type="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    <!--@Table mytest-->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--@mbg.generated-->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--@mbg.generated-->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, `name`
  </sql>
  <select id="selectByExample" parameterType="com.zds.tbl.po.MytestExample" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from mytest
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from mytest
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--@mbg.generated-->
    delete from mytest
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.zds.tbl.po.MytestExample">
    <!--@mbg.generated-->
    delete from mytest
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    insert into mytest (id, `name`)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    insert into mytest
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        `name`,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.zds.tbl.po.MytestExample" resultType="java.lang.Long">
    <!--@mbg.generated-->
    select count(*) from mytest
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--@mbg.generated-->
    update mytest
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.name != null">
        `name` = #{record.name,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--@mbg.generated-->
    update mytest
    set id = #{record.id,jdbcType=INTEGER},
      `name` = #{record.name,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    update mytest
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    update mytest
    set `name` = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateBatch" parameterType="java.util.List">
    <!--@mbg.generated-->
    update mytest
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="`name` = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.name,jdbcType=VARCHAR}
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>
  <update id="updateBatchSelective" parameterType="java.util.List">
    <!--@mbg.generated-->
    update mytest
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="`name` = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.name != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.name,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>
  <insert id="batchInsert" parameterType="map">
    <!--@mbg.generated-->
    insert into mytest
    (id, `name`)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.id,jdbcType=INTEGER}, #{item.name,jdbcType=VARCHAR})
    </foreach>
  </insert>
  <insert id="insertOrUpdate" parameterType="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    insert into mytest
    (id, `name`)
    values
    (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
    on duplicate key update 
    id = #{id,jdbcType=INTEGER}, 
    `name` = #{name,jdbcType=VARCHAR}
  </insert>
  <insert id="insertOrUpdateSelective" parameterType="com.zds.tbl.po.Mytest">
    <!--@mbg.generated-->
    insert into mytest
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        `name`,
      </if>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
    on duplicate key update 
    <trim suffixOverrides=",">
      <if test="id != null">
        id = #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
</mapper>

标题:MybatisCodeHelpPro生成持久层代码
作者:llp
地址:https://llinp.cn/articles/2022/05/12/1652363936803.html