基於Spring Boot的MyBatis配置詳解
Spring Boot是一種快速開發應用程式的框架,而MyBatis是一個流行的持久化框架。在Spring Boot中使用MyBatis可以簡化資料庫存取和資料持久化的過程。本文將詳細解釋如何在Spring Boot中設定和使用MyBatis,並提供具體的程式碼範例。
一、MyBatis設定
- 加入相關依賴
#在使用MyBatis之前,首先需要在pom.xml檔案中加入相關的依賴。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
- 配置資料來源
在Spring Boot中,可以使用內嵌的H2資料庫作為示範。在application.properties檔案中新增以下設定:
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password=
- 設定MyBatis
在application.properties檔案中新增下列設定:
mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.domain
其中, mapper-locations
指定了MyBatis對映檔案的位置,type-aliases-package
指定了實體類別的套件名稱。
- 建立實體類別
建立一個User類別作為範例實體類別:
package com.example.domain; public class User { private Long id; private String name; // 省略getter和setter方法 }
- 建立Mapper介面
建立一個UserMapper接口,在接口中定義需要執行的資料庫操作:
package com.example.mapper; import com.example.domain.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { User getUserById(Long id); void saveUser(User user); void updateUser(User user); void deleteUser(Long id); }
- 建立Mapper映射檔
在resources目錄下建立mapper資料夾,並在該資料夾下建立UserMapper.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.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.domain.User"> <id column="id" property="id" jdbcType="BIGINT"/> <result column="name" property="name" jdbcType="VARCHAR"/> </resultMap> <select id="getUserById" resultMap="BaseResultMap"> SELECT * FROM user WHERE id = #{id} </select> <insert id="saveUser"> INSERT INTO user(name) VALUES(#{name}) </insert> <update id="updateUser"> UPDATE user SET name = #{name} WHERE id = #{id} </update> <delete id="deleteUser"> DELETE FROM user WHERE id = #{id} </delete> </mapper>
二、使用MyBatis進行資料庫操作
- 編寫Service類別
編寫一個UserService類,用於執行特定的資料庫操作:
package com.example.service; import com.example.domain.User; import com.example.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.getUserById(id); } public void saveUser(User user) { userMapper.saveUser(user); } public void updateUser(User user) { userMapper.updateUser(user); } public void deleteUser(Long id) { userMapper.deleteUser(id); } }
- 編寫Controller類別
#編寫一個UserController類,用於接收外部請求並呼叫對應的Service方法:
package com.example.controller; import com.example.domain.User; import com.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } @PostMapping("/") public void saveUser(@RequestBody User user) { userService.saveUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable Long id, @RequestBody User user) { user.setId(id); userService.updateUser(user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } }
- 啟動應用程式
編寫一個啟動類,並新增@SpringBootApplication
註解:
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
- #測試介面
可以使用Postman等工具發送HTTP請求,測試介面的呼叫。例如,發送GET請求:localhost:8080/users/1
,即可查詢id為1的使用者資訊。
結語
本文詳解了在基於Spring Boot的專案中配置和使用MyBatis的過程,並提供了相關的程式碼範例。透過以上步驟,您可以在Spring Boot專案中輕鬆整合並使用MyBatis進行資料庫操作。希望本文對您有幫助!
以上是Spring Boot下MyBatis的設定指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1
強大的PHP整合開發環境