美文网首页
mybatis-generator 在springboot中自动

mybatis-generator 在springboot中自动

作者: JackSpeed | 来源:发表于2020-03-18 23:31 被阅读0次

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.执行命令构建

相关文章

网友评论

      本文标题:mybatis-generator 在springboot中自动

      本文链接:https://www.haomeiwen.com/subject/cgehyhtx.html