第十四篇:SpringBoot2.x整合MyBatisGenerator

在這里簡(jiǎn)單介紹一下如何整合Mybatis自動(dòng)生成代碼的插件MybatisGenerator

引入插件

需要在pom.xml文件中的<build><plugins></plugins></build>中加入以下設(shè)置

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <!-- 添加一個(gè)mysql的依賴,防止等會(huì)找不到driverClass -->
                <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.28</version>
                    <scope>runtime</scope>
                </dependency>
                </dependencies>
                <!-- mybatisGenerator 的配置 -->
                <configuration>
                    <!-- generator 工具配置文件的位置 -->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <!-- 是否覆蓋 -->
                    <!-- 此處要特別注意,如果不加這個(gè)設(shè)置會(huì)導(dǎo)致每次運(yùn)行都會(huì)在原目錄再次創(chuàng)建-->
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

引入依賴后右側(cè)的maven project側(cè)邊欄中的Plugins選項(xiàng)會(huì)多出一個(gè)mybatis-generator

image.png

設(shè)置配置文件

接下來在resources文件夾中創(chuàng)建一個(gè)generatorConfig.xml文件用于自動(dòng)構(gòu)建代碼的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="MySQLTables" targetRuntime="MyBatis3" defaultModelType="flat">

        <!-- 公共設(shè)置 -->
        <commentGenerator>
            <!-- 是否取消自動(dòng)生成時(shí)的注釋 -->
            <property name="suppressAllComments" value="true"/>
            <!-- 是否取消在注釋中加上時(shí)間 -->
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!-- 鏈接數(shù)據(jù)庫(kù)的配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///test" userId="root" password="root"/>

        <!-- 關(guān)于生成實(shí)體類的設(shè)置 -->
        <!-- targetPackage 生成代碼的目標(biāo)目錄 -->
        <!-- targetProject 目錄所屬位置 -->
        <javaModelGenerator targetPackage="priv.gabriel.model" targetProject="src/main/java">
            <!-- 在targetPackge的基礎(chǔ)上根據(jù)schema再生成一層package 默認(rèn)flase -->
            <property name="enableSubPackages" value="true"/>
            <!-- 是否在get方法中 對(duì)String類型的字段做空的判斷 -->
            <property name="trimStrings" value="true"/>
            <!-- 是否生成一個(gè)包含所有字段的構(gòu)造器 -->
            <property name="constructorBased" value="false"/>
            <!-- 是否創(chuàng)建一個(gè)不可變類-->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--關(guān)于生成映射文件的設(shè)置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <!--同上-->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--關(guān)于生成dao層的設(shè)置-->
        <javaClientGenerator type="mapper" targetPackage="priv.gabriel.dao" targetProject="src/main/java">
            <!--同上-->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--需要生成的代碼對(duì)應(yīng)的表名-->
        <table tableName="user"></table>
    </context>
</generatorConfiguration>

運(yùn)行插件

配置就結(jié)束了此疹,最后運(yùn)行一下巍佑,這里找到在Maven Projectmybatis-generator:generator選項(xiàng)辞做,雙擊運(yùn)行

image.png

最后的成果

User.java

package priv.gabriel.model;

import java.util.Date;

public class User {
    private Integer id;

    private String username;

    private String pword;

    private Integer age;

    private Date utime;

    private Date ctime;

    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 == null ? null : username.trim();
    }

    public String getPword() {
        return pword;
    }

    public void setPword(String pword) {
        this.pword = pword == null ? null : pword.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getUtime() {
        return utime;
    }

    public void setUtime(Date utime) {
        this.utime = utime;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }
}

