小程序入門學習20--springboot之集成mybatis

學習視頻
官方文檔
本次採用 main方法執行配置文件

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章