Spring?Boot?利用?XML?方式整合?MyBatis
一、前言
上一篇文章中我們已經(jīng)Spring Boot 利用注解方式整合 MyBatis,今天我們就來看看,如何利用 XML 文件的方式來將兩者整合起來!
下圖是整個(gè)整合過程,接下來開始整合:
二、整合過程
最終項(xiàng)目結(jié)構(gòu)如下圖所示:
新建 Spring Boot 項(xiàng)目
新建一個(gè) Spring Boot 項(xiàng)目,添加 Web 組件,具體過程可以參照我的另一篇博客 Spring Boot 教程之創(chuàng)建項(xiàng)目的三種方式
添加 pom 依賴
由于要整合 MyBatis,所以我們需要在項(xiàng)目的配置文件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
三個(gè)屬性,其中 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ù)源配置
在項(xiàng)目配置文件 application.properties
中配置數(shù)據(jù)源;
# 數(shù)據(jù)庫配置 spring.datasource.username=root spring.datasource.password=1112233 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)建實(shí)體類,為了精簡代碼,后面過程中都或多或少用了 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 實(shí)體類 */ @Data @NoArgsConstructor @AllArgsConstructor public class User { private Long id; private String name; private Integer age; }
dao 層
- 接口編寫
實(shí)體類創(chuàng)建完成后,編寫實(shí)體類對應(yīng)接口;
package com.cunyu.dao; import com.cunyu.pojo.User; import org.apache.ibatis.annotations.Mapper; /** * @InterfaceName : UserDao * @Author : cunyu * @Version : 1.0 * @Description : User 類對應(yīng)接口 **/ @Mapper public interface UserDao { /** * @param id 用戶 id * @return 對應(yīng) id 的用戶 * @description 根據(jù)用戶 id 查詢用戶 * @author cunyu1943 * @version 1.0 */ User getUserById(Long id); }
- 配置 MyBatis
在項(xiàng)目配置文件 application.properties
中添加 MyBatis 配置;
# MyBatis 配置 mybatis.type-aliases-package=com.cunyu.pojo.User mybatis.mapper-locations=classpath:mapper/*.xml
- mapper 編寫
在 src/main/resources/mapper
下新建 UserDao.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.cunyu.dao.UserDao"> <select id="getUserById" resultType="com.cunyu.pojo.User"> SELECT id, name, age FROM user WHERE id = #{id} </select> </mapper>
service 層
- service 接口
package com.cunyu.service; import com.cunyu.pojo.User; /** * @author : cunyu * @version : 1.0 * @className : UserService * @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 接口實(shí)現(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; /** * @author : cunyu * @version : 1.0 * @className : UserServiceImpl * @description : service 接口實(shí)現(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; /** * @author : cunyu * @version : 1.0 * @className : UserController * @description : User controller */ @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é)
通過 XMl 文件來整合 Spring Boot 和 MyBatis 的具體過程了,是不是很簡單呢?對比 XML 文件和注解的方式,最大的不同就在于 DAO 層。前者是通過 XML 配置文件的方式,而后者則是使用 MyBatis 中所提供的注解來實(shí)現(xiàn)。兩種方式各有優(yōu)劣,而且大家也都有使用,不過貌似大家使用的更多的還是 XML 配置的方式。
到此這篇關(guān)于Spring Boot 利用 XML 方式整合 MyBatis的文章就介紹到這了,更多相關(guān)Spring Boot 整合 MyBatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot校園綜合管理系統(tǒng)實(shí)現(xiàn)流程分步講解
這篇文章主要介紹了SpringBoot+Vue實(shí)現(xiàn)校園綜合管理系統(tǒng)流程分步講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的示例
本篇文章主要介紹了Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02SpringBoot中@ConditionalOnBean實(shí)現(xiàn)原理解讀
這篇文章主要介紹了SpringBoot中@ConditionalOnBean實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Springboot集成JUnit5優(yōu)雅進(jìn)行單元測試的示例
這篇文章主要介紹了Springboot集成JUnit5優(yōu)雅進(jìn)行單元測試的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10Java8中用foreach循環(huán)獲取對象的index下標(biāo)詳解
這篇文章主要給大家介紹了關(guān)于Java8中用foreach循環(huán)獲取對象的index下標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04