UserMapper.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="priv.gabriel.dao.UserMapper">
  <resultMap id="BaseResultMap" type="priv.gabriel.model.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="pword" jdbcType="VARCHAR" property="pword" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="utime" jdbcType="DATE" property="utime" />
    <result column="ctime" jdbcType="DATE" property="ctime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <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">
    <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">
    id, username, pword, age, utime, ctime
  </sql>
  <select id="selectByExample" parameterType="priv.gabriel.model.UserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <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">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="priv.gabriel.model.UserExample">
    delete from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="priv.gabriel.model.User">
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  </insert>
  <insert id="insertSelective" parameterType="priv.gabriel.model.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="pword != null">
        pword,
      </if>
      <if test="age != null">
        age,
      </if>
      <if test="utime != null">
        utime,
      </if>
      <if test="ctime != null">
        ctime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        #{ctime,jdbcType=DATE},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="priv.gabriel.model.UserExample" resultType="java.lang.Long">
    select count(*) from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.username != null">
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.pword != null">
        pword = #{record.pword,jdbcType=VARCHAR},
      </if>
      <if test="record.age != null">
        age = #{record.age,jdbcType=INTEGER},
      </if>
      <if test="record.utime != null">
        utime = #{record.utime,jdbcType=DATE},
      </if>
      <if test="record.ctime != null">
        ctime = #{record.ctime,jdbcType=DATE},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="priv.gabriel.model.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        pword = #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        utime = #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        ctime = #{ctime,jdbcType=DATE},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="priv.gabriel.model.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="priv.gabriel.model.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="pword" jdbcType="VARCHAR" property="pword" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="utime" jdbcType="DATE" property="utime" />
    <result column="ctime" jdbcType="DATE" property="ctime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <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">
    <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">
    id, username, pword, age, utime, ctime
  </sql>
  <select id="selectByExample" parameterType="priv.gabriel.model.UserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <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">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="priv.gabriel.model.UserExample">
    delete from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="priv.gabriel.model.User">
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  </insert>
  <insert id="insertSelective" parameterType="priv.gabriel.model.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="pword != null">
        pword,
      </if>
      <if test="age != null">
        age,
      </if>
      <if test="utime != null">
        utime,
      </if>
      <if test="ctime != null">
        ctime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        #{ctime,jdbcType=DATE},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="priv.gabriel.model.UserExample" resultType="java.lang.Long">
    select count(*) from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.username != null">
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.pword != null">
        pword = #{record.pword,jdbcType=VARCHAR},
      </if>
      <if test="record.age != null">
        age = #{record.age,jdbcType=INTEGER},
      </if>
      <if test="record.utime != null">
        utime = #{record.utime,jdbcType=DATE},
      </if>
      <if test="record.ctime != null">
        ctime = #{record.ctime,jdbcType=DATE},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="priv.gabriel.model.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        pword = #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        utime = #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        ctime = #{ctime,jdbcType=DATE},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="priv.gabriel.model.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="priv.gabriel.model.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="pword" jdbcType="VARCHAR" property="pword" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="utime" jdbcType="DATE" property="utime" />
    <result column="ctime" jdbcType="DATE" property="ctime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <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">
    <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">
    id, username, pword, age, utime, ctime
  </sql>
  <select id="selectByExample" parameterType="priv.gabriel.model.UserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <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">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="priv.gabriel.model.UserExample">
    delete from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="priv.gabriel.model.User">
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  </insert>
  <insert id="insertSelective" parameterType="priv.gabriel.model.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="pword != null">
        pword,
      </if>
      <if test="age != null">
        age,
      </if>
      <if test="utime != null">
        utime,
      </if>
      <if test="ctime != null">
        ctime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        #{ctime,jdbcType=DATE},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="priv.gabriel.model.UserExample" resultType="java.lang.Long">
    select count(*) from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.username != null">
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.pword != null">
        pword = #{record.pword,jdbcType=VARCHAR},
      </if>
      <if test="record.age != null">
        age = #{record.age,jdbcType=INTEGER},
      </if>
      <if test="record.utime != null">
        utime = #{record.utime,jdbcType=DATE},
      </if>
      <if test="record.ctime != null">
        ctime = #{record.ctime,jdbcType=DATE},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="priv.gabriel.model.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        pword = #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        utime = #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        ctime = #{ctime,jdbcType=DATE},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="priv.gabriel.model.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="priv.gabriel.model.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="pword" jdbcType="VARCHAR" property="pword" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="utime" jdbcType="DATE" property="utime" />
    <result column="ctime" jdbcType="DATE" property="ctime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <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">
    <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">
    id, username, pword, age, utime, ctime
  </sql>
  <select id="selectByExample" parameterType="priv.gabriel.model.UserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <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">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="priv.gabriel.model.UserExample">
    delete from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="priv.gabriel.model.User">
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  </insert>
  <insert id="insertSelective" parameterType="priv.gabriel.model.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="pword != null">
        pword,
      </if>
      <if test="age != null">
        age,
      </if>
      <if test="utime != null">
        utime,
      </if>
      <if test="ctime != null">
        ctime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        #{ctime,jdbcType=DATE},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="priv.gabriel.model.UserExample" resultType="java.lang.Long">
    select count(*) from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.username != null">
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.pword != null">
        pword = #{record.pword,jdbcType=VARCHAR},
      </if>
      <if test="record.age != null">
        age = #{record.age,jdbcType=INTEGER},
      </if>
      <if test="record.utime != null">
        utime = #{record.utime,jdbcType=DATE},
      </if>
      <if test="record.ctime != null">
        ctime = #{record.ctime,jdbcType=DATE},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="priv.gabriel.model.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="pword != null">
        pword = #{pword,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="utime != null">
        utime = #{utime,jdbcType=DATE},
      </if>
      <if test="ctime != null">
        ctime = #{ctime,jdbcType=DATE},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="priv.gabriel.model.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

