使用MyBatis Generator自动生成实体mapper和dao层

Posted 星朝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用MyBatis Generator自动生成实体mapper和dao层相关的知识,希望对你有一定的参考价值。

  通过MyBatis Generator可以自动生成实体、mapper和dao层,记录一下怎么用的。

主要步骤:  

  关于mybatis从数据库反向生成实体、DAO、mapper:
  参考文章:http://www.cnblogs.com/wangkeai/p/6934683.html
第一种方式:main方法运行(推荐)
  1.在pom.xml中加入插件依赖:
  2.写mbgConfiguration.xml文件,jdbc.properties文件
  3.写/SSM/src/main/java/main/GenMain.java main方法
  4.运行main函数,刷新

具体步骤:

  1.在pom.xml中加入插件依赖(需要mybatis的包和反向生成的包):

复制代码
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
复制代码

  2、写mbgConfiguration.xml文件,jdbc.properties文件

  mbgConfiguration.xml:里面的一些配置也是“因人而异”哦,需要适当修改。文件最后<table></table>就是对数据库中的表作的对应,具体可以看篇头的推荐文章或自己百度。

复制代码
<?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"
>
<!-- 第一种mybatis逆向生成xml配置 -->
<generatorConfiguration>
<properties resource="jdbc.properties" />
<context id="sqlserverTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 数据库链接URL、用户名、密码 </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">jdbcConnection </span><span style="color: #ff0000">driverClass</span><span style="color: #0000ff">="${jdbc.driver}"</span><span style="color: #ff0000">    </br>
                    connectionURL</span><span style="color: #0000ff">="${jdbc.url}"</span><span style="color: #ff0000">    </br>
                    userId</span><span style="color: #0000ff">="${jdbc.username}"</span><span style="color: #ff0000">    </br>
                    password</span><span style="color: #0000ff">="${jdbc.password}"</span><span style="color: #0000ff">&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">jdbcConnection</span><span style="color: #0000ff">&gt;</span>    </br></br>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">      </br>
    默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer    </br>
        true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal </br>     
    </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">javaTypeResolver</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="forceBigDecimals"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="false"</span> <span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">javaTypeResolver</span><span style="color: #0000ff">&gt;</span> </br></br>   

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">     </br>
    生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,    </br>
    也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下    </br>
    </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">&lt;javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN"&gt;</span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">javaModelGenerator </span><span style="color: #ff0000">targetPackage</span><span style="color: #0000ff">="com.yyc.entity"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">="./src/main/java"</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">/&gt;</span>    </br>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 从数据库返回的值被清理前后的空格  </span><span style="color: #008000">--&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="trimStrings"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">javaModelGenerator</span><span style="color: #0000ff">&gt;</span>    </br></br>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">对应的mapper.xml文件  </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">sqlMapGenerator </span><span style="color: #ff0000">targetPackage</span><span style="color: #0000ff">="mappers"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">="./src/main/resources"</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">sqlMapGenerator</span><span style="color: #0000ff">&gt;</span>    </br></br>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 对应的Mapper接口类文件 </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">javaClientGenerator </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="XMLMAPPER"</span><span style="color: #ff0000"> targetPackage</span><span style="color: #0000ff">="com.yyc.dao"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">="./src/main/java"</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">javaClientGenerator</span><span style="color: #0000ff">&gt;</span> </br></br>   


    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 列出要生成代码的所有表,这里配置的是不生成Example文件 </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">table </span><span style="color: #ff0000">tableName</span><span style="color: #0000ff">="user_t"</span><span style="color: #ff0000"> domainObjectName</span><span style="color: #0000ff">="UserInfo"</span><span style="color: #ff0000">    </br>
           enableCountByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> enableUpdateByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> enableDeleteByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000">    </br>
           enableSelectByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> selectByExampleQueryId</span><span style="color: #0000ff">="false"</span> <span style="color: #0000ff">&gt;</span>    
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="useActualColumnNames"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="false"</span><span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">table</span><span style="color: #0000ff">&gt;</span>    </br>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">context</span><span style="color: #0000ff">&gt;</span>    </br>

</generatorConfiguration>

复制代码

  由于上面的文件中使用了数据库连接,所以还别忘了数据库的包(相信在准备好学习mybatis前,已经在pom.xml文件中准备好了这些相关依赖)

  jdbc.properties: 

复制代码
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url
=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username
=root
jdbc.password
=123
#定义初始连接数
initialSize
=0
#定义最大连接数
maxActive
=20
#定义最大空闲
maxIdle
=20
#定义最小空闲
minIdle
=1
#定义最长等待时间
maxWait
=60000
复制代码

  3.写GenMain.java main方法

复制代码
/**
 * 
 */
package main;

import java.io.File;

import java.io.IOException;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;

import org.mybatis.generator.config.Configuration;

import org.mybatis.generator.config.xml.ConfigurationParser;

import org.mybatis.generator.exception.InvalidConfigurationException;

import org.mybatis.generator.exception.XMLParserException;

import org.mybatis.generator.internal.DefaultShellCallback;

