• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

mybatis-generator-core-1.3.6.jar代码生成器的使用

武飞扬头像
暗夜@
帮助1

使用代码生成器能快速的生成mapper、实体类、以及mapper映射文件,大大提高了开发效率,具体步骤如下:

1、新建一个格式为XML的配置文件 名称generatorConfig

我把配置文件放在了resources目录下的generator下

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3.  
    <generatorConfiguration>
  4.  
    <!-- 1、生成文件的命令:java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
  5.  
    2、在mybatis-generator-core-1.3.6.jar所在目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成文件的命令即可。 -->
  6.  
     
  7.  
    <!-- 数据库驱动包位置 -->
  8.  
    <classPathEntry location="D:\apache-maven-3.6.1\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />
  9.  
    <context id="DB2Tables" targetRuntime="MyBatis3">
  10.  
    <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
  11.  
    <commentGenerator>
  12.  
    <property name="suppressAllComments" value="true" />
  13.  
    </commentGenerator>
  14.  
    <!-- 数据库链接URL、用户名、密码 -->
  15.  
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  16.  
    connectionURL="jdbc:mysql://localhost:8080/mp?characterEncoding=utf-8"
  17.  
    userId="root"
  18.  
    password="123456">
  19.  
    </jdbcConnection>
  20.  
    <javaTypeResolver>
  21.  
    <property name="forceBigDecimals" value="false" />
  22.  
    </javaTypeResolver>
  23.  
    <!-- pojo -->
  24.  
    <javaModelGenerator targetPackage="com.caochenlei.mpdemo.pojo"
  25.  
    targetProject="E:\Test\mp-demo\src\main\java">
  26.  
    <property name="enableSubPackages" value="true" />
  27.  
    <property name="trimStrings" value="true" />
  28.  
    </javaModelGenerator>
  29.  
    <!-- xml -->
  30.  
    <sqlMapGenerator targetPackage="com.caochenlei.mpdemo.mapper"
  31.  
    targetProject="E:\Test\mp-demo\src\main\resources">
  32.  
    <property name="enableSubPackages" value="true" />
  33.  
    </sqlMapGenerator>
  34.  
     
  35.  
    <!-- DAo -->
  36.  
    <javaClientGenerator type="XMLMAPPER"
  37.  
    targetPackage="com.caochenlei.mpdemo.mapper"
  38.  
    targetProject="E:\Test\mp-demo\src\main\java">
  39.  
    <property name="enableSubPackages" value="true" />
  40.  
    </javaClientGenerator>
  41.  
     
  42.  
    <table tableName="Test_case" domainObjectName="TestCaseDO" enableDeleteByExample="false" >
  43.  
    </table>
  44.  
     
  45.  
    </context>
  46.  
    </generatorConfiguration>
学新通

需要修改的有:

1、数据库驱动包位置

2、数据库地址以及账号密码

3、实体类的包名以及路径

4、xml映射文件的包名及路径

5、DAO的路径

6、tableName表名称    domainObjectName 需要生成的类名(会根据配置的库名中的表名生成实体类、mapper、mapper映射文件)

二、下载 mybatis-generator-core-1.3.6.jar

链接:https://pan.百度.com/s/1URuElNFllYMNCIBWBYfrbw
提取码:obqj

和XML文件放在同一个目录下

学新通

三、进入Terminal命令界面 cd到jar包放置的目录

resources目录下的generator下

学新通

cd src\main\resources\generator

输入如下命令java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite

回车即可

学新通

四、看看效果

