SpringBoot 3.2.0 集成MyBatisPlus
2023-12-26 18:28:15
依赖版本
- JDK 17
- Spring Boot 3.2.0
- MyBatis Plus 3.5.4.1
集成MyBatis-Plus
工程源码: Gitee
导入依赖
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.2.0</spring-boot.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<mybatis-spring.version>3.0.3</mybatis-spring.version>
<mybatis-plus.version>3.5.4.1</mybatis-plus.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>${mybatis-plus.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
编写application.yml
配置文件
# ======== 数据源配置 ========
spring:
datasource:
driver-class-name: org.h2.Driver
username: root
password: test
sql:
init:
schema-locations: classpath:db/schema-h2.sql
data-locations: classpath:db/data-h2.sql
# ======== MybatisPlus配置 ========
mybatis-plus:
# 搜索指定包别名
type-aliases-package: com.yiyan.study.domain
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath:mapper/**/*xml
# 配置驼峰
configuration:
# 是否开启自动驼峰命名规则(camel case)映射。默认值:true
map-underscore-to-camel-case: true
# 开启 Mybatis 二级缓存,默认为 true。
cache-enabled: true
# 日志输出实现。StdOutImpl 标准日志控制台输出
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
# 全局默认主键类型
id-type: assign_id
# 表名是否使用驼峰转下划线命名,只对表名生效。 默认true
table-underline: true
编写基于MyBatisPlus的数据库操作
-- data-h2.sql
DELETE
FROM `user`;
INSERT INTO `user` (id, name, age, email)
VALUES (1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
-- schema-h2.sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
id BIGINT NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
实体类
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@TableName(value = "`user`")
@Data
public class User implements Serializable {
@TableId
private Long id;
private String name;
private Integer age;
private String email;
@Serial
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
DAO(mapper)
import com.yiyan.study.domain.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IUserDao extends BaseMapper<User> {
}
Service
import com.yiyan.study.domain.User;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IUserService extends IService<User> {
}
ServiceImpl
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyan.study.domain.User;
import com.yiyan.study.service.IUserService;
import com.yiyan.study.dao.IUserDao;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<IUserDao, User>
implements IUserService{
}
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.yiyan.study.dao.IUserDao">
</mapper>
编写测试用例
package com.yiyan.study;
import com.yiyan.study.dao.IUserDao;
import com.yiyan.study.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@Slf4j
@SpringBootTest
@DisplayName("MyBatis Plus Test")
public class MyBatisPlusTest {
@Autowired
private IUserDao userDao;
@Test
@DisplayName("MyBatis Plus Select Test")
public void testSelect() {
log.info(("----- selectAll method test ------"));
List<User> userList = userDao.selectList(null);
for (User user : userList) {
log.info(user.toString());
}
}
@Test
@DisplayName("MyBatis Plus Insert Test")
public void testInsert() {
log.info(("----- insert method test ------"));
User user = new User();
user.setName("Alex");
user.setAge(18);
user.setEmail("alex@gmail.com");
int result = userDao.insert(user);
log.info("insert result: {}", result);
User selectedById = userDao.selectById(user.getId());
log.info("insert user info: {}", selectedById);
}
@Test
@DisplayName("MyBatis Plus Update Test")
public void testUpdate() {
log.info(("----- update method test ------"));
User user = userDao.selectList(null).get(0);
log.info("before update: {}", user);
user.setName("Updated Name");
int result = userDao.updateById(user);
log.info("update result: {}", result);
User selectedById = userDao.selectById(user.getId());
log.info("after update: {}", selectedById);
}
@Test
@DisplayName("MyBatis Plus Delete Test")
public void testDelete() {
log.info("----- delete method test ------");
List<User> users = userDao.selectList(null);
log.info("before delete user list size: {}", users.size());
User user = users.get(0);
userDao.deleteById(user.getId());
log.info("delete user id: {}", user.getId());
List<User> usersAfterDelete = userDao.selectList(null);
log.info("after delete user list size: {}", usersAfterDelete.size());
}
}
文章来源:https://blog.csdn.net/m0_55712478/article/details/135189873
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!