美文网首页
springboot+mybaits自动生成dto/mapper

springboot+mybaits自动生成dto/mapper

作者: 邵云涛 | 来源:发表于2018-04-25 13:12 被阅读61次

从eclipse转战idea,但发现没有mybatis插件自动生成代码工具,甚是烦恼,后发现可以pom导入插件实现,具体实现方法如下:
springboot pom.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>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

generatorConfig.xml配置如下:建议targetProject写绝对路径

<?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>
    <!--导入属性配置 -->
    <properties resource="generator.properties"/>
    <classPathEntry location="${jdbc.driverLocation}"/>
    <context id="context1">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}"
            userId="${jdbc.userId}" password="${jdbc.password}"/>
        <javaModelGenerator targetPackage="${mybatis.model}"
            targetProject="E:\workspaceForZNYY\public_service\iop-common\src\main\java">
            <property name="enableSubPackages" value="false"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="${mybatis.xml}" targetProject="E:\workspaceForZNYY\bocommlife-iop\iop-mq-service\src\main\resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <javaClientGenerator targetPackage="${mybatis.mapper}"
            targetProject="E:\workspaceForZNYY\bocommlife-iop\iop-mq-service\src\main\java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <table tableName="testtable" domainObjectName="TestTable">
        </table>
   </context>
</generatorConfiguration>

generatorConfig.xml导入属性配置 <properties resource="generator.properties"/>
generator.properties配置如下:

jdbc.driverLocation=D:/mysql-connector.jar
#jdbc.driverLocation=E:/Database Driver/MySQL Driver/mysql-connector-java-5.1.19.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/test
jdbc.userId=root
jdbc.password=mysql
mybatis.model=com.shaoytsh.mq.mybaitsDto
mybatis.mapper=com.shaoytsh.mq.mapper.mybatisMapper
mybatis.xml=mybatis.sqlmap
最后

idea中添加一个generator插件运行选项。
首先,点击runEdit Configurations...如下图

image.png

在弹出的窗口中点击“+”号,选择Maven,填写Name和Command line,如下图

image.png

点击ok完成设置。
点击Run generator,就会自动生成代码。
大功告成!!!

相关文章

网友评论

      本文标题:springboot+mybaits自动生成dto/mapper

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