实体类:

  1.  
    package com.caochenlei.mpdemo.pojo;
  2.  
     
  3.  
     
  4.  
    import java.sql.Date;
  5.  
     
  6.  
    public class SysUserDO {
  7.  
    private Long uid;
  8.  
     
  9.  
    private String userName;
  10.  
     
  11.  
    private String passWord;
  12.  
     
  13.  
    private String nickName;
  14.  
     
  15.  
    private String mobile;
  16.  
     
  17.  
    private String email;
  18.  
     
  19.  
    private String createTime;
  20.  
     
  21.  
    private String updateTime;
  22.  
     
  23.  
    public Long getUid() {
  24.  
    return uid;
  25.  
    }
  26.  
     
  27.  
    public void setUid(Long uid) {
  28.  
    this.uid = uid;
  29.  
    }
  30.  
     
  31.  
    public String getUserName() {
  32.  
    return userName;
  33.  
    }
  34.  
     
  35.  
    public void setUserName(String userName) {
  36.  
    this.userName = userName == null ? null : userName.trim();
  37.  
    }
  38.  
     
  39.  
    public String getPassWord() {
  40.  
    return passWord;
  41.  
    }
  42.  
     
  43.  
    public void setPassWord(String passWord) {
  44.  
    this.passWord = passWord == null ? null : passWord.trim();
  45.  
    }
  46.  
     
  47.  
    public String getNickName() {
  48.  
    return nickName;
  49.  
    }
  50.  
     
  51.  
    public void setNickName(String nickName) {
  52.  
    this.nickName = nickName == null ? null : nickName.trim();
  53.  
    }
  54.  
     
  55.  
    public String getMobile() {
  56.  
    return mobile;
  57.  
    }
  58.  
     
  59.  
    public void setMobile(String mobile) {
  60.  
    this.mobile = mobile == null ? null : mobile.trim();
  61.  
    }
  62.  
     
  63.  
    public String getEmail() {
  64.  
    return email;
  65.  
    }
  66.  
     
  67.  
    public void setEmail(String email) {
  68.  
    this.email = email == null ? null : email.trim();
  69.  
    }
  70.  
     
  71.  
    public String getCreateTime() {
  72.  
    return createTime;
  73.  
    }
  74.  
     
  75.  
    public void setCreateTime(String createTime) {
  76.  
    this.createTime = createTime;
  77.  
    }
  78.  
     
  79.  
    public String getUpdateTime() {
  80.  
    return updateTime;
  81.  
    }
  82.  
     
  83.  
    public void setUpdateTime(String updateTime) {
  84.  
    this.updateTime = updateTime;
  85.  
    }
  86.  
    }
学新通

mapper:

  1.  
    package com.caochenlei.mpdemo.mapper;
  2.  
     
  3.  
    import com.caochenlei.mpdemo.pojo.SysUserDO;
  4.  
    import com.caochenlei.mpdemo.pojo.SysUserDOExample;
  5.  
    import java.util.List;
  6.  
    import org.apache.ibatis.annotations.Param;
  7.  
    import org.apache.ibatis.session.RowBounds;
  8.  
     
  9.  
    public interface SysUserDOMapper {
  10.  
    long countByExample(SysUserDOExample example);
  11.  
     
  12.  
    int deleteByPrimaryKey(Long uid);
  13.  
     
  14.  
    int insert(SysUserDO record);
  15.  
     
  16.  
    int insertSelective(SysUserDO record);
  17.  
     
  18.  
    List<SysUserDO> selectByExampleWithRowbounds(SysUserDOExample example, RowBounds rowBounds);
  19.  
     
  20.  
    List<SysUserDO> selectByExample(SysUserDOExample example);
  21.  
     
  22.  
    SysUserDO selectByPrimaryKey(Long uid);
  23.  
     
  24.  
    int updateByExampleSelective(@Param("record") SysUserDO record, @Param("example") SysUserDOExample example);
  25.  
     
  26.  
    int updateByExample(@Param("record") SysUserDO record, @Param("example") SysUserDOExample example);
  27.  
     
  28.  
    int updateByPrimaryKeySelective(SysUserDO record);
  29.  
     
  30.  
    int updateByPrimaryKey(SysUserDO record);
  31.  
     
  32.  
    List<SysUserDO> queryByPage(@Param("pageNo")Integer pageNo, @Param("pageSize")Integer pageSize);
  33.  
    }
学新通

