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

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

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

蕃薯耀 2018年3月13日

http://fanshuyao.iteye.com/

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

<properties>
    <spring.version>4.3.13.RELEASE</spring.version>
		
    <mybatis.version>3.4.6</mybatis.version>
    <mybatis.spring.version>1.3.1</mybatis.spring.version>                
    <mybatis.generator.version>1.3.6</mybatis.generator.version>
		
    <junit.version>4.12</junit.version>
</properties>

 

<!-- mybatis-->
		<dependency>
		    <groupId>org.mybatis</groupId>
    		<artifactId>mybatis</artifactId>
		    <version>${mybatis.version}</version>
	    </dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>${mybatis.spring.version}</version>
	    </dependency>
	    <dependency>
		    <groupId>org.mybatis.generator</groupId>
		    <artifactId>mybatis-generator-core</artifactId>
		    <version>${mybatis.generator.version}</version>
		    <scope>provided</scope>
		</dependency>

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

直接在项目文件夹下建立xml配置文件:mybatis-generator.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">

<!-- 配置文件信息见:http://www.mybatis.org/generator/configreference/xmlconfig.html -->
<generatorConfiguration>

	<!-- 配置数据库信息 -->
	<context id="mysql" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 设置不生成注释  suppressAllComments :When the property is true, no comments will be added to any generated element.-->
	    	<property name="suppressAllComments" value="true" />
	    </commentGenerator>
	    
	    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
	        connectionURL="jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
	        userId="root"
	        password="root">
	    </jdbcConnection>
	
	    <javaTypeResolver>
	      <property name="forceBigDecimals" value="false" />
	    </javaTypeResolver>
	
		<!-- 指定JavaBean生成的位置,.\src表示直接在项目的src目录下生成 -->
	    <javaModelGenerator targetPackage="com.lqy.ssm.bean" targetProject=".\src\main\java">
	      <property name="enableSubPackages" value="true" />
	      <property name="trimStrings" value="true" />
	    </javaModelGenerator>
	
		<!-- 指定sql映射文件生成的位置 -->
	    <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
	    	<property name="enableSubPackages" value="true" />
	    </sqlMapGenerator>
	
		<!-- 指定dao接口生成的位置 -->
	    <javaClientGenerator type="XMLMAPPER" targetPackage="com.lqy.ssm.dao"  targetProject=".\src\main\java">
	      <property name="enableSubPackages" value="true" />
	    </javaClientGenerator>
	    
		<!-- 指定每个table表生成的策略 -->
		<!-- domainObjectName可省略 -->
	    <table tableName="user" domainObjectName="User" 
	    	enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>
	    <table tableName="user_ext" 
	    	enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>

	</context>
</generatorConfiguration>

 

 

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

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

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

import java.io.File;
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.internal.DefaultShellCallback;

public class MybatisGenerator {

	public static void main(String[] args) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("mybatis-generator.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}
	
}

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



 

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

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

蕃薯耀 2018年3月13日

http://fanshuyao.iteye.com/

猜你喜欢

转载自fanshuyao.iteye.com/blog/2412996