mybatis-generator 在springboot中自动生成dao、sqlxml、实体类

mybatis generator官网:http://mybatis.org/generator/index.html
整篇文档分为

  1. 建表
  2. 新建springboot项目,根据mybatis generator官网文档配置pom
  3. 配置generatorConfig.xml
  4. 执行构建命令
  5. 查看结果

Let's start coding

  1. 建表语句
create table t_consume_type
(
    id          int auto_increment
        primary key,
    type_name   varchar(100)     null,
    type_value  int(2)           null,
    create_time datetime         null,
    update_time datetime         null,
    is_delete   char default '0' null
);

create table t_user_group
(
    id          int auto_increment
        primary key,
    group_id    int              null,
    group_name  varchar(100)     null,
    is_delete   char default '0' null comment '0正常 1删除',
    create_date datetime         null,
    update_time datetime         null,
    need_invite char default '0' null comment '邀请码  0不需要,1需要',
    invite_code varchar(50)      null comment '邀请码'
)
    charset = utf8;

create table t_invite_code
(
    id          int auto_increment
        primary key,
    invite_code varchar(20) null comment '邀请码',
    is_delete   char        null comment '是否删除0否1是'
)
    charset = utf8;

create table t_order_clear
(
    id           int auto_increment
        primary key,
    payed        decimal(10, 2) default 0.00 null comment '支出',
    user_id      varchar(30)                 not null comment '用户id',
    order_result decimal(10, 2) default 0.00 null comment '结算结果',
    order_date   datetime                    null comment '结算时间',
    avg_pay      decimal(10, 2) default 0.00 null comment '人均支出',
    order_no     varchar(20)                 not null comment '结算清单编号',
    create_date  datetime                    null
);

create table t_user
(
    id                   int auto_increment
        primary key,
    user_id              varchar(30)      not null comment '用户编号',
    group_id             int              null comment '分组id',
    user_type            char default '0' null comment '用户类型:0普通,1管理员',
    user_name            varchar(100)     null comment '用户名称',
    nick_name            varchar(100)     null comment '昵称',
    mobile               varchar(20)      null comment '手机号码',
    password             varchar(200)     null comment '密码',
    alipay_auth_code     varchar(200)     null comment '支付宝授权码',
    is_delete            char default '0' null comment '0正常 1删除',
    create_date          datetime         null,
    update_date          datetime         null,
    avatar               varchar(500)     null comment '头像地址',
    ignore_consume_types varchar(20)      null comment '结算时忽略的消费类型,多个类型用英文逗号隔开'
);

create table t_user_pay
(
    id            bigint(11) auto_increment comment '主键'
        primary key,
    user_id       varchar(30)                    not null comment '用户id',
    is_delete     char charset utf8 default '0'  null comment '状态 0正常 1删除',
    consume_date  datetime                       null comment '消费时间',
    payed         decimal(10, 2)    default 0.00 null comment '消费金额',
    consume_type  char                           null comment '消费类型,0 水费,1 其他',
    create_date   datetime                       null comment '创建时间',
    update_date   datetime                       null comment '修改时间',
    remarks       varchar(600)                   null,
    settle_status char              default '0'  null comment '是否已经结算,0否,1是'
);


  1. 新建springboot项目,按常规选择web、数据库连接等需要的依赖配置好pom文件
    2.1. 进入mybatis generator官网找到Running MyBatis Generator With Maven

    2.2. 选择maven这种方式。然后把代码复制到pom文件的插件配置里面去。
           <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
  1. 配置generatorConfig.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>
<!--  构建命令:mvn mybatis-generator:generate  -->
   <!--    windows下路径和Mac路径不一样,需要自己替换-->
   <classPathEntry
           location="C:\Users\admin\.m2\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar"/>

   <context id="DB2Tables" targetRuntime="MyBatis3">

       <!--        不再追加xml内容-->
       <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>

       <commentGenerator>
           <property name="suppressAllComments" value="true"/>
       </commentGenerator>
       <!-- -->
       <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                       connectionURL="jdbc:mysql://127.0.0.1:3306/test?useSSL=FALSE"
                       userId="root"
                       password="123456">
       </jdbcConnection>

       <javaTypeResolver>
           <property name="forceBigDecimals" value="false"/>
       </javaTypeResolver>

       <javaModelGenerator targetPackage="com.ycj.mall.model" targetProject="src/main/java">
           <property name="enableSubPackages" value="true"/>
           <!--            <property name="trimStrings" value="true" />-->
       </javaModelGenerator>

       <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
           <property name="enableSubPackages" value="true"/>
       </sqlMapGenerator>

       <javaClientGenerator type="XMLMAPPER" targetPackage="com.ycj.mall.mapper" targetProject="src/main/java">
           <property name="enableSubPackages" value="true"/>
       </javaClientGenerator>

       <table tableName="t_consume_type" domainObjectName="consumeType" enableCountByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
       <table tableName="t_user_group" domainObjectName="UserGroup" enableCountByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
       <table tableName="t_invite_code" domainObjectName="InviteCode" enableCountByExample="false" enableDeleteByExample="false"
              enableSelectByExample="false" enableUpdateByExample="false"/>
       <table tableName="t_order_clear" domainObjectName="OrderClear" enableCountByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
       <table tableName="t_user" domainObjectName="MUser" enableCountByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
       </table>
       <table tableName="t_user_pay" domainObjectName="UserPay" enableCountByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
   </context>
</generatorConfiguration>
  1. 在控制台执行构建命令
mvn mybatis-generator:generate
image.png
  1. 查看结果
    自动生成了dao、sql、xml、实体类


    image.png

    image.png

一般配置已经oh了,更高级的可以看官方文档做修改

最后附上pom文件配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ycj</groupId>
    <artifactId>jackmall</artifactId>
    <version>1.0</version>
    <name>jackmall</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

4.执行命令构建

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,110评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,443评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,474评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,881评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,902评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,698评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,418评论 3 419
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,332评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,796评论 1 316
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,968评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,110评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,792评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,455评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,003评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,130评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,348评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,047评论 2 355

推荐阅读更多精彩内容