首页 > 技术文章 > mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

fanshuyao 2018-03-14 10:13 原文

mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

 

==============================

蕃薯耀 2018年3月14日

http://www.cnblogs.com/fanshuyao/

 

一、使用Maven方式引入Mybatis依赖Jar包(版本号自己改或定义)

Xml代码  收藏代码
  1. <properties>  
  2.     <spring.version>4.3.13.RELEASE</spring.version>  
  3.           
  4.     <mybatis.version>3.4.6</mybatis.version>  
  5.     <mybatis.spring.version>1.3.1</mybatis.spring.version>                  
  6.     <mybatis.generator.version>1.3.6</mybatis.generator.version>  
  7.           
  8.     <junit.version>4.12</junit.version>  
  9. </properties>  

 

 

Xml代码  收藏代码
  1. <!-- mybatis-->  
  2.         <dependency>  
  3.             <groupId>org.mybatis</groupId>  
  4.             <artifactId>mybatis</artifactId>  
  5.             <version>${mybatis.version}</version>  
  6.         </dependency>  
  7.         <dependency>  
  8.             <groupId>org.mybatis</groupId>  
  9.             <artifactId>mybatis-spring</artifactId>  
  10.             <version>${mybatis.spring.version}</version>  
  11.         </dependency>  
  12.         <dependency>  
  13.             <groupId>org.mybatis.generator</groupId>  
  14.             <artifactId>mybatis-generator-core</artifactId>  
  15.             <version>${mybatis.generator.version}</version>  
  16.             <scope>provided</scope>  
  17.         </dependency>  

 

二、加入文件生成xml配置文件

直接在项目文件夹下建立xml配置文件:mybatis-generator.xml,内容如下:

 

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. <!-- 配置文件信息见:http://www.mybatis.org/generator/configreference/xmlconfig.html -->  
  7. <generatorConfiguration>  
  8.   
  9.     <!-- 配置数据库信息 -->  
  10.     <context id="mysql" targetRuntime="MyBatis3">  
  11.         <commentGenerator>  
  12.             <!-- 设置不生成注释  suppressAllComments :When the property is true, no comments will be added to any generated element.-->  
  13.             <property name="suppressAllComments" value="true" />  
  14.         </commentGenerator>  
  15.           
  16.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  17.             connectionURL="jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"  
  18.             userId="root"  
  19.             password="root">  
  20.         </jdbcConnection>  
  21.       
  22.         <javaTypeResolver>  
  23.           <property name="forceBigDecimals" value="false" />  
  24.         </javaTypeResolver>  
  25.       
  26.         <!-- 指定JavaBean生成的位置,.\src表示直接在项目的src目录下生成 -->  
  27.         <javaModelGenerator targetPackage="com.lqy.ssm.bean" targetProject=".\src\main\java">  
  28.           <property name="enableSubPackages" value="true" />  
  29.           <property name="trimStrings" value="true" />  
  30.         </javaModelGenerator>  
  31.       
  32.         <!-- 指定sql映射文件生成的位置 -->  
  33.         <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">  
  34.             <property name="enableSubPackages" value="true" />  
  35.         </sqlMapGenerator>  
  36.       
  37.         <!-- 指定dao接口生成的位置 -->  
  38.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.lqy.ssm.dao"  targetProject=".\src\main\java">  
  39.           <property name="enableSubPackages" value="true" />  
  40.         </javaClientGenerator>  
  41.           
  42.         <!-- 指定每个table表生成的策略 -->  
  43.         <!-- domainObjectName可省略 -->  
  44.         <table tableName="user" domainObjectName="User"   
  45.             enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>  
  46.         <table tableName="user_ext"   
  47.             enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>  
  48.   
  49.     </context>  
  50. </generatorConfiguration>  

 

 

 

把数据库连接信息修改为自己的数据库连接。

 

三、创建生成文件的代码类:MybatisGenerator.java,内容如下:

官网代码见:http://www.mybatis.org/generator/running/runningWithJava.html

 

Java代码  收藏代码
  1. import java.io.File;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.   
  5. import org.mybatis.generator.api.MyBatisGenerator;  
  6. import org.mybatis.generator.config.Configuration;  
  7. import org.mybatis.generator.config.xml.ConfigurationParser;  
  8. import org.mybatis.generator.internal.DefaultShellCallback;  
  9.   
  10. public class MybatisGenerator {  
  11.   
  12.     public static void main(String[] args) throws Exception {  
  13.         List<String> warnings = new ArrayList<String>();  
  14.         boolean overwrite = true;  
  15.         File configFile = new File("mybatis-generator.xml");  
  16.         ConfigurationParser cp = new ConfigurationParser(warnings);  
  17.         Configuration config = cp.parseConfiguration(configFile);  
  18.         DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
  19.         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
  20.         myBatisGenerator.generate(null);  
  21.     }  
  22.       
  23. }  

 其中File configFile = new File("mybatis-generator.xml");就是读取自己的配置文件,如果命名不一致,需要改为一致。



 

四、最后运行MybatisGenerator方法生成文件,然后F5刷新项目,文件就直接生成。

 

 

==============================

蕃薯耀 2018年3月14日

http://www.cnblogs.com/fanshuyao/

推荐阅读