mybatisGenerator自动生成pojo、dao、xml文件

一、简介:

mybatisGenerator是一款自动生成文件工具、本文使用idea2017.3.1来进行操作。

二、文件生成之后的项目结构:

三、开始生成步骤:

1、使用idea生成maven的结构

在idea中点击 file-->new-->project后出现如下界面

依次点击 maven-->Create from archetype(选择下面的archtype-webapp结尾的名字)-->next

点击next之后会出现如下界面、按照下面方式输入项目名等继续next

点击next后会出现如下界面

将两个方框打上√、选择maven的配置文件以及本地仓库。之后继续 next

会出现如下界面、之后点击finish普通的maven项目基本结构就已经生成了。

2、在 src/main目录下面新建 java 文件夹与 resources 文件夹

并将java文件夹标记为资源文件夹(右键java文件夹-->MarkDirectory as-->Sources Root)

将resources文件夹标记为资源文件夹(右键resources文件夹-->MarkDirectory as -->Resources Root)

3、在resources目录下新建 datasource.properites

# mybatisGenerator工具存放位置
db.driverLocation=d:/workspace/ideaWorkspace/Test/mysql-connector-java-5.1.6-bin.jar
# 数据库驱动
db.driverClassName=com.mysql.jdbc.Driver
# 数据库连接
db.url=jdbc:mysql://127.0.0.1:3306/ate?characterEncoding=utf-8

db.username=root
db.password=123456

 在resources目录下新建 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 resource="datasource.properties"></properties>

    <!-- 指定数据库驱动的jdbc驱动jar包的位置 -->
    <classPathEntry location="${db.driverLocation}"/>

    <!-- context 是逆向工程的主要配置信息 -->
    <!-- id:起个名字 -->
    <!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">

        <!--optional,旨在创建class时,对注释进行控制-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接-->
        <jdbcConnection driverClass="${db.driverClassName}"
                        connectionURL="${db.url}"
                        userId="${db.username}"
                        password="${db.password}">
        </jdbcConnection>

        <!--非必须,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <!-- 默认情况下数据库中的 decimal,bigInt 在 Java 对应是 sql 下的 BigDecimal 类 -->
            <!-- 不是 doublelong 类型 -->
            <!-- 使用常用的基本类型代替 sql 包下的引用类型 -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- targetPackage:生成的实体类所在的包 -->
        <!-- targetProject:生成的实体类所在的硬盘位置 -->
        <javaModelGenerator targetPackage="com.mall.pojo"
                            targetProject="./src/main/java">
            <!-- 是否允许子包 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对modal添加构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
        <sqlMapGenerator targetPackage="com.mall.mappers"
                         targetProject="./src/main/java">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.mall.dao" targetProject="./src/main/java">
            <!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--tableName:数据库对应表明 domainObjectName:pojo类名字-->
        <table tableName="t_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

在pom.xml中的</pluginManagement>结尾标签中增加一段话

</pluginManagement>
        <plugins>
            <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>

4、将工具放入指定的位置,与 datasource.properties 设置的路径要对应

5、开始生成,在 IDEA 左下角点击小电脑按钮,IDEA 界面两边会出现插件,单机右边的 Maven Projects,会出现如下界面,点击绿色开始,文件就会自动生成。

6、附上工具连接:https://pan.baidu.com/s/1qItkv28Ax10O3xSHak2UTQ 密码:b0ff

原文地址:https://www.cnblogs.com/zhanzhuang/p/9598455.html