美文网首页
IDEA使用mybatis-generator

IDEA使用mybatis-generator

作者: 努力耕耘少问收获 | 来源:发表于2019-11-16 00:13 被阅读0次

环境版本

-mysql 5.7.20
-springboot 2.1.10.RELEASE
-jdk 1.8
1.新建项目
QQ截图20191115235129.png
2.在pom文件的<build>下的<plugins>添加以下配置
QQ截图20191115235307.png
<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.5</version>
                <configuration>
                    <!-- 在控制台打印执行日志 -->
                    <verbose>true</verbose>
                    <!-- 重复生成时会覆盖之前的文件-->
                    <overwrite>true</overwrite>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
                <!-- 数据库连接选择5.7.20以上的,因为用的mysql5.7.20-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.17</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
3.在项目中新建包
QQ截图20191115235621.png
4.在mysql中建立测试数据库wg_inset
QQ截图20191115235743.png
5.在resources下创建generatorConfig.xml
QQ截图20191116000058.png
QQ截图20191116000406.png
6.配置文件躯体内容如下
<?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>
    <!-- context 是逆向工程的主要配置信息 -->
    <!-- id:起个名字 -->
    <!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">
        <!--optional,指在创建class时,对注释进行控制-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--jdbc的数据库连接 wg_insert 为数据库名字-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/wg_insert?useUnicode=true&amp;characeterEncoding=utf-8&amp;serverTimezone=UTC"
                        userId="root"
                        password="123456"></jdbcConnection>
        <!--非必须,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <!-- 默认情况下数据库中的 decimal,bigInt 在 Java 对应是 sql 下的 BigDecimal 类 -->
            <!-- 不是 double 和 long 类型 -->
            <!-- 使用常用的基本类型代替 sql 包下的引用类型 -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- targetPackage:生成的实体类所在的包 -->
        <!-- targetProject:生成的实体类所在的硬盘位置 -->
        <javaModelGenerator targetPackage="com.example.mybatisgenerator.entity"
                            targetProject="src/main/java">
            <!-- 是否允许子包 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对modal添加构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>
        <!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.mybatisgenerator.dao" targetProject="src/main/java">
            <!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!-- tableName是数据库中的表名,domainObjectName是生成的JAVA模型名,后面的参数不用改,要生成更多的表就在下面继续加table标签 -->
        <table tableName="student" domainObjectName="Student"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <!-- tableName是数据库中的表名,domainObjectName是生成的JAVA模型名,后面的参数不用改,要生成更多的表就在下面继续加table标签 -->
        <table tableName="post" domainObjectName="Post"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>
7.跑起来看是否成功
QQ截图20191116000745.png
8.在IDEA控制台看输出信息
"D:\Program Files\Java\jdk1.8.0_172\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\work\kaiyuan\work\mybatisgenerator "-Dmaven.home=D:\Program Files\tool\apache-maven-3.5.2" "-Dclassworlds.conf=D:\Program Files\tool\apache-maven-3.5.2\bin\m2.conf" -javaagent:C:\Users\Administrator\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\191.6183.87\lib\idea_rt.jar=54437:C:\Users\Administrator\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\191.6183.87\bin -Dfile.encoding=UTF-8 -classpath "D:\Program Files\tool\apache-maven-3.5.2\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1 -s "D:\Program Files\tool\apache-maven-3.5.2\setting.xml" org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mybatisgenerator 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ mybatisgenerator ---
[INFO] Connecting to the Database
[INFO] Introspecting table student
[INFO] Introspecting table post
[INFO] Generating Record class for table student
[INFO] Generating Mapper Interface for table student
[INFO] Generating SQL Map for table student
[INFO] Saving file StudentMapper.xml
[INFO] Saving file Student.java
[INFO] Saving file StudentMapper.java
[WARNING] Table configuration with catalog null, schema null, and table post did not resolve to any tables
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.410 s
[INFO] Finished at: 2019-11-15T23:39:17+08:00
[INFO] Final Memory: 14M/155M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0
9.查看生成的文件
QQ截图20191116001049.png

到此springboot快速生成mybatis所需要的实体,dao接口,以及xml文件完成了!

相关文章

网友评论

      本文标题:IDEA使用mybatis-generator

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