Mybas 的高级应用
实验内容:
1、通过 sql 片段、if 判断实现动态 sql 使用
2、配置 mybatis 的一对多、多对一、多对多、一对一
创建三张表 student(学生表)、card(身份证表)、teacher(表)
通过 mybatis 实现学生的登录注册功能,实现 student 表与 card 表的一对一关
联查询,实现 student 表和 teacher 表的多对多关联查询
实验过程(请用简单的文字描述):
1. 通过 sql 语句建立 student(学生表)、card(身份证表)、teacher
(表)以及一张 st 表(连接学生和老师)
2. 在 idea 中建立表的类。
3. 写配置文件
4. 写三各类对应的 dao 接口
5. 写 Main 类模拟网页
实验详细操作步骤或程序清单:
1.建表
通过 sql 语句建立 student(学生表)、card(身份证表)、teacher
(表)以及一张 st 表(连接学生和老师)
2.在 idea 中建立三个类。
package com.bean;
import java.io.Serializable;
public class Card implements Serializable {
private Integer cid;
private String cname;
private Integer age;
private String sex;
public Card(String cname, Integer age, String sex) {
this.cname = cname;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Card{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
public Card() {
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package com.bean;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable {
private Integer sid;
private String sname;
private String password;
private Card card;
private List<Teacher> list;
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", password='" + password + '\'' +
", card=" + card +
", list=" + list +
'}';
}
public Student(String sname, String password, Card card,
List<Teacher> list) {
this.sname = sname;
this.password = password;
this.card = card;
this.list = list;
}
public Student() {
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public List<Teacher> getList() {
return list;
}
public void setList(List<Teacher> list) {
if (this.list==null){
this.list=list;
}else{
this.list.addAll(list);
}