SpringBoot测试mapper接口
SpringBoot测试mapper接口
一、创建例子
1.建立数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`student_id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT ‘编号‘,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT ‘姓名‘,
`phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT ‘电话‘,
`email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT ‘邮箱‘,
`sex` tinyint NULL DEFAULT NULL COMMENT ‘性别‘,
`locked` tinyint NULL DEFAULT NULL COMMENT ‘状态(0:正常,1:锁定)‘,
`gmt_created` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘存入数据库的时间‘,
`gmt_modified` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ‘修改的时间‘,
PRIMARY KEY (`student_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = ‘学生表‘ ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
SET FOREIGN_KEY_CHECKS = 1;
2.创建SpringBoot项目
- 配置数据源
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: xxx
password: xxx
mybatis-plus:
mapper-locations: classpath*:/mapper/**Mapper.xml
server:
port: 8081
-
添加mapper接口
List
selectByStudentSelective(Student student); -
对应的xml
student_id,name,phone,email,sex,locked,gmt_created,gmt_modified 测试mapper接口
import com.mrdouym.demo2.entity.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; /** * @program: demo2 * @description: SpringBoot测试mapper接口 * @create: 2021-07-12 15:38 * @author: MrdouYM **/ @SpringBootTest @RunWith(SpringRunner.class) //@RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("test") public class StudentMapperTest { @Autowired private StudentMapper studentMapper; @Test public void test(){ List
list = new ArrayList<>(); Student student=new Student(); //student.setName("xxx"); student.setSex(1); list=studentMapper.selectByStudentSelective(student); System.out.println("================================================================="); for (Student student1 : list) { System.out.println(student1); } System.out.println("================================================================="); } }
@RunWith(SpringRunner.class)继承了@RunWith(SpringJUnit4ClassRunner.class),两者都可。
测试成功!
原文:https://www.cnblogs.com/blogBydym/p/15005727.html