Spring Boot 与 MyBatis-Flex 集成测试

项目具体结构如下

第 1 步:创建数据库表

-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `s_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'xiaoxue', '123');
INSERT INTO `student` VALUES ('2', 'qianxiao', '1234');

-- ----------------------------
-- Table structure for `student_grade`
-- ----------------------------
DROP TABLE IF EXISTS `student_grade`;
CREATE TABLE `student_grade` (
  `g_id` int(11) NOT NULL AUTO_INCREMENT,
  `s_id` int(11) NOT NULL,
  `course` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `teacher` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `grade` int(11) NOT NULL,
  PRIMARY KEY (`g_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student_grade
-- ----------------------------
INSERT INTO `student_grade` VALUES ('1', '1', '数学', '张三', '99');
INSERT INTO `student_grade` VALUES ('2', '1', '语文', '李四', '98');
INSERT INTO `student_grade` VALUES ('3', '1', '语文', '王五', '97');
INSERT INTO `student_grade` VALUES ('4', '2', '数学', '张三', '100');

第 2 步:创建Springboot工程
包依赖:
<dependencies>
    <dependency>
        <groupId>com.mybatis-flex</groupId>
        <artifactId>mybatis-flex-spring-boot-starter</artifactId>
        <version>1.9.7</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
    <!-- for test only -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>
第 3 步:application.yml 配置文件中添加 Mysql 数据库的相关配置

第 4 步:在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
第 5 步:Mybatis原生应用
Student.java:
package com.example.school.po;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.Data;

@Data
@Table("student")
public class Student {
    @Id(keyType = KeyType.Auto)
    private Integer s_id;
    private String username;
    private String password;
}
StudentGrade.java:
package com.example.school.po;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.Data;

@Data
@Table("student_grade")
public class StudentGrade {
    @Id(keyType = KeyType.Auto)
    private Integer g_id;
    private Integer s_id;
    private String course;
    private String teacher;
    private Integer grade;
}
Mapper 接口继承 BaseMapper 接口:
StudentMapper.java:
package com.example.school.mapper;

import com.example.school.po.Student;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface StudentMapper extends BaseMapper<Student> {

    @Select("select * from student where username=#{username} and password=#{password}")
    Student login(@Param("username") String username,@Param("password") String password);
}
StudentGradeMapper.java:
package com.example.school.mapper;

import com.example.school.po.StudentGrade;
import com.mybatisflex.core.BaseMapper;

import java.util.List;

public interface StudentGradeMapper extends BaseMapper<StudentGrade> {
//    根据学生id查询student_grade表中相关所有信息
//    select * from student_grade where s_id = 1
    List<StudentGrade> selectBySid(Integer s_id);

//    根据成绩id查询student_grade表中相关所有信息
//    select * from student_grade where g_id = 1
    List<StudentGrade> selectByGid(Integer g_id);
//    增加学生相关信息
//    insert into student_grade (s_id,course,teacher,grade) values (1,"英语","王五",97);
    int insertStudentGrade(StudentGrade studentGrades);
//    根据成绩id更改学生成绩相关信息
//    update student_grade set grade = 90 where g_id = 3
    int updateByGid(StudentGrade studentGrades);
//    根据成绩id删除学生成绩相关信息
//    delete from student_grade where g_id = 3
    int deleteByGid(Integer g_id);

}
StudentGradeMapper.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.example.school.mapper.StudentGradeMapper">
    <select id="selectBySid" parameterType="java.lang.Integer" resultType="com.example.school.po.StudentGrade">
        select * from student_grade where s_id = #{s_id}
    </select>
    <select id="selectByGid" parameterType="java.lang.Integer" resultType="com.example.school.po.StudentGrade">
        select * from student_grade where g_id = #{g_id}
    </select>
    <insert id="insertStudentGrade" parameterType="com.example.school.po.StudentGrade">
        insert into student_grade (s_id, course, teacher, grade) values (#{s_id}, #{course}, #{teacher}, #{grade})
    </insert>
    <!-- 更新学生成绩 -->
    <update id="updateByGid" parameterType="java.lang.Integer">
        update student_grade set grade = #{grade} where g_id = #{g_id}
    </update>
    <!-- 删除学生成绩 -->
    <delete id="deleteByGid" parameterType="java.lang.Integer">
        delete from student_grade where g_id = #{g_id}
    </delete>
</mapper>
第 6 步添加测试类,进行功能测试
SchoolApplicationTests.java
package com.example.school;

import com.example.school.mapper.StudentGradeMapper;
import com.example.school.mapper.StudentMapper;
import com.example.school.po.Student;
import com.example.school.po.StudentGrade;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SchoolApplicationTests {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentGradeMapper studentGradeMapper;
    //登录测试
    @Test
    void studentLogin() {
        Student student = studentMapper.login("xiaoxue","123");
        System.out.println(student);
    }
    //    根据学生id查询student_grade表中相关所有信息
    @Test
    void studentSelectBySid(){
        List<StudentGrade> studentGrades = studentGradeMapper.selectBySid(1);
        System.out.println(studentGrades);
    }
    //    根据成绩id查询student_grade表中相关所有信息
    @Test
    void studentSelectByGid(){
        List<StudentGrade> studentGrades = studentGradeMapper.selectByGid(1);
        System.out.println(studentGrades);
    }
    //    增加学生相关信息
    @Test
    public void InsertStudentGrade() {
        StudentGrade studentGrades = new StudentGrade();
        studentGrades.setS_id(2);
        studentGrades.setCourse("语文");
        studentGrades.setTeacher("王五");
        studentGrades.setGrade(90);
        int hang = studentGradeMapper.insertStudentGrade(studentGrades);
        System.out.println(hang);

    }
    //    根据成绩id更改学生成绩相关信息
    @Test
    void userinfoUpdateUserinfoLog() {
        StudentGrade studentGrades = new StudentGrade();
        studentGrades.setG_id(3);
        studentGrades.setGrade(88);
        int hang = studentGradeMapper.updateByGid(studentGrades);
        System.out.println(hang);
    }
    //    根据成绩id删除学生成绩相关信息
    @Test
    public void deleteStudentGradeByGid() {
        int hang = studentGradeMapper.deleteByGid(5);
        System.out.println(hang);
    }

}
测试结果如下
登录测试:
根据学生id查询student_grade表中相关所有信息:

根据成绩id查询student_grade表中相关所有信息:

增加学生相关信息:

添加前:

添加后:

根据成绩id更改学生成绩相关信息:

修改前:

修改后:

根据成绩id删除学生成绩相关信息:

删除前:

删除后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值