mybatis generator configuration, Mybatis automatically generates file configuration, Mybatis automatically generates entity bean configuration

mybatis generator configuration, Mybatis automatically generates file configuration, Mybatis automatically generates entity bean configuration

 

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

Sweet Potato YaoMarch 13, 2018

http://fanshuyao.iteye.com/

 

1. Use Maven to introduce the Mybatis dependency Jar package (the version number is changed or defined by yourself)

<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>

 

2. Add files to generate xml configuration files

Create an xml configuration file directly under the project folder: mybatis-generator.xml, with the following contents:

 

<?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">

<!-- For configuration file information, see: http://www.mybatis.org/generator/configreference/xmlconfig.html -->
<generatorConfiguration>

	<!-- Configuration database information-->
	<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>
	
		<!-- Specify the location where JavaBeans are generated, .\src means to generate directly in the src directory of the project -->
	    <javaModelGenerator targetPackage="com.lqy.ssm.bean" targetProject=".\src\main\java">
	      <property name="enableSubPackages" value="true" />
	      <property name="trimStrings" value="true" />
	    </javaModelGenerator>
	
		<!-- Specify the location where the sql mapping file is generated-->
	    <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
	    	<property name="enableSubPackages" value="true" />
	    </sqlMapGenerator>
	
		<!-- Specify the location where the dao interface is generated-->
	    <javaClientGenerator type="XMLMAPPER" targetPackage="com.lqy.ssm.dao"  targetProject=".\src\main\java">
	      <property name="enableSubPackages" value="true" />
	    </javaClientGenerator>
	    
		<!-- Specify the strategy generated by each table table -->
		<!-- domainObjectName can be omitted -->
	    <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>

 

 

 

Modify the database connection information to your own database connection.

 

3. The code class for creating the generated file: MybatisGenerator.java, the content is as follows:

See the official website code: 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);
	}
	
}

 Among them, File configFile = new File("mybatis-generator.xml"); is to read your own configuration file. If the naming is inconsistent, you need to change it to the same.



 

Fourth, finally run the MybatisGenerator method to generate the file, and then F5 refresh the project, the file will be generated directly.

 

 

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

Sweet Potato YaoMarch 13, 2018

http://fanshuyao.iteye.com/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326264776&siteId=291194637