Mybatis逆向工程

MyBatis逆向工程

一. 作用

自动配置实体类,dao,xml

Maven --集成逆向工程插件完成项目开发

  • 如果项目中业务涉及到表很多
  • 表中字段很多

使用mybatis逆向工程步骤:

1.准备测试的数据库表

t_user,t_type,t_goods

2. 引入mybatis逆向工程的依赖

<!--Mybatis generator-->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.2</version>
</dependency>

3.引入mybatis逆向工程的插件配置

<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
            <configuration>
                <configurationFile>src/main/resources/mbg.xml</configurationFile>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
            </configuration>
            <executions>
                <execution>
                    <id>Generate MyBatis Artifacts</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

4.引入mybatis generator逆向工程的配置文件mgb.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>

    <!-- 数据库驱动包位置 -->
    <classPathEntry location="D:\locarepo\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar" />

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

        <!-- 解决覆盖生成 XML 文件的 Bug -->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

        <commentGenerator>
            <!-- 去掉注解中的生成日期 -->
            <property name="suppressDate" value="true"/>
            <!--关闭注释 -->
            <property name="suppressAllComments" value="true"/>

        </commentGenerator>

        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/woniu_test?serverTimezone=Asia/Shanghai&amp;characterEncoding=UTF-8"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <javaTypeResolver>
            <!--
            true: 无论什么情况,都是使用 BigDecimal 对应 DECIMAL 和 NUMERIC 数据类型
            false: 默认值,
                scale>0;length>18:使用 BigDecimal;
                scale=0;length[10,18]:使用 Long;
                scale=0;length[5,9]:使用 Integer;
                scale=0;length<5:使用 Short;
            -->
            <property name="forceBigDecimals" value="true"/>
        </javaTypeResolver>

        <!-- PO 类的相关设置 -->
        <javaModelGenerator targetProject="src/main/java"
                            targetPackage="com.woniuxy.goods.entity">
            <!-- 在 targetPackage 的基础上,根据数据库的 schema 再生成一层 package,最终生成的类放在这个 package下,默认为false  -->
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- Mapper.xml 配置文件的相关设置 -->
        <sqlMapGenerator targetProject="src/main/resources"
                         targetPackage="mapper">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- DAO 接口的相关设置-->
        <javaClientGenerator targetProject="src/main/java"
                             targetPackage="com.woniuxy.goods.dao"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--生成对应表及类名-->
        <!--
             schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类
             更改tableName和domainObjectName就可以
        -->
        <table tableName="t_user" schema="woniu_test"
               domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
        />
        <table tableName="t_type" schema="woniu_test"
               domainObjectName="Type"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
        />
        <table tableName="t_goods" schema="woniu_test"
               domainObjectName="Goods"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
        />
    </context>
</generatorConfiguration>

5.执行插件

直接在 Idea Maven窗口双击mybatis-generator:generate

或

运行-->Run As -->Maven build-->Goals填写mybatis-generator:generate--Run

三、lombok插件(了解)

  • lombok插件针对实体类的操作,提供常用的方法,在实体类编译自动生成到 class文件 中.

  • 比如: getter/setter,构造器,toString,hashCode,equals()

  • 在编译器生成getter…

  • 使用步骤:

    (1) 安装lombok插件
              --通过idea setting中的plugins进行查找安装
              
    (2)  引入pom.xml文件中添加lombok依赖
     <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
            </dependency>
    (3)在实体类中添加注解,生成相应实体类方法
    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    @Getter
    @Setter
    @EqualsAndHashCode
    public class User {
        private Integer userid;
        private String username;
        private String password;
    
        public static void main(String[] args) {
            User user = new User(1,"jack","123");
            System.out.println(user.toString());
        }
    }    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值