mybatis逆向工程

2020-03-26 17:43:19 浏览数 (1)

pom文件引入插件

代码语言:javascript复制
<plugin>
    <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <!--配置文件的位置-->
        <configurationFile>src/main/resources/config/mybatis-generator-config.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <phase>deploy</phase>
            <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>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
    </dependencies>
</plugin>

新建mybatis-geneerator-config.xml

代码语言:javascript复制
<?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="BaseDataTables" targetRuntime="MyBatis3">
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库连接参数 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" userId="username"
                        password="password" />
        <!--把JDBC DECIMAL 和  NUMERIC 类型解析为java.math.BigDecimal. (默认false,即把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer ) -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- entity存放目录 -->
        <javaModelGenerator targetPackage="com.shaojie.runoob.dao.entity" targetProject="src/main/java" />
        <!-- mappers xml存放目录 -->
        <sqlMapGenerator targetPackage="com.shaojie.runoob.dao.mapper" targetProject="src/main/java" />
        <!-- mapper接口存放目录  -->
        <javaClientGenerator targetPackage="com.shaojie.runoob.dao.mapper" targetProject="src/main/java" type="XMLMAPPER" />

        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
        <table tableName="user" >
            <generatedKey column="id" sqlStatement="Mysql" identity="true" type="post"/>
        </table>

        </context>
    </generatorConfiguration>

数据库test 表user

然后双击即可

0 人点赞