SpringBoot-2.X 學習筆記12.0 整合 Mybatis-plus
1 創建項目及配置
1.1 創建一個 SpringBoot-2.X 項目
下圖測試項目除了 com.xu.springboot 中的文件爲創建項目是自帶的其餘全部代碼爲 Mybatis-Plus 代碼自動生成 生成。
1.2 配置 SpringBoot-2.X pom.xml
作者在項目中使用了 阿里的 druid 數據連接池,可以根據自己的實際情況使用其他數據庫連接池。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.xu.erp</groupId>
<artifactId>erp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ERP</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- SpringBoot Web模塊支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringBoot mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!-- SpringBoot thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- SpringBoot 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- SpringBoot lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--MySQL 連接驅動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3 配置 SpringBoot-2.X application.properties
############################################################
# 服務端口配置
############################################################
server.port=8888
############################################################
# 靜態資源配置
############################################################
spring.resources.static-locations=classpath:/templates,classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
############################################################
# thymelea 配置
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
############################################################
# MySQL 配置
############################################################
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource #SpringBoot默認數據源
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://192.168.0.111:3333/erp?serverTimezone=UTC&characterEncoding=utf8
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=60000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
############################################################
# mybatis-plus 配置
############################################################
mybatis-plus.type-aliases-package=com.xu.erp.entity
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2 測試代碼
2.1 Entity
package com.xu.erp.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.extension.activerecord.Model;
/**
* <p>
*
* </p>
*
* @author hyacinth
* @since 2019-12-11
*/
public class Currency extends Model<Currency> {
private static final long serialVersionUID = 1L;
private Integer id;
private String tag;
private String name;
public Integer getId() {
return id;
}
public Currency setId(Integer id) {
this.id = id;
return this;
}
public String getTag() {
return tag;
}
public Currency setTag(String tag) {
this.tag = tag;
return this;
}
public String getName() {
return name;
}
public Currency setName(String name) {
this.name = name;
return this;
}
@Override
protected Serializable pkVal() {
return this.id;
}
}
2.2 Mapper
package com.xu.erp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xu.erp.entity.Currency;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* <p>
* Mapper 接口
* </p>
*
* @author hyacinth
* @since 2019-12-11
*/
@Mapper
public interface CurrencyMapper extends BaseMapper<Currency> {
//這個函數的函數名要和 StudentMapper.xml 中 id 一樣
public List<Currency> all();
@Select("select * from currency where id=#{id}")
public List<Currency> mySelectById(Integer id);
}
2.3 CurrencyMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xu.erp.mapper.CurrencyMapper">
<!-- 通用查詢映射結果 -->
<resultMap id="BaseResultMap" type="com.xu.erp.entity.Currency">
<id column="id" property="id" />
<result column="tag" property="tag" />
<result column="name" property="name" />
</resultMap>
<!-- 通用查詢結果列 -->
<sql id="Base_Column_List">
id, tag, name
</sql>
<!--以下是新增的方法-->
<select id="all" resultType="com.xu.erp.entity.Currency">
select * from currency;
</select>
</mapper>
2.4 Service
package com.xu.erp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xu.erp.entity.Currency;
import java.util.List;
/**
* <p>
* 服務類
* </p>
*
* @author hyacinth
* @since 2019-12-11
*/
public interface CurrencyService extends IService<Currency> {
public List<Currency> all();
public List<Currency> selectById(Integer id);
}
2.4 ServiceImpl
package com.xu.erp.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.erp.entity.Currency;
import com.xu.erp.mapper.CurrencyMapper;
import com.xu.erp.service.CurrencyService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
/**
* <p>
* 服務實現類
* </p>
*
* @author hyacinth
* @since 2019-12-11
*/
@Primary
@Service
public class CurrencyServiceImpl extends ServiceImpl<CurrencyMapper, Currency> implements CurrencyService {
@Autowired
private CurrencyMapper mapper;
@Override
public List<Currency> all() {
return mapper.all();
}
@Override
public List<Currency> selectById(Integer id) {
return mapper.mySelectById(id);
}
}
2.5 Controller
package com.xu.erp.entity.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xu.erp.service.CurrencyService;
/**
* <p>
* 前端控制器
* </p>
*
* @author hyacinth
* @since 2019-12-11
*/
@RestController
@RequestMapping("/erp/currency")
public class CurrencyController {
@Autowired
private CurrencyService service;
@RequestMapping("/test")
public Object test() {
return service.all();
}
@RequestMapping("/select")
public Object select() {
return service.selectById(1);
}
}
2.6 Application
package com.xu.erp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = {"com.xu.erp.*","com.xu.erp.service.impl"})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3 測試結果
3.1 啓動
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2019-12-11 15:44:30.448 INFO 14316 --- [ main] com.xu.erp.Application : Starting Application on ZWJ0R16WWO114LL with PID 14316 (E:\SourceCode\Eclipse-2019-06\ERP\target\classes started by Administrator in E:\SourceCode\Eclipse-2019-06\ERP)
2019-12-11 15:44:30.454 INFO 14316 --- [ main] com.xu.erp.Application : The following profiles are active: test
2019-12-11 15:44:34.737 INFO 14316 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http)
2019-12-11 15:44:34.757 INFO 14316 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-11 15:44:34.757 INFO 14316 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-11 15:44:34.951 INFO 14316 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-12-11 15:44:34.951 INFO 14316 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4304 ms
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.3.0
2019-12-11 15:44:38.139 INFO 14316 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-11 15:44:38.608 INFO 14316 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ''
2019-12-11 15:44:38.614 INFO 14316 --- [ main] com.xu.erp.Application : Started Application in 9.14 seconds (JVM running for 9.967)
2019-12-11 15:45:01.337 INFO 14316 --- [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-12-11 15:45:01.337 INFO 14316 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-12-11 15:45:01.354 INFO 14316 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
2019-12-11 15:45:01.543 INFO 14316 --- [nio-8888-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-12-11 15:45:02.263 INFO 14316 --- [nio-8888-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.