springboot xml 数据库访问 联表查询
创始人
2024-04-11 09:00:39
0

springboot xml 数据库访问 联表查询

练习数据库表:

course表

字段名数据类型&长度是否为空主键/外键描述
idint主键,自增课程ID
course_nochar(8)课程编号,唯一
course_namevarchar(40)课程名称
course_prior_idint外键先修课程id
course_creditsmallint学分

student表

字段名数据类型&长度是否为空主键/外键描述
idint主键,自增学生ID
stu_nochar(9)学号,唯一
stu_namevarchar(20)姓名
stu_sexchar(2)性别(男或女)
stu_ageint年龄
stu_deptChar(15)所在院系

sc表

字段名数据类型&长度是否为空主键外键描述
idint主键,自增选课ID
stu_idint外键学生id
course_idint外键课程id
gradeint成绩0~100之间

建立Spring boot 项目,并初始化对应依赖

实体类

Course

package com.example.sqltest.enity;import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("course")
public class Course {@TableId("id")private Integer id;private String courseNo;private String courseName;private Integer coursePriorId;private Integer courseCredit;private Score score;// 为了联表查询
}

sc

package com.example.sqltest.enity;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("sc")
public class Score {private Integer id;private Integer stuId;private Integer courseId;private Integer grade;
}

student:

package com.example.sqltest.enity;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("student")
public class Student {private Integer id;private String stuNo;private String stuName;@TableField("stu_sex")private String stuSex;private Integer stuAge;@TableField("stu_dept")private String stuDept;
}

基本查询和插入

完成这类操作,不需要联表,使用注解@Mapper,@Select,@Insert基本可以实现。

例如:实现选课信息表的添加功能,要求一个学生一门课程只能选修一次。

@Insert("insert into sc(stu_id,course_id) values (#{stu_id},#{course_id})")
void insertCourse(Integer stu_id,Integer course_id);

动态SQL

这一类部分也可以使用注解来完成相对应的功能。

例如:实现学生信息表的查询功能,要求:查询条件中包含学号,姓名,性别以及院系,返回符合查询条件的学生信息。只要完成AND条件的组合即可。例如:输入学号为NULL,姓名为NULL,性别为女,院系为SS,则返回SS系的所有女生信息

@Select("select * from student where (stu_no = #{stuNo} or #{stuNo} is null) and (stu_name = #{stuName} or #{stuName} is null) and (stu_sex= #{stuSex} or #{stuSex} is null)  and (stu_dept = #{stuDept} or #{stuDept} is null)")List getStudents(String stuNo,String stuName,String stuSex,String stuDept);

当然,也可以使用xml动态SQL:


xml联表查询

使用SQL语句对数据库的两个或多个表进行联表查询

例如:返回所有课程的选修情况,要求结果中包含课程信息,以及选课信息。不需要把选课学生的详情列出,必须使用关联映射实现。

mybatis官网:https://mybatis.net.cn/sqlmap-xml.html

application主启动类加

@MapperScan(basePackages = "com.example.sqlTest.Mapper")

扫描自己建立的Mapper接口类

resourses下建文件夹存xml





    

application.yml加配置项:

