目录
环境准备
MySQL,JDK17,Maven,MyBatis
库,表
库:
CREATE DATABASE StuDB;
学生表:Student(Sno,Sname,Ssex,Sage,Sdept) 其中Sno 为主键、Ssex取值为男或女、Sage在15到30之间:
Sno | Sname | Ssex | Sage | Sdept |
---|---|---|---|---|
95001 | 李敏勇 | 男 | 20 | CS |
95002 | 刘晨 | 女 | 19 | IS |
95003 | 王敏 | 女 | 18 | MA |
95004 | 张立 | 男 | 18 | IS |
课程表:Course(Cno,Cname,Cpno,Credeit,remarks) 其中Cno为主键
Cno | Cname | Cpno | Credit | Teacher |
---|---|---|---|---|
1 | 数据库 | 5 | 4 | 王芳 |
2 | 数学 | NULL | 2 | 刘新 |
3 | 信息系统 | 1 | 4 | 刘新 |
4 | 操作系统 | 6 | 3 | 高升 |
5 | 数据结构 | 7 | 4 | 宋明 |
6 | 数据处理 | NULL | 2 | 张彬 |
7 | Pascal语言 | 6 | 4 | 李磊 |
学生选修表:SC(Sno,Cno,Grade) 其中Sno,Cno为主键同时又为外键、Grade值在0到100;
Sno | Cno | Grade |
---|---|---|
95001 | 1 | 92 |
95001 | 2 | 85 |
95001 | 3 | 88 |
95002 | 2 | 90 |
95003 | 2 | 55 |
95004 | 2 | 70 |
CREATE TABLE Student
(
Sno char(5)PRIMARY KEY,
Sname char(10)NOT NULL,
Ssex char(2)CHECK(Ssex IN('男','女')),
Sage int CHECK
(Sage>=15 AND Sage<=30),
Sdept char(5) NULL
);
CREATE TABLE Course
(
Cno int PRIMARY KEY,
Cname varchar(20) NOT NULL,
Cpno char(2) NULL,
Credit int,
Teacher char(20) NOT NULL
);
CREATE TABLE SC
(
Sno char(5),
Cno int,
Grade int CHECK (Grade BETWEEN 0 AND 100),
PRIMARY KEY(Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
);
INSERT INTO Student
SELECT '95001', '李敏勇', '男', 20, 'CS'
UNION
SELECT '95002', '刘晨', '女', 19, 'IS'
UNION
SELECT '95003', '王敏', '女', 18, 'MA'
UNION
SELECT '95004', '张立', '男', 18, 'IS';
INSERT INTO Course
SELECT 1, '数据库', 5, 4, '王芳'
UNION
SELECT 2, '数学', NULL, 2, '刘新'
UNION
SELECT 3, '信息系统', 1, 4, '刘新'
UNION
SELECT 4, '操作系统', 6, 3, '高升'
UNION
SELECT 5, '数据结构', 7, 4, '宋明'
UNION
SELECT 6, '数据处理', NULL, 2, '张彬'
UNION
SELECT 7, 'Pascal语言', 6, 4, '李磊';
INSERT INTO SC
SELECT '95001', 1, 92
UNION
SELECT '95001', 2, 85
UNION
SELECT '95001', 3, 88
UNION
SELECT '95002', 2, 90
UNION
SELECT '95003', 2, 55
UNION
SELECT '95004', 2, 70;
idea准备
项目结构:
-
创建maven项目导入坐标
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> </dependencies>
-
编写mybatis-config.xml核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///StuDB"/> <property name="username" value=""/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <!--SQL映射文件--> </mappers> </configuration>
-
在com.YiZaiA.practice.pojo目录下创建对应的实体类
-
Student类
package com.YiZaiA.practice.pojo; public class Student { private String ID; private String name; private String gender; private int age; private String dept; public Student() { } public Student(String ID, String name, String gender, int age, String dept) { this.ID = ID; this.name = name; this.gender = gender; this.age = age; this.dept = dept; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } @Override public String toString() { return "Student{" + "ID='" + ID + '\'' + ", name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + ", dept='" + dept + '\'' + '}'; } }
-
Course类
package com.YiZaiA.practice.pojo; public class Course { private int Cno; private String name; private String optionalCourse; private int credit; private String teacher; public Course() { } public Course(int cno, String name, String optionalCourse, int credit, String teacher) { Cno = cno; this.name = name; this.optionalCourse = optionalCourse; this.credit = credit; this.teacher = teacher; } public int getCno() { return Cno; } public void setCno(int cno) { Cno = cno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOptionalCourse() { return optionalCourse; } public void setOptionalCourse(String optionalCourse) { this.optionalCourse = optionalCourse; } public int getCredit() { return credit; } public void setCredit(int credit) { this.credit = credit; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } @Override public String toString() { return "Course{" + "Cno=" + Cno + ", name='" + name + '\'' + ", optionalCourse='" + optionalCourse + '\'' + ", credit=" + credit + ", teacher='" + teacher + '\'' + '}'; } }
-
SC类
package com.YiZaiA.practice.pojo; public class SC { private String ID; private int Cno; private int grade; public SC() { } public SC(String ID, int cno, int grade) { this.ID = ID; Cno = cno; this.grade = grade; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public int getCno() { return Cno; } public void setCno(int cno) { Cno = cno; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } @Override public String toString() { return "SC{" + "ID='" + ID + '\'' + ", Cno=" + Cno + ", grade=" + grade + '}'; } }
-
-
修改mybatis-config.xml核心配置文件,(起别名,加载SQL映射文件,包扫描形式)
<typeAliases> <package name="com.YiZaiA.practice.pojo"/> </typeAliases>
<mappers> <package name="com.YiZaiA.practice.mapper"/> </mappers>
-
在main/resources目录com.YiZaiA.practice.mapper路径下创建实体类对应的SQL映射文件
-
在main/java目录com.YiZaiA.practice.mapper路径下创建SQL映射文件对应的接口
-
在test目录com.YiZaiA.practice路径下创建TestQuery,TestUpdate,TestInsertmTestDelete四个测试类
题目
一、简单查询
-
给定0或多个有效字段(其中0表示所有字段),要求查询全体学生的相关字段信息,并封装成一个学生对象集合返回。
-
本题考察的是mybatis查询动态列,可以使用一个字符串数组来接收字段信息
<!--接口原型:List<StudentMapper> title01(@Param("columns")String[] arr);--> <select id="title01" resultMap="StudentResultMap"> SELECT <choose> <when test="columns != null and columns.length > 0"> <foreach collection="columns" item="column" separator=","> ${column} </foreach> </when> <otherwise> * </otherwise> </choose> FROM Student; </select>
-
相关知识点:
- 此处用到了choose(when,otherwise),foreach动态SQL
- 在使用MyBatis传入数组或者list集合类型的参数的时候如果要用自己的参数名那就必须加上@Param("自己的参数名"),详见上述接口原型;原因:在mybatis中如果传入数组类型的参数则其会被转换成Map,而这个Map的key是array ,value 则是数组里面的值。Map<array,value>,不加@Param的话上边的columns就只能写成array
- 占位符应该用${},用#{}的话可能会出错
-
-
给一课程号,查询选择了这一课程的学生信息
-
<!--List<Student> title02(int ID);--> <select id="title02" resultMap="StudentResultMap"> SELECT * FROM Student WHERE Sno IN ( SELECT Sno From SC WHERE Cno = #{ID} ) </select>
-
未完待续
标签:String,int,练习,public,MyBatis,ID,SELECT,name From: https://www.cnblogs.com/YiZaiA/p/16905063.html