詳解SpringBoot整合MyBatis詳細(xì)教程
1. 導(dǎo)入依賴
首先新建一個springboot項(xiàng)目,勾選組件時勾選Spring Web、JDBC API、MySQL Driver
然后導(dǎo)入以下整合依賴
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
2. 連接數(shù)據(jù)庫
數(shù)據(jù)庫代碼:
-- 創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE springboot;
-- 使用springboot數(shù)據(jù)庫
use springboot;
-- 創(chuàng)建user表
CREATE TABLE IF NOT EXISTS `user`(
`id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '身份號',
`name` VARCHAR(30) NOT NULL DEFAULT '匿名' COMMENT '姓名',
`pwd` VARCHAR(30) NOT NULL DEFAULT '123456' COMMENT '密碼',
PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
-- 給user表插入數(shù)據(jù)
INSERT INTO `user`(`id`,`name`,`pwd`)
VALUES ('1','zsr',000204),('2','gcc',000421),('3','BaretH',200024);
然后IDEA連接數(shù)據(jù)庫

打開我們創(chuàng)建的數(shù)據(jù)庫springboot

對應(yīng)的user表

3. 編寫數(shù)據(jù)庫配置信息
在springboot配置文件中配置數(shù)據(jù)庫信息
spring.datasource.username=root spring.datasource.password=200024 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
4. 編寫pojo實(shí)體類
在主程序同級目錄下新建pojo包,其中新建User實(shí)體類(使用了lombok)
package com.zsr.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
int id;
String name;
String password;
}
5. 編寫mapper接口
在主程序同級目錄下新建mapper包,其中新建UserMapper接口

package com.zsr.mapper;
import com.zsr.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper //表示這是Mybatis的mapper類
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserByID(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
6. 編寫mapper.xml
在resources目錄下新建mabatis包,其中新建mapper包,再在其中新建mapper.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.zsr.mapper.UserMapper">
<select id="queryUserList" resultType="user">
select * from user
</select>
<select id="queryUserByID" resultType="user">
select * from user where id= #{id}
</select>
<insert id="addUser" parameterType="user">
insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="user">
update user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
7. 編寫controller
在主程序同級目錄下新建controller包,在其中新建UserController
package com.zsr.controller;
import com.zsr.mapper.UserMapper;
import com.zsr.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList() {
List<User> users = userMapper.queryUserList();
return users;
}
@GetMapping("/queryUserByID")
public User queryUserByID() {
User user = userMapper.queryUserByID(2);
return user;
}
@GetMapping("/addUser")
public String addUser() {
userMapper.addUser(new User(4, "zml", "45632"));
return "增加用戶完畢";
}
@GetMapping("/updateUser")
public String updateUser() {
userMapper.updateUser(new User(4, "zml", "678910"));
return "修改用戶完畢";
}
@GetMapping("/deleteUser")
public String deleteUser() {
userMapper.deleteUser(4);
return "刪除用戶完畢";
}
}
8. 測試
測試訪問http://localhost:8080/queryUserList

測試訪問http://localhost:8080/queryUserByID

測試訪問http://localhost:8080/addUser


測試訪問http://localhost:8080/updateUser


測試訪問http://localhost:8080/deleteUser


到此這篇關(guān)于詳解SpringBoot整合MyBatis詳細(xì)教程的文章就介紹到這了,更多相關(guān)SpringBoot整合MyBatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java微信公眾平臺開發(fā)(14) 微信web開發(fā)者工具使用
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第十四步,微信web開發(fā)者工具的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
SpringBoot中利用@Valid和@Validated進(jìn)行參數(shù)校驗(yàn)
為了保證數(shù)據(jù)的正確性、完整性,前后端都需要進(jìn)行數(shù)據(jù)檢驗(yàn),作為一名后端開發(fā)工程師,不能僅僅依靠前端來校驗(yàn)數(shù)據(jù),我們還需要對接口請求的參數(shù)進(jìn)行后端的校驗(yàn),所以本文給大家介紹了SpringBoot中利用@Valid和@Validated進(jìn)行參數(shù)校驗(yàn),需要的朋友可以參考下2024-09-09
Java建造者模式構(gòu)建復(fù)雜對象的最佳實(shí)踐
建造者模式,是一種對象構(gòu)建模式?它可以將復(fù)雜對象的建造過程抽象出來,使這個抽象過程的不同實(shí)現(xiàn)方法可以構(gòu)造出不同表現(xiàn)的對象。本文將通過示例講解建造者模式,需要的可以參考一下2023-04-04
解決javaBean規(guī)范導(dǎo)致json傳參首字母大寫將永遠(yuǎn)獲取不到問題
這篇文章主要介紹了解決javaBean規(guī)范導(dǎo)致json傳參首字母大寫將永遠(yuǎn)獲取不到問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
java項(xiàng)目中讀取jdbc.properties文件操作
這篇文章主要介紹了java項(xiàng)目中讀取jdbc.properties文件操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
java 使用JDBC構(gòu)建簡單的數(shù)據(jù)訪問層實(shí)例詳解
以下是如何使用JDBC構(gòu)建一個數(shù)據(jù)訪問層,包括數(shù)據(jù)轉(zhuǎn)換(將從數(shù)據(jù)庫中查詢的數(shù)據(jù)封裝到對應(yīng)的對象中……),數(shù)據(jù)庫的建立,以及如何連接到數(shù)據(jù)庫,需要的朋友可以參考下2016-11-11

