[Mybatis] xml development of Mybatis - 4 cases to take you to a quick start

Table of contents

database

practice questions


database

1. Type table

CREATE TABLE `tb_teacher_type` (
  `type_id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `content` VARCHAR(50) COMMENT '类型'
);

INSERT  INTO `tb_teacher_type`(`type_id`,`content`) VALUES (1,'授课老师');
INSERT  INTO `tb_teacher_type`(`type_id`,`content`) VALUES (2,'助理老师');
INSERT  INTO `tb_teacher_type`(`type_id`,`content`) VALUES (3,'辅导员老师');

2. Teacher form

CREATE TABLE `tb_teacher` (
  `tid` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `tname` VARCHAR(50) DEFAULT NULL COMMENT '老师姓名',
  `type` INT(11) DEFAULT NULL COMMENT '老师类型ID'
);
INSERT  INTO `tb_teacher`(`tid`,`tname`,`type`) VALUES (1,'梁桐老师',1);
INSERT  INTO `tb_teacher`(`tid`,`tname`,`type`) VALUES (2,'马坤老师',2);
INSERT  INTO `tb_teacher`(`tid`,`tname`,`type`) VALUES (3,'仲燕老师',3);
INSERT  INTO `tb_teacher`(`tid`,`tname`,`type`) VALUES (4,'袁新奇老师',1);
INSERT  INTO `tb_teacher`(`tid`,`tname`,`type`) VALUES (5,'任林达老师',2);
INSERT  INTO `tb_teacher`(`tid`,`tname`,`type`) VALUES (6,'王珊珊老师',3);

3. Class table

CREATE TABLE `tb_class` (
  `cid` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `cname` VARCHAR(50) DEFAULT NULL COMMENT '班级名称',
  `teacher1_id` INT(11) DEFAULT NULL COMMENT '授课老师',
  `teacher2_id` INT(11) DEFAULT NULL COMMENT '助理老师',
  `teacher3_id` INT(11) DEFAULT NULL COMMENT '辅导员老师'
);

INSERT  INTO `tb_class`(`cid`,`cname`,`teacher1_id`,`teacher2_id`,`teacher3_id`) VALUES (1,'Java56',1,2,3);
INSERT  INTO `tb_class`(`cid`,`cname`,`teacher1_id`,`teacher2_id`,`teacher3_id`) VALUES (2,'Java78',1,2,3);
INSERT  INTO `tb_class`(`cid`,`cname`,`teacher1_id`,`teacher2_id`,`teacher3_id`) VALUES (3,'Java12',4,5,6);
INSERT  INTO `tb_class`(`cid`,`cname`,`teacher1_id`,`teacher2_id`,`teacher3_id`) VALUES (4,'Java34',4,5,6);

4. City table

CREATE TABLE tb_city(
  c_id VARCHAR(32) PRIMARY KEY COMMENT '城市ID',
  city_name VARCHAR(20) COMMENT '城市名称' ,
  parent_id VARCHAR(32) COMMENT '父ID'
);

INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('320000','江苏省','0');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140000','山西省','0');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('130000','河北省','0');

INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('320100','南京市','320000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('320102','玄武区','320100');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('320103','白下区','320100');

INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('321300','宿迁市','320000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('321322','沭阳县','321300');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('321323','泗阳县','321300');


INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140100','太原市','140000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140106','迎泽区','140100');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140108','尖草坪区','140100');

INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140800','运城市','140000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140823','闻喜县','140800');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('140828','夏 县','140800');

INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('130100','石家庄市','130000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('130127','高邑县','130100');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('130185','鹿泉市','130100');

INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('131000','廊坊市','130000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('131003','广阳区','131000');
INSERT INTO tb_city(c_id,city_name,parent_id) VALUES('131022','固安县','131000');

5. Student table

CREATE TABLE `tb_student` (
  `s_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学生ID',
  `sname` VARCHAR(50) DEFAULT NULL COMMENT '姓名',
  `age` INT(11) DEFAULT NULL COMMENT '年龄',
  `birthday` DATETIME DEFAULT NULL COMMENT '生日',
  `gender` CHAR(1) DEFAULT NULL COMMENT '性别',
  `c_id` INT DEFAULT NULL COMMENT '班级ID',
  `province_id` VARCHAR(32) DEFAULT NULL COMMENT '省ID',
  `city_id` VARCHAR(32) DEFAULT NULL COMMENT '市ID',
  `county_id` VARCHAR(32) DEFAULT NULL COMMENT '县ID'
);

INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (1,'赵三33',21,'2001-01-17 00:00:00','1',1,'320000','321300','321322');
INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (2,'钱四444',1900,'2001-05-16 00:00:00','1',2,'320000','321300','321322');
INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (3,'孙五56',189,'2022-03-15 00:00:00','0',1,'320000','321300','321322');
INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (4,'张三',20,'2020-12-21 00:00:00','0',2,'320000','321300','321322');
INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (5,'xxx',18,'2020-12-21 00:00:00','0',2,'140000','140800','140823');
INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (6,'123',18,'2020-11-01 00:00:00','0',3,'130000','130100','130127');
INSERT  INTO `tb_student`(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`,`province_id`,`city_id`,`county_id`) VALUES (7,'xx',18,'2020-11-02 00:00:00','0',1,'130000','131000','131003');

6. Class schedule

CREATE TABLE `tb_course` (
  `c_id` INT NOT NULL PRIMARY KEY COMMENT '课程ID',
  `cname` VARCHAR(50) DEFAULT NULL COMMENT '课程名称',
  `desc` VARCHAR(100) DEFAULT NULL COMMENT '课程描述'
);

INSERT  INTO `tb_course`(`c_id`,`cname`,`desc`) VALUES (1,'Java基础','JavaSE所有课程');
INSERT  INTO `tb_course`(`c_id`,`cname`,`desc`) VALUES (2,'JavaWeb','Java Web 所有课程');
INSERT  INTO `tb_course`(`c_id`,`cname`,`desc`) VALUES (3,'SSM','Spring Mvc、Spring、MyBatis所有课程');

7. Intermediate table

-- 学生课程中间表
CREATE TABLE `tb_student_course` (
  `s_id` INT NOT NULL COMMENT '学生ID',
  `c_id` INT NOT NULL COMMENT '课程ID',
  `score` DOUBLE DEFAULT NULL,
  PRIMARY KEY (`s_id`,`c_id`)
);

INSERT INTO tb_student_course(s_id,c_id,score) VALUES(1,1,100);
INSERT INTO tb_student_course(s_id,c_id,score) VALUES(1,2,95);
INSERT INTO tb_student_course(s_id,c_id,score) VALUES(1,3,NULL);

INSERT INTO tb_student_course(s_id,c_id,score) VALUES(2,1,100);
INSERT INTO tb_student_course(s_id,c_id,score) VALUES(2,2,95);
INSERT INTO tb_student_course(s_id,c_id,score) VALUES(2,3,100);

INSERT INTO tb_student_course(s_id,c_id,score) VALUES(3,1,80);
INSERT INTO tb_student_course(s_id,c_id,score) VALUES(3,2,NULL);
INSERT INTO tb_student_course(s_id,c_id,score) VALUES(3,3,90);


-- 老师课程中间表
create table `tb_teacher_course`(
	`tid` int not null COMMENT '老师id',
	`c_id` int not null COMMENT '课程id',
	PRIMARY KEY (`tid`,`c_id`)
);

insert into tb_teacher_course(tid,c_id) values(1,1);
insert into tb_teacher_course(tid,c_id) values(1,2);
insert into tb_teacher_course(tid,c_id) values(1,3);
insert into tb_teacher_course(tid,c_id) values(2,3);
insert into tb_teacher_course(tid,c_id) values(3,1);
insert into tb_teacher_course(tid,c_id) values(4,1);
insert into tb_teacher_course(tid,c_id) values(4,2);
insert into tb_teacher_course(tid,c_id) values(4,3);
insert into tb_teacher_course(tid,c_id) values(5,2);
insert into tb_teacher_course(tid,c_id) values(5,3);
insert into tb_teacher_course(tid,c_id) values(6,3);

practice questions

1. Query all teachers, and at the same time

  • Query the corresponding type of teacher

  • Inquire about courses taught by teachers

2. Query all classes, at the same time

  • Find the teacher corresponding to the class

  • Query the total number of students in the class

  • Query the details of the students included in the class

3. Query all students, and at the same time

  • Query the class a student belongs to

  • Query student province, city and county information

  • Query students' courses

4. Query all courses, and at the same time

  • Query the total number of students studying the course

  • Query the details of students studying the course

Requirements: While inquiring about everything, each relevant two-way information must be specified.

For example: there are students in the class, the total number of students, and the teacher; the corresponding class information among the students; at the same time, each student has the address information of the province, city and county, and the student is in the class. When querying all classes, it is necessary to query all the classes, as well as the total number of students corresponding to the class and the student details of the class including students, and the student details include class information and address information.

query all teachers

query all classes

query all students

 Find all courses

Check out the resources for answers. 

Guess you like

Origin blog.csdn.net/zsy3757486/article/details/130494287