/**********************************

  • @author E-mail:34782655@qq.com
  • @version 创建时间:2017年6月23日 下午9:18:19
  • 类说明:
  • mybatis逆向工程main函数</br>
    

**************************************

/

public class GenMain {

public static void main(String[] args) {

List
<String> warnings = new ArrayList<String>();

boolean overwrite = true;
     //如果这里出现空指针,直接写绝对路径即可。

String genCfg
= "/mbgConfiguration.xml";

File configFile
= new File(GenMain.class.getResource(genCfg).getFile());

ConfigurationParser cp
= new ConfigurationParser(warnings);

Configuration config
= null;

try {

config
= cp.parseConfiguration(configFile);

}
catch (IOException e) {

e.printStackTrace();

}
catch (XMLParserException e) {

e.printStackTrace();

}

DefaultShellCallback callback
= new DefaultShellCallback(overwrite);

MyBatisGenerator myBatisGenerator
= null;

try {

myBatisGenerator
= new MyBatisGenerator(config, callback, warnings);

}
catch (InvalidConfigurationException e) {

e.printStackTrace();

}

try {

myBatisGenerator.generate(
null);

}
catch (SQLException e) {

e.printStackTrace();

}
catch (IOException e) {

e.printStackTrace();

}
catch (InterruptedException e) {

e.printStackTrace();

}

}

}

复制代码

  4、运行Main方法,刷新项目,就会在上面mbgConfiguration.xml中配置的对应文件的目录下生成实体,mapper,dao了。

第二种方式:maven插件运行

  1.pom.xml配置插件

  2.写generatorConfig.xml文件

  3.运行

具体步骤:  

  1.pom.xml配置插件build节点下加入插件:也就是maven一般加入tomcat插件的地方。

复制代码
<!-- 第二种方式数据库反向生成java -->
<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>
复制代码

  2.写generatorConfig.xml文件,与mbgConfiguration.xml文件不同的是:文件名固定只能是generatorConfig,文件中要配置数据库连接驱动绝对路径,并且,数据库连接信息url,username,password等都要写固定<classPathEntry location="E:\\MavenRepository\\mysql\\mysql-connector-java\\5.1.38\\mysql-connector-java-5.1.38.jar" />

  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"
>
<!-- 第二种mybatis逆向生成xml配置 -->
<generatorConfiguration>
<!-- 需要指明数据库连接器的绝对路径 -->
<classPathEntry
location="E:\\MavenRepository\\mysql\\mysql-connector-java\\5.1.38\\mysql-connector-java-5.1.38.jar" />
<context id="sqlserverTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 数据库链接URL、用户名、密码 </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">jdbcConnection </span><span style="color: #ff0000">driverClass</span><span style="color: #0000ff">="com.mysql.jdbc.Driver"</span><span style="color: #ff0000">    </br>
                    connectionURL</span><span style="color: #0000ff">="jdbc:mysql://localhost:3306/ssm"</span><span style="color: #ff0000">    </br>
                    userId</span><span style="color: #0000ff">="root"</span><span style="color: #ff0000">    </br>
                    password</span><span style="color: #0000ff">="123cy-coo.com"</span><span style="color: #0000ff">&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">jdbcConnection</span><span style="color: #0000ff">&gt;</span></br></br>    

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">      </br>
    默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer    </br>
        true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal   </br>   
    </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">javaTypeResolver</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="forceBigDecimals"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="false"</span> <span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">javaTypeResolver</span><span style="color: #0000ff">&gt;</span>    </br></br>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">     </br>
    生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java, </br>   
    也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下    </br>
    </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">&lt;javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN"&gt;</span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">javaModelGenerator </span><span style="color: #ff0000">targetPackage</span><span style="color: #0000ff">="com.yyc.entity"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">="./src/main/java"</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">/&gt;</span>    </br>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 从数据库返回的值被清理前后的空格  </span><span style="color: #008000">--&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="trimStrings"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/&gt;</span>  </br>  
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">javaModelGenerator</span><span style="color: #0000ff">&gt;</span>    </br></br>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">对应的mapper.xml文件  </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">sqlMapGenerator </span><span style="color: #ff0000">targetPackage</span><span style="color: #0000ff">="mappers"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">="./src/main/resources"</span><span style="color: #0000ff">&gt;</span>    </br>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">/&gt;</span>    
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">sqlMapGenerator</span><span style="color: #0000ff">&gt;</span>    </br></br>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 对应的Mapper接口类文件 </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">javaClientGenerator </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="XMLMAPPER"</span><span style="color: #ff0000"> targetPackage</span><span style="color: #0000ff">="com.yyc.dao"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">="./src/main/java"</span><span style="color: #0000ff">&gt;</span>    
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">javaClientGenerator</span><span style="color: #0000ff">&gt;</span>    </br></br>


    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 列出要生成代码的所有表,这里配置的是不生成Example文件 </span><span style="color: #008000">--&gt;</span>    </br>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">table </span><span style="color: #ff0000">tableName</span><span style="color: #0000ff">="user_t"</span><span style="color: #ff0000"> domainObjectName</span><span style="color: #0000ff">="UserInfo"</span><span style="color: #ff0000">    </br>
           enableCountByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> enableUpdateByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> enableDeleteByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000">    </br>
           enableSelectByExample</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> selectByExampleQueryId</span><span style="color: #0000ff">="false"</span> <span style="color: #0000ff">&gt;</span>    
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="useActualColumnNames"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="false"</span><span style="color: #0000ff">/&gt;</span>    </br>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">table</span><span style="color: #0000ff">&gt;</span></br>    
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">context</span><span style="color: #0000ff">&gt;</span> </br>   

</generatorConfiguration>

复制代码

   3.运行方法:在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate  运行即可。

因为第一种xml文件的配置比较灵活,不像第二种xml中还需要写死数据库驱动地址和连接信息,并且还要多加一个jar插件。所以推荐第一种方式

以上是关于使用MyBatis Generator自动生成实体mapper和dao层的主要内容,如果未能解决你的问题,请参考以下文章

使用MyBatis Generator自动生成实体mapper和dao层

Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类

mybatis逆向工程--自动生成实体代码(mybatis-generator)

springboot 里mybatis generator逆向工程生成mapper 实体类

IDEA——mybatis-generator插件自动生成实体代码(Maven)

使用maven的mybatis-generator代码生成器插件生成实体类mapper配置文件和mapper接口(使用idea)