UserMapper.java

package priv.gabriel.dao;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import priv.gabriel.model.User;
import priv.gabriel.model.UserExample;

public interface UserMapper {
    long countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    List<User> selectByExample(UserExample example);

    User selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

可以看到已經(jīng)幫我們生產(chǎn)了關(guān)于CRUD的絕大代碼,再結(jié)合之前的通用mapper就可以解決絕大部分的問題啦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末项秉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子咕宿,更是在濱河造成了極大的恐慌烦粒,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件始锚,死亡現(xiàn)場(chǎng)離奇詭異刽酱,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)瞧捌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來润文,“玉大人姐呐,你說我怎么就攤上這事〉潋颍” “怎么了曙砂?”我有些...
    開封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)骏掀。 經(jīng)常有香客問我鸠澈,道長(zhǎng)柱告,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任笑陈,我火速辦了婚禮际度,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘涵妥。我一直安慰自己乖菱,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開白布蓬网。 她就那樣靜靜地躺著窒所,像睡著了一般。 火紅的嫁衣襯著肌膚如雪帆锋。 梳的紋絲不亂的頭發(fā)上吵取,一...
    開封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天,我揣著相機(jī)與錄音锯厢,去河邊找鬼海渊。 笑死,一個(gè)胖子當(dāng)著我的面吹牛哲鸳,可吹牛的內(nèi)容都是我干的臣疑。 我是一名探鬼主播盯仪,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼疏咐,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼实昨!你這毒婦竟也來了滔驶?” 一聲冷哼從身側(cè)響起粟瞬,我...
    開封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤琢感,失蹤者是張志新(化名)和其女友劉穎宇攻,沒想到半個(gè)月后型雳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萍摊,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挤茄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冰木。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片穷劈。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖踊沸,靈堂內(nèi)的尸體忽然破棺而出歇终,到底是詐尸還是另有隱情,我是刑警寧澤逼龟,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布评凝,位于F島的核電站,受9級(jí)特大地震影響腺律,放射性物質(zhì)發(fā)生泄漏奕短。R本人自食惡果不足惜宜肉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望翎碑。 院中可真熱鬧谬返,春花似錦、人聲如沸杈女。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽达椰。三九已至翰蠢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間啰劲,已是汗流浹背梁沧。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蝇裤,地道東北人廷支。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像栓辜,于是被迫代替她去往敵國(guó)和親恋拍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 1 Mybatis入門 1.1 單獨(dú)使用jdbc編程問題總結(jié) 1.1.1 jdbc程序 上邊使...
    哇哈哈E閱讀 3,293評(píng)論 0 38
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理藕甩,服務(wù)發(fā)現(xiàn)施敢,斷路器,智...
    卡卡羅2017閱讀 134,599評(píng)論 18 139
  • Mybatis介紹 MyBatis 本是apache的一個(gè)開源項(xiàng)目iBatis, 2010年這個(gè)項(xiàng)目由apache...
    day_Sunny閱讀 2,642評(píng)論 0 6
  • https://www.cnblogs.com/huahua0809/p/5293098.html
    MrWT閱讀 299評(píng)論 0 0
  • 現(xiàn)如今小鮮肉當(dāng)?shù)老晾常蝿?shì)一片大好僵娃,這股鮮肉風(fēng)不僅是演藝圈,兢兢業(yè)業(yè)的體壇也跟著湊熱鬧腋妙。國(guó)民老公王思聰隨著“霍頓事件”...
    Director楊閱讀 812評(píng)論 8 6