IDEA基于maven项目使用mybatis-generator-plugin生成mapper和pojo

1、创建好相关的数据库和数据表(本例中使用的是MySql)

2、mybatis-generator使用配置

  • 打开pom.xml文件,添加3个依赖和mybatis-generator插件,分别是1.mybatis3.xjar包 2.逆向工程核心包 3.数据库连接包 4.log4j.jar用于输出日志
<?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.houtian</groupId>
    <artifactId>MybatisFirst</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <!--
                用maven mybatis插件
                如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
                在plugin外部得jar包,他不会去找到并执行,
                所以要把plugin运行依赖得jar配置都放在里面
                -->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>1.2.17</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                        <version>3.2.6</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.30</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>


</project>
View Code
  • 在src/main/resources包下创建逆向工程配置文件generatorConfig.xml,内容如下:
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 
 6 <generatorConfiguration>
 7     <context id="testTables" targetRuntime="MyBatis3">
 8         <commentGenerator>
 9             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
10             <property name="suppressAllComments" value="true" />
11         </commentGenerator>
12         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
13         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
14                         connectionURL="jdbc:mysql://localhost:3306/ego"
15                         userId="root"
16                         password="zheng">
17         </jdbcConnection>
18         <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
19             connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
20             userId="yycg"
21             password="yycg">
22         </jdbcConnection> -->
23 
24         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
25             NUMERIC 类型解析为java.math.BigDecimal -->
26         <javaTypeResolver>
27             <property name="forceBigDecimals" value="false" />
28         </javaTypeResolver>
29 
30         <!-- targetProject:生成PO类的位置 -->
31         <javaModelGenerator targetPackage="po"
32                             targetProject="src">
33             <!-- enableSubPackages:是否让schema作为包的后缀 -->
34             <property name="enableSubPackages" value="false" />
35             <!-- 从数据库返回的值被清理前后的空格 -->
36             <property name="trimStrings" value="true" />
37         </javaModelGenerator>
38         <!-- targetProject:mapper映射文件生成的位置 -->
39         <sqlMapGenerator targetPackage="mapper"
40                          targetProject="src">
41             <!-- enableSubPackages:是否让schema作为包的后缀 -->
42             <property name="enableSubPackages" value="false" />
43         </sqlMapGenerator>
44         <!-- targetPackage:mapper接口生成的位置 -->
45         <javaClientGenerator type="XMLMAPPER"
46                              targetPackage="mapper"
47                              targetProject="src">
48             <!-- enableSubPackages:是否让schema作为包的后缀 -->
49             <property name="enableSubPackages" value="false" />
50         </javaClientGenerator>
51         <!-- 指定数据库表 -->
52         <table tableName="tb_content"></table>
53         <table tableName="tb_order"></table>
54         <table tableName="tb_item_cat"></table>
55         <!-- <table schema="" tableName="sys_user"></table>
56         <table schema="" tableName="sys_role"></table>
57         <table schema="" tableName="sys_permission"></table>
58         <table schema="" tableName="sys_user_role"></table>
59         <table schema="" tableName="sys_role_permission"></table> -->
60 
61         <table tableName="tb_content" domainObjectName="WarningSetting" enableCountByExample="false" enableUpdateByExample="false"
62                enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
63 
64         <!-- 有些表的字段需要指定java类型
65          <table schema="" tableName="">
66             <columnOverride column="" javaType="" />
67         </table> -->
68     </context>
69 </generatorConfiguration>
View Code
  • 需要修改的地方:

  ① jdbcConnection,数据库的连接配置;

  ② javaModelGenerator,生成PO类的位置 ;

  ③ sqlMapGenerator,mapper映射文件生成的位置 ;

  ④ javaClientGenerator,mapper接口生成的位置 ;

  ⑤ table,其tableName属性对应数据库中相应表

3、点击IDEA右边的maven标签,选择mybatis-generator:generate,点击运行

4、IDEA自动生成po和mapper

原文地址:https://www.cnblogs.com/Anemia-BOY/p/11162744.html