Idea搭建mybatis-generator

1、springboot 2.0 + mybatis+web + mysql +jdbc 搭建项目

2、pom.xml中引入mybatis-generator-maven-plugin,只需配置第二个plugin。之后会在Maven Projects的Plugins下自动生成一个mybatis-generator插件,配置generatorConfig.xml后双击该插件即可自动生成相关代码。

<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.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>

    </build>

3、在resources文件夹下创建“generatorConfig.xml”

注意:

(1)提前建好contoller、model、dao、service以及resources的子目录mapper;

(2)classPathEntry配置的是本地mysql驱动包路径;

(3)mapper的xml映射文件自动生成时我移到了resources/mapper目录下;

  (4)  model、dao和mapper代码的生成路径别配错了;

(5)table可以配置一或多个,主要是配置tablename和domainObjectName;

<?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:/Maven/mysql-connector-java-5.1.46.jar" />
    <context id="test" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <commentGenerator>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should
             force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 文件夹自己定义-->
        <javaModelGenerator targetPackage="com.mm.spboot.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 文件夹自己定义-->
        <sqlMapGenerator targetPackage="mapper"
                           targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 文件夹自己定义-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.mm.spboot.dao"
                             implementationPackage="com.mm.spboot.dao.impl"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成哪些表 -->
        <table tableName="t_user" domainObjectName="user"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

4、双击mabitis-generator插件,代码会生成到相应的位置。

猜你喜欢

转载自blog.csdn.net/csmnjk/article/details/84191291
今日推荐