1 生成文件
目录结构(运行结果)
需要自己创建所有文件夹和部分文件,需创建文件代码在下面
1 像之前一样创建springboot项目
区别:
pom.xml中的mybatis处添加如下代码
<!-- mybatis逆向生成工具 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
2 需自己写的代码
- 1 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="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.next.utils.MyMapper"/>
</plugin>
<!-- 若url写成jdbc:mysql://localhost:3306/news,就会报如下错误 -->
<!-- The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/news?serverTimezone=UTC"
userId="root"
password="root">
</jdbcConnection>
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.next.pojo" targetProject="src/main/java"/>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.next.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
<table tableName="news"></table>
<table tableName="type"></table>
</context>
</generatorConfiguration>
- 2 MyMapper.java
package com.next.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T>{
}
- 3 GeneratorDisplay.java
package com.next.mybatis.utils;
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 GeneratorDisplay {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定逆向工程配置文件
File configFile = new File("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) throws Exception{
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
}catch(Exception e) {
e.printStackTrace();
}
}
}
若多次运行GeneratorDisplay.java,xml文件内的内容会不断增加。因此,再次运行前需将生成文件删除
2 文件迁移
将上面的文件复制到之前创建的项目的相应地方,如图所示:
application.properties内添加mybatis配置
# Server 服务器端相关配置
# 配置api端口号
server.port=8081
#Server -tomcat 相关常用配置
server.tomcat.uri-encoding=UTF-8
#不配置下方数据,需在启动类时这样写:@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
#不然会出现:Failed to configure a DataSource: 'url' attribute is not specified and no embedded
#配置数据源相关 使用HikariCP 数据源
#jdbc_config datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLExpection,默认:30秒
spring.datasource.hikari.connection-timeout=30000
#最小连接数
spring.datasource.hikari.minimum-idle=5
#最大连接数
spring.datasource.hikari.maximum-pool-size=15
#自动提交
spring.datasource.hikari.auto-commit=true
#一个连接idle的最大时长(毫秒),超时则被释放(retired),默认:10分钟
spring.datasource.hikari.idle-timeout=600000
#连接池名字
spring.datasource.hikari.pool-name=DatebookHikariCP
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms
spring.datasource.hikari.max-lifetime=28740000
spring.datasource.hikari.connection-test-query=SELECT 1
#mybatis配置
mybatis.type-aliases-package=com.next.pojo
#classpath 即src/main/resources
mybatis.mapper-locations=classpath:mapper/*.xml
#通用Mapper配置
mapper.mappers=com.next.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
- eclipse 快捷键
java类搜索:Open Type 快捷键:Ctrl+Shift+T
文件内搜索:Find/Replace 快捷键:Ctrl+F