Spring?Boot?利用注解方式整合?MyBatis
前言
目前而言,國內(nèi)大家使用最多的持久層框架可能還是 MyBatis 吧,那既然如此,更強大的 Spring Boot 遇上熾手可熱的 MyBatis,又會擦出什么樣的火花呢?
那本文就來看看,如何利用 SpringBoot 來整合 Mybatis。
如下圖是總結(jié)的整合過程的大概流程,那接下來我們就來開始具體的整合操作!

整合過程
最終項目結(jié)構(gòu)如下圖所示:

新建 Spring Boot 項目
新建一個 Spring Boot 項目,添加 Web 組件,具體過程可以參照我的另一篇博客Spring Boot 教程之創(chuàng)建項目的三種方式
添加 pom 依賴
由于要整合 MyBatis,所以我們需要在項目的配置文件 pom.xml 中添加 mysql 驅(qū)動和 SpringBoot MyBatis 整合包;
<!-- springboot mybatis 整合包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- mysql 驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>準(zhǔn)備數(shù)據(jù)庫
- 數(shù)據(jù)庫創(chuàng)建及輸入插入
準(zhǔn)備一張 user 表,有 id、name、age 三個屬性,其中 id 為主鍵且自增,然后插入三條數(shù)據(jù);
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(50) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO user values (1,"村雨遙",25); INSERT INTO user values (2,"張三",26); INSERT INTO user values (3,"李四",27);
- 數(shù)據(jù)源配置
在項目配置文件 application.properties 中配置數(shù)據(jù)源;
# 數(shù)據(jù)庫配置 spring.datasource.username=root spring.datasource.password=0908 spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
pojo 層
根據(jù)數(shù)據(jù)庫創(chuàng)建實體類,為了精簡代碼,后面過程中都或多或少用了 Lombok 插件,所以需要事先在 pom.xml 引入;
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>package com.cunyu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author : cunyu
* @version : 1.0
* @className : User
* @date : 2020/7/26 20:44
* @description : User 實體類
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
}dao 層
實體類創(chuàng)建完成后,編寫實體類對應(yīng)接口;
package com.cunyu.dao;
import com.cunyu.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* @InterfaceName : UserDao
* @Author : cunyu
* @Date : 2020/7/26 20:47
* @Version : 1.0
* @Description : User 類對應(yīng)接口
**/
@Mapper
public interface UserDao {
/**
* @param id 用戶 id
* @return 對應(yīng) id 的用戶
* @description 根據(jù)用戶 id 查詢用戶
* @date 2020/7/26 20:48
* @author cunyu1943
* @version 1.0
*/
@Select("SELECT id,name,age FROM user where id = #{id}")
User getUserById(Long id);
}service 層
- service 接口
package com.cunyu.service;
import com.cunyu.pojo.User;
/**
* @author : cunyu
* @version : 1.0
* @className : UserService
* @date : 2020/7/26 20:57
* @description : User service 接口
*/
public interface UserService {
/**
* @param id 用戶 iD
* @return 對應(yīng) id 的用戶
* @description 根據(jù) id 查找用戶
* @date 2020/7/26 20:58
* @author cunyu1943
* @version 1.0
*/
User getUserById(Long id);
}- service 接口實現(xiàn)類
package com.cunyu.service.impl;
import com.cunyu.dao.UserDao;
import com.cunyu.pojo.User;
import com.cunyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @description : service 接口實現(xiàn)類
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUserById(Long id) {
return userDao.getUserById(id);
}
}controller 層
package com.cunyu.controller;
import com.cunyu.pojo.User;
import com.cunyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
/**
* 自動注入
*/
@Autowired
private UserService userService;
@GetMapping("/user")
public User getUserById() {
User user = userService.getUserById(1L);
return user;
}
}入口程序配置
在入口程序中配置 mapper 自動掃描;
package com.cunyu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = "com.cunyu.dao")
@SpringBootApplication
public class MybatisXmlApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisXmlApplication.class, args);
}
}網(wǎng)頁測試
完成上述所有步驟之后,在瀏覽器中訪問 http://localhost:8080/user,就可以在網(wǎng)頁中顯示對應(yīng) id 的 User 對象的所有信息;

總結(jié)
完成 Spring Boot 整合 MyBatis 的具體過程了,可能發(fā)現(xiàn)了,我們在 DAO 層未使用任何 XML 文件,取而代之的是各種不同的注解。那我們下一篇文章就來看看,Spring Boot 利用 XML 方式整合 MyBatis
到此這篇關(guān)于Spring Boot 利用注解方式整合 MyBatis的文章就介紹到這了,更多相關(guān)Spring Boot 整合 MyBatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java線程池ThreadPoolExecutor原理及使用實例
這篇文章主要介紹了Java線程池ThreadPoolExecutor原理及使用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
java-SSH2實現(xiàn)數(shù)據(jù)庫和界面的分頁
本文主要是介紹SSH2實現(xiàn)數(shù)據(jù)庫和界面的分頁的代碼,分頁在web應(yīng)用中是經(jīng)常要做的事情,實用性比較大,有需要的朋友可以來了解一下。2016-10-10
關(guān)于文件上傳MultipartBody的使用方法
這篇文章主要介紹了關(guān)于文件上傳MultipartBody的使用方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring Cloud Gateway 服務(wù)網(wǎng)關(guān)快速實現(xiàn)解析
這篇文章主要介紹了Spring Cloud Gateway 服務(wù)網(wǎng)關(guān)快速實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08

