Mybatis Generator 代码生成器

开发环境介绍

  • 系统框架:Spring Boot
  • 数据库:MySql
  • 项目构建: Maven
  • 开发工具: Intellij Idea
  • 代码生成插件: mybatis-generator

引入Maven依赖

            <!--Mybatis代码自动生成器-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${org.mybatis.version}</version>
                <dependencies>
                    <dependency>
                        <groupId> mysql</groupId>
                        <artifactId> mysql-connector-java</artifactId>
                        <version>${mysql.connector.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>${org.mybatis.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 自动生成的配置 -->
                    <configurationFile>src/main/resources/generator/mybatis-generator.xml</configurationFile>
                </configuration>
            </plugin>

设置自动生成配置mybatis-generator.xml

<?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>
    <!-- defaultModelType定义了MBG如何生成实体类,flat表示只为每张表生成一个实体类-->
    <context id="MySqlContext" defaultModelType="flat" targetRuntime="Mybatis3" >
        <!-- 由于MBG生成的注释信息没有任何价值,而且有时间戳的情况下每次生成的注释都不一样,使用版本控制的时候每次都会提交,因而一般情况下都会屏蔽注释信息-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/sbframework" userId="root" password="dmw123456">
        </jdbcConnection>
        <!-- 用来指定JDBC类型和Java 类型如何转换-->
        <javaTypeResolver>
            <!-- 控制是否强制将DECIMAL 和 NUMERIC 类型的JDBC字段转换为Java类型的java.math.BigDecimal,默认值为false,一般不需要配置-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.chinaunicom.sbframework.entity" targetProject="src/main/java">
            <!-- 如果为true, MBG会根据catalog和schema来生成子包。如果为false就会直接使用targetPackage属性。默认为false-->
            <property name="enableSubPackages" value="true"/>
            <!-- 判断是否对数据库查询结果进行trim操作-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.chinaunicom.sbframework.orm.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 该标签用于配置Java客户端生成器(Mapper接口)的属性,如果不配置该标签,就不会生成Mapper接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.chinaunicom.sbframework.orm.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 生成全部的表 -->
        <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!--如果指定这个标签, MBG将在生成insert的SQL映射文件中插入一个selectKey -->
            <!--<generatedKey column="id" sqlStatement="MySql" identity="true" type="post"/>-->
        </table>
<!--
        <table tableName="sys_privilege" domainObjectName="SysPrivilege" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
-->
        </context>
</generatorConfiguration>

运行Maven mybatis-generator插件

在这里插入图片描述

运行结果

在这里插入图片描述

<?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.chinaunicom.sbframework.orm.mapper.HelloWorldMapper" >
  <resultMap id="BaseResultMap" type="com.chinaunicom.sbframework.entity.HelloWorld" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="hello_world" property="helloWorld" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, hello_world
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from hello_world
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from hello_world
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.chinaunicom.sbframework.entity.HelloWorld" >
    insert into hello_world (id, hello_world)
    values (#{id,jdbcType=INTEGER}, #{helloWorld,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.chinaunicom.sbframework.entity.HelloWorld" >
    insert into hello_world
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="helloWorld != null" >
        hello_world,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="helloWorld != null" >
        #{helloWorld,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.chinaunicom.sbframework.entity.HelloWorld" >
    update hello_world
    <set >
      <if test="helloWorld != null" >
        hello_world = #{helloWorld,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.chinaunicom.sbframework.entity.HelloWorld" >
    update hello_world
    set hello_world = #{helloWorld,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

Note

Mybatis Generator官网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值