MyBatis automatically generates sql tool

mybatis-generator-core

Introduction

Generate corresponding entity classes, dao interface and mapper.xml according to the database corresponding table fields, which solves the basic sql writing;
mybatis code generation tool, based on the mybatis-generator-core 1.3.5 version to expand, before using the code generator Assuming you have experience in using the mybatis-generator-core generation tool, and are familiar with the various configurations, if you are not familiar with it, please search for the official version of the usage and configuration on the Internet. The extensions are provided in the form of plug-ins. Modify the source code, if you don’t need the expanded version, just remove the relevant extension plug-ins in the xml

Use test

Project structure: ordinary java console project
Ordinary java console project

Database: school
Insert picture description here

Core files: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">

<generatorConfiguration>

	<context id="DB2Tables" targetRuntime="MyBatis3">

		<!-- 不生成注释 -->
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>

		<!-- 配置数据库连接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/school" 
			userId="root"
			password="123456">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 指定entity生成位置 -->
		<javaModelGenerator targetPackage="com.ysk.entity" targetProject=".\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!-- sql映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.ysk.mapper" targetProject=".\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- 指定dao接口生成位置,mapper接口 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ysk.dao" targetProject=".\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>

		<!-- 指定每个表达生成策略 -->
		<!--执行过后应将其注释掉,否则下次执行就会将我们自己写的代码覆盖-->
		<table tableName="student" domainObjectName="Student"></table>
		<table tableName="teacher" domainObjectName="Teacher"></table>

	</context>
</generatorConfiguration>

Test category: Test

package test;

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 Test {
    
    
	public static void main(String[] args) {
    
    
		List<String> war = new ArrayList<String>();
		Boolean ovr = true;
		File file = new File("./resources/generatorConfig.xml");
		ConfigurationParser cp = new ConfigurationParser(war);
		try {
    
    
			Configuration config = cp.parseConfiguration(file);
			DefaultShellCallback back = new DefaultShellCallback(ovr);
			MyBatisGenerator my = new MyBatisGenerator(config, back, war);
			my.generate(null);
			for (String s : war) {
    
    
				System.out.println(s);
			}
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}
		System.out.println("执行完成>>>>>>");
	}
}

Refreshing the project after execution will generate the corresponding class and xml
Insert picture description here
. It is quite convenient to use, you should try it!

Precautions:

  • Multiple executions will overwrite the existing ones, you should pay attention to the generatorConfig.xml>table tag, just comment it out after execution
  • Official warning: Please read carefully before using it. Please do not add the [email protected] comment where you write, and do not delete the automatically generated [email protected] comment, because the merge is done based on this comment If you do not pay attention to the operation, the code added by yourself will be overwritten when repeated generation, remember! ! !

Guess you like

Origin blog.csdn.net/qq_44621891/article/details/109065776