xml映射文件:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3.  
    <mapper namespace="com.caochenlei.mpdemo.mapper.SysUserDOMapper">
  4.  
    <resultMap id="BaseResultMap" type="com.caochenlei.mpdemo.pojo.SysUserDO">
  5.  
    <id column="uid" jdbcType="BIGINT" property="uid" />
  6.  
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
  7.  
    <result column="pass_word" jdbcType="VARCHAR" property="passWord" />
  8.  
    <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
  9.  
    <result column="mobile" jdbcType="VARCHAR" property="mobile" />
  10.  
    <result column="email" jdbcType="VARCHAR" property="email" />
  11.  
    <result column="create_time" jdbcType="DATE" property="createTime" />
  12.  
    <result column="update_time" jdbcType="DATE" property="updateTime" />
  13.  
    </resultMap>
  14.  
    <sql id="Example_Where_Clause">
  15.  
    <where>
  16.  
    <foreach collection="oredCriteria" item="criteria" separator="or">
  17.  
    <if test="criteria.valid">
  18.  
    <trim prefix="(" prefixOverrides="and" suffix=")">
  19.  
    <foreach collection="criteria.criteria" item="criterion">
  20.  
    <choose>
  21.  
    <when test="criterion.noValue">
  22.  
    and ${criterion.condition}
  23.  
    </when>
  24.  
    <when test="criterion.singleValue">
  25.  
    and ${criterion.condition} #{criterion.value}
  26.  
    </when>
  27.  
    <when test="criterion.betweenValue">
  28.  
    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  29.  
    </when>
  30.  
    <when test="criterion.listValue">
  31.  
    and ${criterion.condition}
  32.  
    <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
  33.  
    #{listItem}
  34.  
    </foreach>
  35.  
    </when>
  36.  
    </choose>
  37.  
    </foreach>
  38.  
    </trim>
  39.  
    </if>
  40.  
    </foreach>
  41.  
    </where>
  42.  
    </sql>
  43.  
    <sql id="Update_By_Example_Where_Clause">
  44.  
    <where>
  45.  
    <foreach collection="example.oredCriteria" item="criteria" separator="or">
  46.  
    <if test="criteria.valid">
  47.  
    <trim prefix="(" prefixOverrides="and" suffix=")">
  48.  
    <foreach collection="criteria.criteria" item="criterion">
  49.  
    <choose>
  50.  
    <when test="criterion.noValue">
  51.  
    and ${criterion.condition}
  52.  
    </when>
  53.  
    <when test="criterion.singleValue">
  54.  
    and ${criterion.condition} #{criterion.value}
  55.  
    </when>
  56.  
    <when test="criterion.betweenValue">
  57.  
    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  58.  
    </when>
  59.  
    <when test="criterion.listValue">
  60.  
    and ${criterion.condition}
  61.  
    <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
  62.  
    #{listItem}
  63.  
    </foreach>
  64.  
    </when>
  65.  
    </choose>
  66.  
    </foreach>
  67.  
    </trim>
  68.  
    </if>
  69.  
    </foreach>
  70.  
    </where>
  71.  
    </sql>
  72.  
    <sql id="Base_Column_List">
  73.  
    uid, user_name, pass_word, nick_name, mobile, email, create_time, update_time
  74.  
    </sql>
  75.  
    <select id="selectByExample" parameterType="com.caochenlei.mpdemo.pojo.SysUserDOExample" resultMap="BaseResultMap">
  76.  
    select
  77.  
    <if test="distinct">
  78.  
    distinct
  79.  
    </if>
  80.  
    <include refid="Base_Column_List" />
  81.  
    from user_base
  82.  
    <if test="_parameter != null">
  83.  
    <include refid="Example_Where_Clause" />
  84.  
    </if>
  85.  
    <if test="orderByClause != null">
  86.  
    order by ${orderByClause}
  87.  
    </if>
  88.  
    </select>
  89.  
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
  90.  
    select
  91.  
    <include refid="Base_Column_List" />
  92.  
    from user_base
  93.  
    where uid = #{uid,jdbcType=BIGINT}
  94.  
    </select>
  95.  
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
  96.  
    delete from user_base
  97.  
    where uid = #{uid,jdbcType=BIGINT}
  98.  
    </delete>
  99.  
    <insert id="insert" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
  100.  
    insert into user_base (uid, user_name, pass_word,
  101.  
    nick_name, mobile, email,
  102.  
    create_time, update_time)
  103.  
    values (#{uid,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{passWord,jdbcType=VARCHAR},
  104.  
    #{nickName,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
  105.  
    #{createTime,jdbcType=DATE}, #{updateTime,jdbcType=DATE})
  106.  
    </insert>
  107.  
    <insert id="insertSelective" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
  108.  
    insert into user_base
  109.  
    <trim prefix="(" suffix=")" suffixOverrides=",">
  110.  
    <if test="uid != null">
  111.  
    uid,
  112.  
    </if>
  113.  
    <if test="userName != null">
  114.  
    user_name,
  115.  
    </if>
  116.  
    <if test="passWord != null">
  117.  
    pass_word,
  118.  
    </if>
  119.  
    <if test="nickName != null">
  120.  
    nick_name,
  121.  
    </if>
  122.  
    <if test="mobile != null">
  123.  
    mobile,
  124.  
    </if>
  125.  
    <if test="email != null">
  126.  
    email,
  127.  
    </if>
  128.  
    <if test="createTime != null">
  129.  
    create_time,
  130.  
    </if>
  131.  
    <if test="updateTime != null">
  132.  
    update_time,
  133.  
    </if>
  134.  
    </trim>
  135.  
    <trim prefix="values (" suffix=")" suffixOverrides=",">
  136.  
    <if test="uid != null">
  137.  
    #{uid,jdbcType=BIGINT},
  138.  
    </if>
  139.  
    <if test="userName != null">
  140.  
    #{userName,jdbcType=VARCHAR},
  141.  
    </if>
  142.  
    <if test="passWord != null">
  143.  
    #{passWord,jdbcType=VARCHAR},
  144.  
    </if>
  145.  
    <if test="nickName != null">
  146.  
    #{nickName,jdbcType=VARCHAR},
  147.  
    </if>
  148.  
    <if test="mobile != null">
  149.  
    #{mobile,jdbcType=VARCHAR},
  150.  
    </if>
  151.  
    <if test="email != null">
  152.  
    #{email,jdbcType=VARCHAR},
  153.  
    </if>
  154.  
    <if test="createTime != null">
  155.  
    #{createTime,jdbcType=DATE},
  156.  
    </if>
  157.  
    <if test="updateTime != null">
  158.  
    #{updateTime,jdbcType=DATE},
  159.  
    </if>
  160.  
    </trim>
  161.  
    </insert>
  162.  
    <select id="countByExample" parameterType="com.caochenlei.mpdemo.pojo.SysUserDOExample" resultType="java.lang.Long">
  163.  
    select count(*) from user_base
  164.  
    <if test="_parameter != null">
  165.  
    <include refid="Example_Where_Clause" />
  166.  
    </if>
  167.  
    </select>
  168.  
    <update id="updateByExampleSelective" parameterType="map">
  169.  
    update user_base
  170.  
    <set>
  171.  
    <if test="record.uid != null">
  172.  
    uid = #{record.uid,jdbcType=BIGINT},
  173.  
    </if>
  174.  
    <if test="record.userName != null">
  175.  
    user_name = #{record.userName,jdbcType=VARCHAR},
  176.  
    </if>
  177.  
    <if test="record.passWord != null">
  178.  
    pass_word = #{record.passWord,jdbcType=VARCHAR},
  179.  
    </if>
  180.  
    <if test="record.nickName != null">
  181.  
    nick_name = #{record.nickName,jdbcType=VARCHAR},
  182.  
    </if>
  183.  
    <if test="record.mobile != null">
  184.  
    mobile = #{record.mobile,jdbcType=VARCHAR},
  185.  
    </if>
  186.  
    <if test="record.email != null">
  187.  
    email = #{record.email,jdbcType=VARCHAR},
  188.  
    </if>
  189.  
    <if test="record.createTime != null">
  190.  
    create_time = #{record.createTime,jdbcType=DATE},
  191.  
    </if>
  192.  
    <if test="record.updateTime != null">
  193.  
    update_time = #{record.updateTime,jdbcType=DATE},
  194.  
    </if>
  195.  
    </set>
  196.  
    <if test="_parameter != null">
  197.  
    <include refid="Update_By_Example_Where_Clause" />
  198.  
    </if>
  199.  
    </update>
  200.  
    <update id="updateByExample" parameterType="map">
  201.  
    update user_base
  202.  
    set uid = #{record.uid,jdbcType=BIGINT},
  203.  
    user_name = #{record.userName,jdbcType=VARCHAR},
  204.  
    pass_word = #{record.passWord,jdbcType=VARCHAR},
  205.  
    nick_name = #{record.nickName,jdbcType=VARCHAR},
  206.  
    mobile = #{record.mobile,jdbcType=VARCHAR},
  207.  
    email = #{record.email,jdbcType=VARCHAR},
  208.  
    create_time = #{record.createTime,jdbcType=DATE},
  209.  
    update_time = #{record.updateTime,jdbcType=DATE}
  210.  
    <if test="_parameter != null">
  211.  
    <include refid="Update_By_Example_Where_Clause" />
  212.  
    </if>
  213.  
    </update>
  214.  
    <update id="updateByPrimaryKeySelective" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
  215.  
    update user_base
  216.  
    <set>
  217.  
    <if test="userName != null">
  218.  
    user_name = #{userName,jdbcType=VARCHAR},
  219.  
    </if>
  220.  
    <if test="passWord != null">
  221.  
    pass_word = #{passWord,jdbcType=VARCHAR},
  222.  
    </if>
  223.  
    <if test="nickName != null">
  224.  
    nick_name = #{nickName,jdbcType=VARCHAR},
  225.  
    </if>
  226.  
    <if test="mobile != null">
  227.  
    mobile = #{mobile,jdbcType=VARCHAR},
  228.  
    </if>
  229.  
    <if test="email != null">
  230.  
    email = #{email,jdbcType=VARCHAR},
  231.  
    </if>
  232.  
    <if test="createTime != null">
  233.  
    create_time = #{createTime,jdbcType=DATE},
  234.  
    </if>
  235.  
    <if test="updateTime != null">
  236.  
    update_time = #{updateTime,jdbcType=DATE},
  237.  
    </if>
  238.  
    </set>
  239.  
    where uid = #{uid,jdbcType=BIGINT}
  240.  
    </update>
  241.  
    <update id="updateByPrimaryKey" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
  242.  
    update user_base
  243.  
    set user_name = #{userName,jdbcType=VARCHAR},
  244.  
    pass_word = #{passWord,jdbcType=VARCHAR},
  245.  
    nick_name = #{nickName,jdbcType=VARCHAR},
  246.  
    mobile = #{mobile,jdbcType=VARCHAR},
  247.  
    email = #{email,jdbcType=VARCHAR},
  248.  
    create_time = #{createTime,jdbcType=DATE},
  249.  
    update_time = #{updateTime,jdbcType=DATE}
  250.  
    where uid = #{uid,jdbcType=BIGINT}
  251.  
    </update>
  252.  
    <select id="selectByExampleWithRowbounds" parameterType="com.caochenlei.mpdemo.pojo.SysUserDOExample" resultMap="BaseResultMap">
  253.  
    select
  254.  
    <if test="distinct">
  255.  
    distinct
  256.  
    </if>
  257.  
    <include refid="Base_Column_List" />
  258.  
    from user_base
  259.  
    <if test="_parameter != null">
  260.  
    <include refid="Example_Where_Clause" />
  261.  
    </if>
  262.  
    <if test="orderByClause != null">
  263.  
    order by ${orderByClause}
  264.  
    </if>
  265.  
    </select>
  266.  
     
  267.  
    <select id="queryByPage" resultMap="BaseResultMap">
  268.  
    select * from user_base limit #{pageNo},#{pageSize}
  269.  
    </select>
  270.  
    </mapper>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhkigkh
系列文章
更多 icon
同类精品
更多 icon
继续加载