MyBatis逆项工程

大憨熊 提交于 2020-03-07 07:12:47
  1. 在pom.xml中引入依赖
<!-- MYSQL连接驱动依赖 -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <!-- mybatis -->
  <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
  </dependency>
  <!-- Mybatis逆向功能的核心依赖 -->
  <dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.5</version>
  </dependency>
  <!-- mapper -->
  <dependency>
   <groupId>tk.mybatis</groupId>
   <artifactId>mapper-spring-boot-starter</artifactId>
   <version>1.1.4</version>
  </dependency>
  1. 在src/main/resources目录下创建application.properties
########################################################
###	配置数据源
########################################################
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/***
spring.datasource.username = ***
spring.datasource.password = ***
########################################################
###	解决乱码问题
########################################################
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
########################################################
###	Mybatis
########################################################
mybatis.mapper-locations=classpath:mapper/*.xml
  1. 在src/main/resources目录下创建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的defaultModelType属性值为:"flat"时表示该模型为每一张表只生成一个实体类,这个实体类包含表中的所有字段 -->
	<context id="SQLContext" targetRuntime="MyBatis3Simple">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是   false:否 -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!-- 数据库连接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
						connectionURL="jdbc:mysql://localhost:3306/***"
						userId="***"
						password="***">
		</jdbcConnection>
		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- targetProject:生成Model类的位置  -->
		<javaModelGenerator targetPackage="***" targetProject="src/main/java">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="true"/>
			<!-- 从数据库返回的值被清理前后的空格 --> 
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		<!-- targetProject:生成mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
			<!-- enableSubPackages:是否让schema作为包的后缀  -->
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<!-- 
			客户端代码,生成易于使用的针对Model对象的XML配置文件的代码
			type="ANNOTATEDMAPPER"	生成Java Model和基于的Mapper对象
			type="MIXEDMAPPER"	生成基于注解的Java Model和相应的Mapper对象
			type="XMLMAPPER"	生成SQLMap XML文件和独立的Mapper接口
		 -->
		<javaClientGenerator targetPackage="***" targetProject="src/main/java" type="XMLMAPPER">
			<!-- enableSubPackages:是否让schema作为包的后缀  -->
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		<!-- table标签可以有多个 -->
		<!-- 
			当context的targetRuntime属性值为:MyBatis3时:
			tableName:要生成的表名(数据库中的表名)
       		domainObjectName:生成后的实体类的类名
	        enableCountByExample:Count语句中加入where条件查询,默认true开启
	        enableUpdateByExample:Update语句中加入where条件查询,默认true开启
	        enableDeleteByExample:Delete语句中加入where条件查询,默认true开启
	        enableSelectByExample:Select多条语句中加入where条件查询,默认true开启
	        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认true开启
		 -->
		<table tableName="***"></table>
	</context>
</generatorConfiguration>
  1. 在src/main/java中创建Generator类
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 Generator {
	public void generator() throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("./src/main/resources/generatorConfig.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);
	}
	public static void main(String[] args) {
		Generator generator = new Generator();
		try {
			generator.generator();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  1. 运行Generator类,然后刷新项目,即可
    温馨提示:此方式用于Maven管理的SpringBoot项目,使用时需要把以上代码块内的“***”改成具体的代码。并且生成的Model类中需要添加 @Entity 注解、生成的mapper接口需要添加 @Mapper 注解,最后需要在SpringBoot启动类中添加 @MapperScan(basePackages = {“mapper接口的包名”}) 注解。
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!