依赖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:
Aollication.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(Read)文章来源:https://www.toymoban.com/news/detail-841764.html
文章来源地址https://www.toymoban.com/news/detail-841764.html
到了这里,关于SpringBoot整合Mybatis-Plus(SpringBoot3)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!