SpringBoot整合MyBatis和SpringBoot整合MyBatis-Plus教程
Spring Boot 整合MyBatis
代碼+配置實現(xiàn)
創(chuàng)建數(shù)據(jù)庫和表
CREATE DATABASE `springboot_mybatis` use `springboot_mybatis` CREATE TABLE `monster` ( `id` INT NOT NULL AUTO_INCREMENT, `age` INT NOT NULL, `birthday` DATE DEFAULT NULL, `email` VARCHAR(255) DEFAULT NULL, `gender` char(1) DEFAULT NULL, `name` VARCHAR(255) DEFAULT NULL, `salary` DOUBLE NOT NULL, PRIMARY KEY (`id`) ) CHARSET=utf8 SELECT * FROM `monster` insert into monster values(null, 20, '2000-11-11', 'nmw@sohu.com', '男', '牛魔王', 5000.88); insert into monster values(null, 10, '2011-11-11', 'bgj@sohu.com', '女', '白骨精', 8000.88);
使用靈活的方式創(chuàng)建maven
注意我們這里使用的數(shù)據(jù)源為druid.
<!--導(dǎo)入springboot父工程-規(guī)定寫法-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<!--引入相關(guān)的依賴-->
<dependencies>
<!--引入web starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入mybatis starter, 如果小伙伴看不到 版本,自己手寫2.2.2-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--引入mysql驅(qū)動: 這里使用版本仲裁 8.0.26-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--引入配置處理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--引入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入test starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--引入druid依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
</dependencies>創(chuàng)建resources/application.yml ,配置數(shù)據(jù)源參數(shù), 并完成Spring Boot 項目啟動測試
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 自己的密碼測試DruidDataSourceConfig
/**
建議, 最好是先自己把這個類寫出,然后拷貝自己需要的代碼, 這樣自己寫代碼也知道是為什么
*
*/
@Configuration
public class DruidDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource =
new DruidDataSource();
return druidDataSource;
}
}創(chuàng)建/Monster.java -SSM整合在博客的項目有介紹
@Data
public class Monster {
private Integer id;
private Integer age;
//這里通過注解來解決時區(qū)問題
//GMT 就是格林尼治標(biāo)準(zhǔn)時間
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}創(chuàng)建MonsterMapper接口
@Mapper
public interface MonsterMapper {
//方法,根據(jù)id返回Monster對象
public Monster getMonsterById(Integer id);
}創(chuàng)建resources\mapper\MonsterMapper.xml , 文件模板從mybatis 官方文檔拷貝
<?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.wyx.springboot.mybatis.mapper.MonsterMapper">
<!--配置getMonsterById-->
<select id="getMonsterById" resultType="Monster">
SELECT * FROM `monster` WHERE id=#{id}
</select>
</mapper>創(chuàng)建 service/MonsterService.java接口
public interface MonsterService {
//根據(jù)id返回Monster對象
public Monster getMonsterById(Integer id);
}創(chuàng)建 service/impl/MonsterServiceImpl.java接口
@Service
public class MonsterServiceImpl implements MonsterService {
//裝配MonsterMapper
@Resource
private MonsterMapper monsterMapper;
@Override
public Monster getMonsterById(Integer id) {
return monsterMapper.getMonsterById(id);
}
}創(chuàng)建MonsterController
@Controller
public class MonsterController {
//裝配MonsterService
@Resource
private MonsterService monsterService;
@ResponseBody
@GetMapping("/monster")
public Monster getMonsterById(@RequestParam(value = "id") Integer id){
return monsterService.getMonsterById(id);
}
}修改resources/application.yml , 指定mybatis.xml 的配置參數(shù)
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 自己的密碼
mybatis:
#指定要掃描的 Xxxmapper.xml
mapper-locations: classpath:mapper/*.xml
#通過config-location 可以指定mybatis-config.xml,可以以傳統(tǒng)的方式來配置mybatis
#config-location:
#config-location: classpath:mybatis-config.xml
#我們也可以直接在application.yml進行配置
#舉例說明1. 比如配置原來的 typeAliases
#舉例說明2 配置輸出底層的原生sql
#還有很多其它的配置,我們使用到再說
type-aliases-package: com.wyxdu.springboot.mybatis.bean
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#說明: 配置mybatis的兩種方式的選擇: 如果配置比較簡單,就直接在application.yml配置即可
#如果配置內(nèi)容比較多,可以考慮單獨的做一個mybatis-config.xml測試頁面效果
- 完成測試, 瀏覽器: http://localhost:10000/monster?id=1

注意事項和細節(jié)說明
spring boot 整合mybatis 取出的日期, 出現(xiàn)8 小時時差解決方案

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
Spring Boot 整合MyBatis-Plus
官網(wǎng)https://baomidou.com

基本介紹
1. MyBatis-Plus (簡稱MP)是一個MyBatis 的增強工具,在MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。

2. 強大的CRUD 操作:內(nèi)置通用Mapper、通用Service,通過少量配置即可實現(xiàn)單表大部分CRUD 操作,更有強大的條件構(gòu)造器,滿足各類使用需求
整合MyBatis-Plus 實例
需求說明/圖解
1. 將Spring Boot 和MyBatis-Plus 整合
2. 查詢數(shù)據(jù),如圖

代碼實現(xiàn)
創(chuàng)建數(shù)據(jù)庫和表
CREATE DATABASE `springboot_mybatisplus` USE `springboot_mybatisplus` CREATE TABLE `monster` ( `id` INT NOT NULL AUTO_INCREMENT, `age` INT NOT NULL, `birthday` DATE DEFAULT NULL, `email` VARCHAR(255) DEFAULT NULL, `gender` CHAR(1) DEFAULT NULL, `name` VARCHAR(255) DEFAULT NULL, `salary` DOUBLE NOT NULL, PRIMARY KEY (`id`) ) CHARSET=utf8 SELECT * FROM `monster` INSERT INTO monster VALUES(NULL, 20, '2000-11-11', 'xzj@sohu.com', ' 男', ' 蝎子精', 15000.88); INSERT INTO monster VALUES(NULL, 10, '2011-11-11', 'ytj@sohu.com', ' 女', ' 玉兔精', 18000.88);
創(chuàng)建springboot_mybatisplus 項目-pom.xml 引入必要的依賴
<!--導(dǎo)入springboot父工程-規(guī)定寫法-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<!--引入必要的依賴-->
<dependencies>
<!--引入web starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入mysql驅(qū)動: 這里使用版本仲裁 8.0.26-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--引入配置處理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--引入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入test starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>修改pom.xml 引入MyBatis-Plus starter

<!--引入mybatis-plus starter-->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>創(chuàng)建resources/application.yml 配置數(shù)據(jù)源參數(shù)
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatisplus?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 自己的數(shù)據(jù)庫密碼切換數(shù)據(jù)源為druid ,修改pom.xml
<!--引入druid依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>創(chuàng)建配置文件DruidDataSourceConfig.java
@Configuration
public class DruidDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource =
new DruidDataSource();
return druidDataSource;
}
}創(chuàng)建主函數(shù)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}測試是否能正確啟動項目, 注意觀察mybatis-plus 是否引入成功

創(chuàng)建ben/ Monster.java
@Data
//說明:
//1. 如果這個類名Monster 和表名monster 一致,可以映射上,則@TableName 可以省略
//2. 如果這個類名Monster 和表名不一致,不能映射上,則@TableName 可以指定
@TableName("monster")
public class Monster {
private Integer id;
private Integer age;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}創(chuàng)建/mapper/MonsterMapper.java
1. BaseMapper 已經(jīng)默認提供了很多crud 方法, 可以直接用
2. 如果BaseMapper 提供的方法不滿足需要,可以再開發(fā)MonsterMapper.xml
@Mapper
public interface MonsterMapper extends BaseMapper<Monster> {
}創(chuàng)建service/MonsterService.java
解讀:
- 1.在傳統(tǒng)的方法中 在接口中定義方法/聲明方法,然后在實現(xiàn)類進行實現(xiàn)
- 2. 在mybits-plus中我們可以繼承父接口IService
- 3. 這個IService接口聲明很多方法比如crud
- 4. 如果默認方法不能滿足需求,我們可以再聲明需要的方法,然后在實現(xiàn)類去實現(xiàn)就可
@Service
public interface MonsterService extends IService<Monster> {
}
創(chuàng)建impl/MonsterServiceImpl.java
解讀:
以前的做法是MonsterServiceImpl implements MonsterService 接口.
- 但是這樣做會報錯 因為以前父接口沒有繼承接口可以直接這樣
- 但是現(xiàn)在父接口繼承了一個IService接口 所以你要實現(xiàn)IService的方法
- 但是那個太多了我們也用不了那么多
- 所以mybits-plus給我們提供一個ServiceImpl類來繼承 這個類實現(xiàn)了IService接口
所以我們可以看 ServiceImpl類 實現(xiàn)了IService接口.
MonsterService又繼承了IService接口.
所以我們MonsterServiceImpl 繼承了ServiceImpl類 就可以相當(dāng)于實現(xiàn)了MonsterService接口.



@Service
public class MonsterServiceImpl extends ServiceImpl<MonsterMapper, Monster> implements MonsterService {
}創(chuàng)建/controller/MonsterController.java
@Controller
public class MonsterController {
@Autowired
MonsterService monsterService;
@ResponseBody
@GetMapping("/monster")
public Monster getByMonsterId(@RequestParam("id") Integer id) {
return monsterService.getById(id);
}
@ResponseBody
@GetMapping("/list")
public List<Monster> listMonster() {
return monsterService.list();
}
}修改Application.java , 加入對Mapper 的掃描
@MapperScan("com.wyx.mybatisplus.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
啟動項目,完成測試
瀏覽器: http://localhost:10000/list http://localhost:10000/monster?id=1

整合MyBatis-Plus 注意事項和細節(jié)
1. @TableName 作用.

如果這個類名Monster 和表名monster 一致,可以映射上,則@TableName 可以省略.
如果這個類名Monster 和表名不一致,不能映射上,則可以通過@TableName 指定.
2. MyBatis-Plus starter 到底引入了哪些依賴?

3. 為了開發(fā)方便, 可以安裝MyBatisX 插件,


總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成XXL-JOB實現(xiàn)靈活控制的分片處理方案
因為需要并行處理同一張數(shù)據(jù)表里的數(shù)據(jù),所以比較自然地想到了分片查詢數(shù)據(jù),可以利用對 id 取模的方法進行分片,避免同一條數(shù)據(jù)被重復(fù)處理,所以本文給大家介紹了SpringBoot集成XXL-JOB實現(xiàn)靈活控制的分片處理方案,需要的朋友可以參考下2024-09-09
詳解Spring boot+CXF開發(fā)WebService Demo
這篇文章主要介紹了詳解Spring boot+CXF開發(fā)WebService Demo,非常具有實用價值,需要的朋友可以參考下2017-05-05
Java畢業(yè)設(shè)計實戰(zhàn)之校園一卡通系統(tǒng)的實現(xiàn)
這是一個使用了java+Springboot+Maven+mybatis+Vue+mysql+wd開發(fā)的校園一卡通系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習(xí),具有校園一卡通系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01
Spring boot+mybatis+thymeleaf 實現(xiàn)登錄注冊增刪改查功能的示例代碼
這篇文章主要介紹了Spring boot+mybatis+thymeleaf 實現(xiàn)登錄注冊增刪改查功能的示例代碼,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