mybatis:mapper-locations: classpath:mapper/*Mapper.xml

Junit测试

在测试类里面写测试方法,写上一些逻辑判断,调用

package com.example.sqltest;import com.example.sqltest.Mapper.CourseMapper;
import com.example.sqltest.enity.Course;
import com.example.sqltest.enity.CourseScore;
import com.example.sqltest.enity.Student;
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 SqlTestApplicationTests {@AutowiredCourseMapper courseMapper;@Testvoid insertCourse() {Integer stu_id = 1;String course_name = "计算机网络";Integer course_id = courseMapper.getCourseNameByName(course_name);if(course_id == null) {System.out.println("课程不存在");}System.out.println(course_id);System.out.println(courseMapper.getScById(course_id));if(courseMapper.getScById(course_id).size() == 0){courseMapper.insertCourse(stu_id,course_id);System.out.println("选课成功");}else{System.out.println("已选课");}}@Testvoid getStudents() {String stuNo = null;String stuName = null;String stuSex = "女";String stuDept = "CS";List students = courseMapper.getStudents(stuNo,stuName,stuSex,stuDept);if(students.size() == 0){System.out.println("学生不存在");}else{System.out.println("学生如下:");for (Student student : students) {System.out.println(student);}}}@Testvoid getAllChoose() {List list = courseMapper.getAllChoose();if(list.size() == 0){System.out.println("没有选课");return;}for (Object o : list) {System.out.println(o);}}
}

SQL脚本:


create database  exam1;
use exam1;-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (`id` int(0) NOT NULL AUTO_INCREMENT,`stu_no` char(9),`stu_name` varchar(20) ,`stu_sex` char(2) ,`stu_age` int(0) NULL DEFAULT NULL,`stu_dept` char(15) ,PRIMARY KEY (`id`) 
) ;-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '201815001', '李晨', '女', 22, 'SS');
INSERT INTO `student` VALUES (2, '201815002', '张毅', '男', 20, 'SS');
INSERT INTO `student` VALUES (3, '201815003', '杨磊', '女', 20, 'SS');
INSERT INTO `student` VALUES (4, '201815004', '张丰毅', '男', 19, 'SS');
INSERT INTO `student` VALUES (5, '201816001', '王敏', '女', 22, 'CS');
INSERT INTO `student` VALUES (6, '201816002', '李蕾', '女', 21, 'CS');
INSERT INTO `student` VALUES (7, '201817001', '李贵', '男', 21, 'MA');
INSERT INTO `student` VALUES (8, '201817002', '严丽', '女', 20, 'MA');
INSERT INTO `student` VALUES (9, '201817003', '沈菁菁', '女', 21, 'MA');-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course`  (`id` int(0) NOT NULL AUTO_INCREMENT,`course_no` char(8),`course_name` varchar(40) ,`course_prior_id` int(0) NULL DEFAULT NULL,`course_credit` smallint(0) NULL DEFAULT NULL,PRIMARY KEY (`id`) ,INDEX `Cpno`(`course_prior_id`) ,CONSTRAINT `course_ibfk_1` FOREIGN KEY (`course_prior_id`) REFERENCES `course` (`id`) 
) ;-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (1, '6324567', '数据处理', NULL, 4);
INSERT INTO `course` VALUES (2, '6234322', '数学', NULL, 2);INSERT INTO `course` VALUES (4, '6256774', '操作系统', 1, 4);
INSERT INTO `course` VALUES (7, '6203183', 'pascal语言', 1, 4);
INSERT INTO `course` VALUES (5, '6228723', '数据结构', 7, 4);
INSERT INTO `course` VALUES (6, '6203456', '数据库系统概论', 5, 2);INSERT INTO `course` VALUES (8, '6203752', '大学英语', NULL, 4);
INSERT INTO `course` VALUES (9, '6203496', '计算机网络', NULL, 4);
INSERT INTO `course` VALUES (3, '6254374', '信息系统', 6, 3);-- ----------------------------
-- Table structure for sc
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc`  (`id` int(0) NOT NULL AUTO_INCREMENT,`stu_id` int(0) NOT NULL,`course_id` int(0) NOT NULL,`grade` int(0) NULL DEFAULT NULL,PRIMARY KEY (`id`),INDEX `FK_sc_course`(`course_id`) ,INDEX `stu_id`(`stu_id`),CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `student` (`id`) ,CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) 
) ;-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES (1, 1, 1, 90);
INSERT INTO `sc` VALUES (2, 1, 2, 85);
INSERT INTO `sc` VALUES (3, 2, 1, 86);
INSERT INTO `sc` VALUES (4, 2, 2, 90);
INSERT INTO `sc` VALUES (5, 2, 4, 67);
INSERT INTO `sc` VALUES (6, 3, 1, 85);select * from student;
select * from sc;
select * from course;

相关内容

热门资讯

新还珠格格,欣荣和永琪有个孩子... 新还珠格格,欣荣和永琪有个孩子?不是说永琪从来都没碰过她吗?绵忆到底是他和小燕子的还是欣荣的啊求正解...
中级会计怎么备考?今年几月考试... 中级会计怎么备考?今年几月考试?您好,很高兴为您解答中级会计师考试,教材是根本和基础,所有的题目都是...
继兴业、招商、中信后,邮储银行... (来源:现代商业银行杂志)金融资产投资公司(AIC)队伍再添新员。邮储银行近日发布公告称,该行拟以自...
中央巡视组对陕西开展两个半月常... 转自:北京日报客户端日前,中央第十五巡视组进驻陕西省,将开展为期两个半月左右的常规巡视,并会同陕西省...
柳州幻境空间在哪里 柳州幻境空间在哪里柳州幻境空间是位于广西柳州市城中区华联商闷郑城4楼的室内主题乐园,提供了各种游戏和...
中央巡视组进驻山东 联动巡视济... 转自:央视新闻客户端经党中央批准,二十届中央第六轮巡视将对16个省(自治区、直辖市)开展常规巡视,并...
继续发布暴雨蓝色预警!北京等地... 转自:央视新闻客户端中央气象台19日早6时继续发布暴雨蓝色预警。预计,19日早8时至20日早8时,青...
降妖伏魔篇演员有哪些 降妖伏魔篇演员有哪些文章舒淇程小东黄勃
晚上十一点在河边抓鱼听到有人叫... 晚上十一点在河边抓鱼听到有人叫我小名声音跟我一个朋友一样,电筒照却没有发现有人而且我女朋友也听见了不...
属猴的为什么吸引属狗的人 属猴的为什么吸引属狗的人属相狗虽不善甜言蜜语,为人多有情感之被捉,然其铅轮内心却多有向往甜蜜幸福之生...