mybatis(7)---spring整合mybatis进行自动生成代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/83475363

1、引入mybatis的generator包

<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>

2、在src下新增配置文件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">

<generatorConfiguration>
  
  <context id="DB2Tables" targetRuntime="MyBatis3">
  	<!-- 配置生成的sql映射文件不需要注释 -->
  	<commentGenerator>
  		<property name="suppressAllComments" value="true" />
	</commentGenerator>
  	<!-- 1、配置数据库连接 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/qx"
        userId="root"
        password="root">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
	<!-- 2、配置javaBean生成的位置 -->
    <javaModelGenerator targetPackage="com.cn.entity" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
	<!-- 3、指定sql映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="com.cn.mapping"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
	<!-- 4、指定mapper接口生成的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.cn.mapper"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
	<!-- 5、指定每个表的生成策略 -->
    <!-- <table tableName="t_emp" domainObjectName="Employee" />
    <table tableName="t_dept" domainObjectName="Dept" /> -->
    <!-- 5、指定每个表的生成策略 -->
    <table schema="general" tableName="user" domainObjectName="User"
    		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
    		enableSelectByExample="false" selectByExampleQueryId="false">
    	
    </table>
	<table schema="general" tableName="t_dept" domainObjectName="Dept"
    		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
    		enableSelectByExample="false" selectByExampleQueryId="false">
    	
    </table>
  </context>
</generatorConfiguration>

3、在src下新建包com.cn.test,然后在该包下新建测试类MyBatsGeneratorTest

package com.cn.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.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
 * 用于生成mapper映射文件、接口、实体类
 * */
public class MyBatisGeneratorTest {
	public static void main(String[] args) throws Exception, XMLParserException {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("G:\\workPlace\\taotao2\\qxSystem\\config\\spring\\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);
	}
}

4、运行测试类后,刷新src目录会看到生成的实体类、mapper接口和mapper映射文件。注意要先建表,是根据表的字段生成的

猜你喜欢

转载自blog.csdn.net/sinat_23490433/article/details/83475363