Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类

Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类

转载至:https://blog.csdn.net/fishinhouse/article/details/82529338

1、File>Settings>Plugins,搜索mybatis-generator,默认没有安装。

2、点击Search in repositories链接,选择idea-mybatis-generator插件,右侧窗口点击install按钮,安装完成后提示重启IDEA。

3、配置idea-mybatis-generator插件

4、加号+新建maven,配置名称:MyBatis Generator,运行命令:mybatis-generator:generate -e

5、配置好后,运行项目选择:MyBatis Generator

6、 配置项目POM.xml文件(特别注意,这里一个坑,mybaits的plugin,必须在单独的plugins里面才会在右边的maven出来东西)

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/MyBatisGenerator.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

 

 

 

7、项目resources下要有MyBatisGenerator.xml文件

注:targetProject属性要配置为项目绝对路径,否则无法自动生成相关实体操作类。

 

IDEA的idea-mybatis-generator插件有个很大的BUG,自动生成的代码会覆盖mapper类中用户自定义接口。

 

其他参考文章:

https://www.cnblogs.com/heben/p/6088251.html

 

配置文件的样本如下:

<?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="C:/Users/Ash/.m2/repository/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar"/>
    <context id="productRelease">
        <property name="mergeable" value="false" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/springbankdb"
            userId="a" password="a" />
        <javaModelGenerator targetPackage="com.springbank.dao.generate.model"
            targetProject="springbank-dao" />
        <sqlMapGenerator targetPackage="com.springbank.dao.generate.mapper"
            targetProject="springbank-dao" />
        
        <javaClientGenerator targetPackage="com.springbank.dao.generate.mapper"
            targetProject="springbank-dao" type="XMLMAPPER" />
        <table tableName="owner" />
        <table tableName="acctrate" />
        <table tableName="acct" />
        <table tableName="acctstatus" />
        <table tableName="sysparam" />
        <table tableName="rtxn" />
        <table tableName="rate" />
        <table tableName="ratetype" />
        <table tableName="prod" />
        <table tableName="rtxntypgl" />
        <!-- <table tableName="scheduledjob" /> -->
    </context>
</generatorConfiguration>

 

 

其他优化的参看文档:结合lombok

https://www.jianshu.com/p/7f57be69bb94

posted on 2019-08-03 00:40  坚守梦想  阅读(8733)  评论(1编辑  收藏  举报