使用 Mybatis Generator 生成mybatis所需要的配置代码

1、导入依赖包

在maven中添加依赖

<dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.7</version>
</dependency>

在maven插件中添加

<plugins>
   <!--mybatis逆向工程-->
   <plugin>
       <groupId>org.mybatis.generator</groupId>
       <artifactId>mybatis-generator-maven-plugin</artifactId>
       <version>1.3.5</version>
       <dependencies>
           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <version>8.0.17</version>
           </dependency>
       </dependencies>
       <configuration>
           <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
           <overwrite>true</overwrite>
       </configuration>
   </plugin>
</plugins>

完成后插件中会有mybatis-generator
在这里插入图片描述

2、编写generatorConfig.xml文件

在resources中新建generator文件夹然后新建generatorConfig.xml

<?xml version="1.0"?>
<!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="DB2Table" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
    	<!-- 是否去除自动生成的注释 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
                <!--数据库连接配置-->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/cms"
                userId="root"
                password="root">
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>
        <!--实体类生成位置-->
        <javaModelGenerator targetPackage="com.yhq.entity"
                            targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--mybatis映射xml文件的位置-->
        <sqlMapGenerator targetPackage="mapper2" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--mapper接口生成的位置-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.yhq.mapper2"
                             targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--配置需要生成的表的信息-->
        <table tableName="t_article" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="user" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

3、使用插件生成代码

在这里插入图片描述
实体类
在这里插入图片描述
mapper接口
在这里插入图片描述
mapper.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.yhq.mapper2.TArticleMapper">
  <resultMap id="BaseResultMap" type="com.yhq.entity.TArticle">
    <id column="aid" jdbcType="INTEGER" property="aid" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="content" jdbcType="VARCHAR" property="content" />
    <result column="author" jdbcType="VARCHAR" property="author" />
    <result column="source" jdbcType="VARCHAR" property="source" />
    <result column="createTime" jdbcType="TIMESTAMP" property="createtime" />
    <result column="cid" jdbcType="INTEGER" property="cid" />
  </resultMap>
  <sql id="Base_Column_List">
    aid, title, content, author, source, createTime, cid
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_article
    where aid = #{aid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_article
    where aid = #{aid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.yhq.entity.TArticle">
    insert into t_article (aid, title, content, 
      author, source, createTime, 
      cid)
    values (#{aid,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, 
      #{author,jdbcType=VARCHAR}, #{source,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, 
      #{cid,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.yhq.entity.TArticle">
    insert into t_article
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="aid != null">
        aid,
      </if>
      <if test="title != null">
        title,
      </if>
      <if test="content != null">
        content,
      </if>
      <if test="author != null">
        author,
      </if>
      <if test="source != null">
        source,
      </if>
      <if test="createtime != null">
        createTime,
      </if>
      <if test="cid != null">
        cid,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="aid != null">
        #{aid,jdbcType=INTEGER},
      </if>
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        #{content,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        #{author,jdbcType=VARCHAR},
      </if>
      <if test="source != null">
        #{source,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null">
        #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="cid != null">
        #{cid,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.yhq.entity.TArticle">
    update t_article
    <set>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        author = #{author,jdbcType=VARCHAR},
      </if>
      <if test="source != null">
        source = #{source,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null">
        createTime = #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="cid != null">
        cid = #{cid,jdbcType=INTEGER},
      </if>
    </set>
    where aid = #{aid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yhq.entity.TArticle">
    update t_article
    set title = #{title,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR},
      author = #{author,jdbcType=VARCHAR},
      source = #{source,jdbcType=VARCHAR},
      createTime = #{createtime,jdbcType=TIMESTAMP},
      cid = #{cid,jdbcType=INTEGER}
    where aid = #{aid,jdbcType=INTEGER}
  </update>
</mapper>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值