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;
# 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<Student> selectByStudentSelective(Student student);
对应的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.mrdouym.demo2.mapper.StudentMapper">
<sql id="List"> student_id,name,phone,email,sex,locked,gmt_created,gmt_modified
</sql>
<select id="selectByStudentSelective" resultType="com.mrdouym.demo2.entity.Student">
select
<include refid="List" />
from student
where 1=1
<if test="name != null and name !=‘‘">
and name like concat(‘%‘, #{name}, ‘%‘)
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</select>
</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<Student> 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