依賴pom.xml:
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>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>demoPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoPlus</name>
<description>demoPlus</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
<exclusions>
<exclusion>
<groupId>com.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
resource包下的Application.yml:
Application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/自己資料庫名字?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pojo包下的實體類User:
User
package com.demoplus.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@TableName("t_user") //目標表名
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mapper包下的介面UserMapper:
UserMapper
package com.demoplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demoplus.pojo.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
主啟動類DemoPlusApplication
DemoPlusApplication
package com.demoplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.demoplus.mapper")
public class DemoPlusApplication {
public static void main(String[] args) {
SpringApplication.run(DemoPlusApplication.class, args);
}
}
測試類DemoApplicationTest:
DemoApplicationTest
package com.demoplus;
import com.demoplus.mapper.UserMapper;
import com.demoplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLOutput;
import java.util.*;
import java.util.logging.Logger;
@SpringBootTest
class DemoPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private DataSource dataSource;
@Test
//檢驗資料庫連線
void dataSource() throws SQLException{
System.out.println(dataSource.getConnection());
}
// CREATE
@Test
//增加資訊
public void testInset(){
User user = new User();
user.setId(111L);
user.setName("qqml");
user.setAge(22);
user.setEmail("4399@qq.com");
int count = userMapper.insert(user);
System.out.println((count == 1 ? "增加成功" : "增加失敗"));
}
//delete
@Test
//透過id刪除
public void testDeleteById(){
int count = userMapper.deleteById(163L);
System.out.println((count == 1 ? "刪除成功" : "刪除失敗"));
}
@Test
//透過多個id批次刪除
public void testDeleteBatchIds(){
List<Long> idList = Arrays.asList(178L, 211L);
int count = userMapper.deleteBatchIds(idList);
System.out.println((count == 1 ? "刪除失敗" : "刪除成功"));
}
@Test
// 透過條件刪除
public void testDeleteByMap(){
Map<String, Object> map = new HashMap<>();
map.put("email", "9292@qq.com");
int count = userMapper.deleteByMap(map);
System.out.println((count == 1 ? "刪除成功" : "刪除失敗"));
}
//update
@Test
//根據id更新資料
public void testUpdateById(){
User user = new User(222L, "heige", 18, "2381@qq.com");
int count = userMapper.updateById(user);
System.out.println((count == 1 ? "更新成功" : "更新失敗"));
}
//retrieve
@Test
//根據id查詢使用者資訊
public void testSelectById(){
User user = userMapper.selectById(222L);
System.out.println(user);
}
@Test
//根據id列表查詢資料
public void testSelectBatchIds(){
List<Long> idList = Arrays.asList(225L, 256L);
List<User> userList = userMapper.selectBatchIds(idList);
userList.forEach(System.out::println);
}
@Test
//根據設定條件查詢資料
public void testSelectByMap(){
Map<String, Object> map = new HashMap<>();
map.put("age", 18);
List<User> userList = userMapper.selectByMap(map);
userList.forEach(System.out::println);
}
@Test
//直接查詢所有資料
public void selectList(){
List<User> user = userMapper.selectList(null);
user.forEach(System.out::println);
}
}
實現結果
檢測資料庫連線:
C(Create):
D(Delete):
U(Update)
R(Retrieve)