SpringBoot3+Mybatis-Plus+h2数据库,入门Mybatis-Plus
- mybatis-plus官网地址
- maven依赖
- 数据库脚本
- 配置文件
- 实体类
- Mapper
- 入门程序
- 启动程序测试
- 单元测试
- 测试结果
- Service层接口
- service层接口单元测试
- 测试结果
- 项目结构
mybatis-plus官网地址
https://www.baomidou.com/
maven依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.0</version><relativePath/> <!-- lookup parent from repository -->
</parent><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- h2数据库依赖 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!--mybatis-plus配置--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.6</version></dependency><!--mybatis-spring的配置--><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><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
数据库脚本
src/main/resources/db/schema-h2.sql
DROP TABLE IF EXISTS sys_user;CREATE TABLE sys_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)
);
src/main/resources/db/data-h2.sql
DELETE FROM sys_user;INSERT INTO sys_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');
配置文件
src/main/resources/application.yml
server:# 端口port: 9090
# 数据库配置
spring:datasource:# 驱动名称driver-class-name: org.h2.Driver# 自定义用户名username: root# 自定义密码password: test# 数据库地址:jdbc:h2:mem:数据库名称(随意填写,会自动创建url: jdbc:h2:mem:testh2:console:enabled: truepath: /h2settings:web-allow-others: truesql:init:platform: h2# 数据库初始脚本schema-locations: classpath:db/schema-h2.sql# 数据库中表的初始脚本data-locations: classpath:db/data-h2.sql
# 日志输出级别
logging:level:com.example: debug
实体类
package org.example.entity;import lombok.Data;/*** @author 16837*/
@Data
public class SysUser {private Long id;private String name;private Integer age;private String email;
}
Mapper
package org.example.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.entity.SysUser;/*** @author 16837*/
public interface SysUserMapper extends BaseMapper<SysUser> {
}
入门程序
package org.example;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author 16837*/
@MapperScan("org.example.mapper")
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
启动程序测试
访问路径: http://localhost:9090/h2
登录成功后的界面
单元测试
package org.example;import jakarta.annotation.Resource;
import org.example.entity.SysUser;
import org.example.mapper.SysUserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTest {@Resourceprivate SysUserMapper userMapper;@Testpublic void testSelect() {System.out.println(("----- selectAll method test ------"));List<SysUser> userList = userMapper.selectList(null);userList.forEach(System.out::println);}}
测试结果
Service层接口
package org.example.service;import com.baomidou.mybatisplus.extension.service.IService;
import org.example.entity.SysUser;public interface SysUserService extends IService<SysUser> {
}
package org.example.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.SysUser;
import org.example.mapper.SysUserMapper;
import org.example.service.SysUserService;
import org.springframework.stereotype.Service;@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
}
service层接口单元测试
package org.example;import jakarta.annotation.Resource;
import org.example.entity.SysUser;
import org.example.mapper.SysUserMapper;
import org.example.service.SysUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTest {@Resourceprivate SysUserService userService;@Testpublic void testSelect() {System.out.println(("----- selectAll method test ------"));List<SysUser> userList = userService.list();userList.forEach(System.out::println);}}