首页 > 移动平台 > 详细

SpringBoot测试mapper接口

时间:2021-07-13 15:08:19      阅读:13      评论:0      收藏:0      [点我收藏+]

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<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>
    
    

    测试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),两者都可。

技术分享图片

测试成功!

SpringBoot测试mapper接口

原文:https://www.cnblogs.com/blogBydym/p/15005727.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!