springboot集成mybatis-generator

Mybatis generate简称MBG(后文均采用MBG来简称),是Mybatis官方维护的逆向生成代码工具,本人工作中的项目中,也有使用到这个工具。

1.新建MAVEN项目,推荐去

spring initializr

自动生成代码包

其中POM文件为

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.umrx</groupId>
    <artifactId>mbg</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mbg</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

    <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.6</version>
            </plugin>
        </plugins>
    </build>


</project>

2.在项目的resource文件夹下新建generatorConfig.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>
    <!-- 配置文件路径
    <properties url="${mybatis.generator.generatorConfig.properties}"/>-->

    <!--数据库驱动包路径,其实这个地方可以填你本地Maven仓库下载的mysql-connector-java包,因为只是在本地开发环境生成代码而已,并不用考虑测试环境和生产环境-->
    <classPathEntry location="D:/a_JAVA/apache-maven-3.5.0/repository-springcloud/mysql\mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar"/>

<context id="MySql" targetRuntime="MyBatis3"> <!--关闭注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mbg" userId="root" password="123456"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成的model 包路径 --> <javaModelGenerator targetPackage="com.umrx.mbg.model" targetProject="src/main/java"></javaModelGenerator> <!--生成xml mapper文件 路径 --> <sqlMapGenerator targetPackage="com.umrx.mbg.mapper" targetProject="src/main/java"> </sqlMapGenerator> <!-- 生成的Dao接口 的包路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.umrx.mbg.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="ture"/> </javaClientGenerator> <table tableName="student"></table> </context> </generatorConfiguration>

其中要注意一点的是:直接引用

http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd

会变成红色,对于所有的dtd变成红色的方法,可以参考下面一篇博文:

intellij idea配置外部dtd文件

直接点击

http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd

下载,并按照上面那一篇博客配置就好了

最后使用maven运行插件就可以自动生成代码了:

 

 





 

posted @ 2018-07-31 22:37  uMBrELlAmRx  阅读(608)  评论(0编辑  收藏  举报