SpringBoot 集成 Mybatis 詳細(xì)教程
(只有操作,沒有理論,僅供參考學(xué)習(xí))
一、操作部分
1. 準(zhǔn)備數(shù)據(jù)庫
1.1 數(shù)據(jù)庫版本:
C:\WINDOWS\system32>mysql -V
mysql Ver 8.0.25 for Win64 on x86_64 (MySQL Community Server - GPL)
1.2 啟動(dòng) mysql 服務(wù):
C:\WINDOWS\system32>net start mysql
The MySQL service is starting..
The MySQL service was started successfully.
1.3 新創(chuàng)建數(shù)據(jù)庫 spring,并創(chuàng)建一個(gè) user 表,user 表詳情:
Column Name Datatype PrimaryKey Unique
id INT √ √
username VARCHAR(45)
password VARCHAR(45)
如下圖:
2. 創(chuàng)建工程
2.1 使用 spring 官網(wǎng)頁面創(chuàng)建工程(其他亦可),鏈接:Spring Initializr 創(chuàng)建 SpringBoot 工程,如下圖:
2.2 用 IDEA 打開工程:
2.3 配置項(xiàng)目的 maven 環(huán)境:File -> Setting... -> Maven
2.4 導(dǎo)入依賴 pom.xml
文件:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
注意點(diǎn):由于我的項(xiàng)目運(yùn)行有報(bào)錯(cuò)的原因,我把項(xiàng)目的版本調(diào)整了一下:
修正前:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
修正后:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
配置完成后完整的 pom.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.buzl</groupId>
<artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>main</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.5 配置數(shù)據(jù)庫連接文件 application.properties
注意點(diǎn):
第一行的數(shù)據(jù)庫要修改為自己的數(shù)據(jù)庫名稱,我的數(shù)據(jù)庫名稱是 spring
,根據(jù)實(shí)際需求更改
第二行和第三行是連接數(shù)據(jù)庫的用戶名和密碼,根據(jù)實(shí)際需求更改
第四行是數(shù)據(jù)庫的驅(qū)動(dòng),使用 com.mysql.cj.jdbc.Driver
還是 com.mysql.jdbc.Driver
按需更改
第五行是 mybatis mapper
文件路徑,我的文件路徑是:resources/mapper/UserMapper.xml
,按需更改
spring.datasource.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
2.6 創(chuàng)建 User.java
實(shí)體類
public class User {
/**
* 主鍵 id
*/
private Integer id;
private String username;
private String password;
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
/**
* 重寫 toString 方法
* @return
*/
@Override
public String toString() {
return "User{" +
"id = " + id +
"\t username = " + username +
"\t password = " + password +
"}";
}
// set and get method ...
}
2.7 創(chuàng)建 UserMapper
接口
@Mapper // 表示這是一個(gè) Mybatis 的 mapper 類
@Repository
public interface UserMapper {
@Autowired
int insertUser(User user);
@Autowired
int deleteByPrimaryKey(Integer id);
@Autowired
User selectByPrimaryKey(Integer id);
@Autowired
int updateUserByPrimaryKey(User user);
@Autowired
List<User> selectAll();
}
2.8 創(chuàng)建 UserMapper
控制器
@RestController
@RequestMapping("/mybatis")
public class UserConroller {
@Resource
private UserMapper userMapper;
@GetMapping("/selectAll")
public List<User> selectAll() {
List<User> userList = userMapper.selectAll();
return userList;
}
@GetMapping("/selectByPrimaryKey")
public User selectByPrimaryKey() {
User user = userMapper.selectByPrimaryKey(111);
return user;
}
@GetMapping("/insertSelective")
public String insertUser() {
User user = new User(123, "梅花", "pwd123");
userMapper.insertUser(user);
return "insert data to database success!";
}
@GetMapping("/updateByPrimaryKeySelective")
public String updateUserByPrimaryKey() {
Integer id = 123;
User user = new User(id, "劉備", "pwd666");
userMapper.updateUserByPrimaryKey(user);
return "database updated success!";
}
@GetMapping("/deleteByPrimaryKey")
public String deleteByPrimaryKey() {
Integer id = 123;
userMapper.deleteByPrimaryKey(id);
return "delete database success!";
}
}
2.6 UserMapper.xml
文件
注意點(diǎn):id
的值要和 UserMapper
接口的函數(shù)名稱對(duì)應(yīng)上
如:
xml 文件 id 的值 selectAll 名字和 UserMapper 接口中的函數(shù)名一一對(duì)應(yīng)
xml 文件:
<select id="selectAll" resultType="com.buzl.main.entity.User">
UserMapper 接口函數(shù):
@Autowired
List<User> selectAll();
<?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.buzl.main.mapper.UserMapper">
<!--共通屬性抽出-->
<sql id="Base_Column_List">
id,username,password
</sql>
<!--查詢數(shù)據(jù)庫中所有的數(shù)據(jù)-->
<select id="selectAll" resultType="com.buzl.main.entity.User">
<!--select * from spring.user-->
select <include refid="Base_Column_List" /> from spring.user
</select>
<!--通過 id 查詢數(shù)據(jù)-->
<select id="selectByPrimaryKey" resultType="com.buzl.main.entity.User">
select <include refid="Base_Column_List" /> from spring.user where id=#{id}
</select>
<!--向數(shù)據(jù)庫中插入數(shù)據(jù)-->
<insert id="insertUser" parameterType="com.buzl.main.entity.User">
insert into spring.user(<include refid="Base_Column_List" />) values(#{id},#{username},#{password})
</insert>
<!--更新數(shù)據(jù)庫中的數(shù)據(jù)-->
<update id="updateUserByPrimaryKey" parameterType="com.buzl.main.entity.User">
update spring.user set username=#{username},password=#{password} where id=#{id}
</update>
<!--通過 id 刪除數(shù)據(jù)-->
<delete id="deleteByPrimaryKey" parameterType="Integer">
delete from spring.user where id=#{id}
</delete>
</mapper>
二、代碼部分
1. 項(xiàng)目目錄結(jié)構(gòu):
2. User 實(shí)體類
package com.buzl.main.entity;
/**
* 實(shí)體類
*
* @author buzenglai@gmail.com
* @data 2023-07-10
*/
public class User {
/**
* 主鍵 id
*/
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
/**
* 重寫 toString 方法
* @return
*/
@Override
public String toString() {
return "User{" +
"id = " + id +
"\t username = " + username +
"\t password = " + password +
"}";
}
}
3. UserController 控制器類
package com.buzl.main.controller;
import com.buzl.main.mapper.UserMapper;
import com.buzl.main.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* controller 實(shí)現(xiàn)
*
* @author buzenglai@gmail.com
* @data 2023-07-10
*/
@RestController
@RequestMapping("/mybatis")
public class UserConroller {
@Resource
private UserMapper userMapper;
/**
* 查詢數(shù)據(jù)操作
*
* @return
*/
@GetMapping("/selectAll")
public List<User> selectAll() {
List<User> userList = userMapper.selectAll();
System.out.println("數(shù)據(jù)庫中所有的 user 數(shù)據(jù) :" + userList);
return userList;
}
/**
* 通過 id 查詢操作
*
* @return
*/
@GetMapping("/selectByPrimaryKey")
public User selectByPrimaryKey() {
User user = userMapper.selectByPrimaryKey(111);
System.out.println("通過 id 查詢出來的 user : " + user);
return user;
}
/**
* 插入數(shù)據(jù)
*
* @return
*/
@GetMapping("/insertSelective")
public String insertUser() {
User user = new User(123, "梅花", "pwd123");
userMapper.insertUser(user);
System.out.println("user :" + user + " 被成功插入數(shù)據(jù)庫");
return "insert data to database success!";
}
/**
* 更新數(shù)據(jù)
*
* @return
*/
@GetMapping("/updateByPrimaryKeySelective")
public String updateUserByPrimaryKey() {
Integer id = 123;
User user = new User(id, "劉備", "pwd666");
userMapper.updateUserByPrimaryKey(user);
System.out.println("數(shù)據(jù)庫中 id = " + id + " 的數(shù)據(jù)已經(jīng)被更新了");
return "database updated success!";
}
/**
* 刪除數(shù)據(jù)
*
* @return
*/
@GetMapping("/deleteByPrimaryKey")
public String deleteByPrimaryKey() {
Integer id = 123;
userMapper.deleteByPrimaryKey(id);
System.out.println("數(shù)據(jù)庫中 id = " + id + " 的 user 數(shù)據(jù)已經(jīng)被刪除了");
return "delete database success!";
}
}
4. UserMapper 接口
package com.buzl.main.mapper;
import com.buzl.main.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 接口類
*
* @author buzenglai@gmail.com
* @data 2023-07-10
*/
@Mapper // 表示這是一個(gè) Mybatis 的 mapper 類
@Repository
public interface UserMapper {
/**
* 向數(shù)據(jù)庫中插入一條數(shù)據(jù)
*
* @param user
* @return
*/
@Autowired
int insertUser(User user);
/**
* 通過主鍵 id 刪除數(shù)據(jù)
*
* @param id
* @return
*/
@Autowired
int deleteByPrimaryKey(Integer id);
/**
* 通過主鍵 id 刪除數(shù)據(jù)庫中的數(shù)據(jù)
*
* @param id
* @return
*/
@Autowired
User selectByPrimaryKey(Integer id);
/**
* 通過主鍵 id 更新數(shù)據(jù)庫中的數(shù)據(jù)
*
* @param user
* @return
*/
@Autowired
int updateUserByPrimaryKey(User user);
/**
* 查詢數(shù)據(jù)庫中所有的數(shù)據(jù)
*
* @return
*/
@Autowired
List<User> selectAll();
}
5. 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.buzl.main.mapper.UserMapper">
<!--共通屬性抽出-->
<sql id="Base_Column_List">
id,username,password
</sql>
<!--查詢數(shù)據(jù)庫中所有的數(shù)據(jù)-->
<select id="selectAll" resultType="com.buzl.main.entity.User">
<!--select * from spring.user-->
select <include refid="Base_Column_List" /> from spring.user
</select>
<!--通過 id 查詢數(shù)據(jù)-->
<select id="selectByPrimaryKey" resultType="com.buzl.main.entity.User">
select <include refid="Base_Column_List" /> from spring.user where id=#{id}
</select>
<!--向數(shù)據(jù)庫中插入數(shù)據(jù)-->
<insert id="insertUser" parameterType="com.buzl.main.entity.User">
insert into spring.user(<include refid="Base_Column_List" />) values(#{id},#{username},#{password})
</insert>
<!--更新數(shù)據(jù)庫中的數(shù)據(jù)-->
<update id="updateUserByPrimaryKey" parameterType="com.buzl.main.entity.User">
update spring.user set username=#{username},password=#{password} where id=#{id}
</update>
<!--通過 id 刪除數(shù)據(jù)-->
<delete id="deleteByPrimaryKey" parameterType="Integer">
delete from spring.user where id=#{id}
</delete>
</mapper>
6. 數(shù)據(jù)庫連接配置文件:
spring.datasource.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
7. pom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.buzl</groupId>
<artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>main</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springboot 集成 mybatis,導(dǎo)入依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mysql 驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.25</version>
</dependency>
<!--jdbc連接數(shù)據(jù)庫-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、測試
使用 ApiFox 工具進(jìn)行測試(用瀏覽器也是一樣)
- 查詢數(shù)據(jù)庫中所有的數(shù)據(jù):
- 通過 id 查詢數(shù)據(jù)庫中的數(shù)據(jù):
- 向數(shù)據(jù)庫中插入數(shù)據(jù):
- 通過 id 更改數(shù)據(jù)庫中的數(shù)據(jù)
文章來源:http://www.zghlxwxcb.cn/news/detail-579799.html
- 通過 id 刪除數(shù)據(jù)庫的數(shù)據(jù)
- 控制臺(tái)打印信息:
數(shù)據(jù)庫中所有的 user 數(shù)據(jù) :[User{id = 111 username = 張三 password = pwd111}, User{id = 222 username = 李四 password = pwd222}]
數(shù)據(jù)庫中所有的 user 數(shù)據(jù) :[User{id = 111 username = 張三 password = pwd111}, User{id = 222 username = 李四 password = pwd222}]
通過 id 查詢出來的 user : User{id = 111 username = 張三 password = pwd111}
user :User{id = 123 username = 梅花 password = pwd123} 被成功插入數(shù)據(jù)庫
數(shù)據(jù)庫中所有的 user 數(shù)據(jù) :[User{id = 111 username = 張三 password = pwd111}, User{id = 123 username = 梅花 password = pwd123}, User{id = 222 username = 李四 password = pwd222}]
數(shù)據(jù)庫中 id = 123 的數(shù)據(jù)已經(jīng)被更新了
數(shù)據(jù)庫中所有的 user 數(shù)據(jù) :[User{id = 111 username = 張三 password = pwd111}, User{id = 123 username = 劉備 password = pwd666}, User{id = 222 username = 李四 password = pwd222}]
數(shù)據(jù)庫中 id = 123 的 user 數(shù)據(jù)已經(jīng)被刪除了
數(shù)據(jù)庫中所有的 user 數(shù)據(jù) :[User{id = 111 username = 張三 password = pwd111}, User{id = 222 username = 李四 password = pwd222}]
這是結(jié)束標(biāo)志,結(jié)束了 ~文章來源地址http://www.zghlxwxcb.cn/news/detail-579799.html
到了這里,關(guān)于SpringBoot 集成 Mybatis的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!