1. 一对一
stu拥有一个stu_card
1.1 student实体类
将外键代表的实体放在属性
public class Student {
private Integer sid;
private String sname;
private StuCard stuCard;
private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
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 StuCard getStuCard() {
return stuCard;
}
public void setStuCard(StuCard stuCard) {
this.stuCard = stuCard;
}
@Override
public String toString() {
return sid + " " + sname + " " + phone + " " + stuCard;
}
}
1.2 StudentMapper.xml
<resultMap id="StudentMap" type="entity.Student">
<id column="sid" property="sid"></id>
<result column="sname" property="sname"></result>
<!-- 一对一关联 外键-->
<association property="stuCard" javaType="entity.StuCard">
<id column="cid" property="cid"></id>
<result column="balance" property="balance"></result>
</association>
</resultMap>
<select id="getStudentBySid" parameterType="Integer" resultMap="StudentMap">
select *
from stu s left join stu_card sc on s.cid = sc.cid
where s.sid = #{sid}
</select>
2.一对多
stu_class有多个stu
2.1 stuClass实体类
public class StuClass {
private Integer cid;
private String cname;
private List<Student> students;
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 List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return cid + " " + cname + " " + students;
}
}
2.2 StuClassMapper.xml
<resultMap id="StuClassMap" type="entity.StuClass">
<!-- 主键-->
<id column="cid" property="cid"></id>
<!-- 列的映射关系-->
<result column="cname" property="cname"></result>
<!-- ofType指定的这个List所存放的javaBean的类型-->
<!-- javaType指定的当前这个配置的标签所对应的属性,比如我们这里的collection配置的是一个List,就可以配置成javaType=“java.util.ArrayList”-->
<collection property="students" ofType="entity.Student">
<id column="sid" property="sid"></id>
<!-- 列的映射关系-->
<result column="sname" property="sname"></result>
</collection>
</resultMap>
<select id="getStuClassByClassId" parameterType="Integer" resultMap="StuClassMap">
select *
from stu_class sc left join stu s on s.class_id = sc.cid
where sc.cid = #{cid}
</select>
3.多对多
通过俩个实体的主键中间表转化为一对多
teacher和stu_class 建立class_teacher表
3.1 teacher实体类
public class Teacher {
private Integer tid;
private String tname;
private List<StuClass> classes;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public List<StuClass> getClasses() {
return classes;
}
public void setClasses(List<StuClass> classes) {
this.classes = classes;
}
@Override
public String toString() {
return tid + " " + tname + classes;
}
}
3.2 TeacherMapper.xml
<resultMap id="TeacherMap" type="entity.Teacher">
<!-- 主键-->
<id column="tid" property="tid"></id>
<!-- 列的映射关系-->
<result column="tname" property="tname"></result>
<!-- ofType指定的这个List所存放的javaBean的类型-->
<!-- javaType指定的当前这个配置的标签所对应的属性,比如我们这里的collection配置的是一个List,就可以配置成javaType=“java.util.ArrayList”-->
<collection property="classes" ofType="entity.StuClass">
<id column="cid" property="cid"></id>
<!-- 列的映射关系-->
<result column="cname" property="cname"></result>
</collection>
</resultMap>
<select id="getTeacherByTid" parameterType="Integer" resultMap="TeacherMap">
select *
from class_teacher ct left join teacher t on ct.tid = t.tid
left join stu_class sc on ct.cid = sc.cid
where t.tid = #{tid}
</select>
4.主键映射
4.1 StuClassMapper.xml
俩种写法
<insert id="insertStuClass" parameterType="entity.StuClass" useGeneratedKeys="true" keyProperty="cid">
insert into stu_class(cid, cname)
values (#{cid}, #{cname})
</insert>
<!-- 不支持主键递增时-->
<insert id="insertStuClass1" parameterType="entity.StuClass">
insert into stu_class(cid, cname)
values (#{cid}, #{cname})
<selectKey keyColumn="cid" keyProperty="cid" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
</insert>
4.2 StuClassService
public int insertStuClass(){
String resource = "config.xml";
StuClass stuClass = new StuClass();
stuClass.setCname("20-13");
int result = -1;
try{
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = ssfb.build(in);
SqlSession sqlSession=ssf.openSession();
String statement="mapper.StuClassMapper.insertStuClass1";
//插入的数据数量
result = sqlSession.insert(statement,stuClass);
System.out.println(result);
//获取自增的主键
System.out.println(stuClass.getCid());
sqlSession.commit();
sqlSession.close();
}catch (Exception e){
e.printStackTrace();
}
return result;
}
标签:return,String,映射,cid,class,关联,stu,Mybatis,public
From: https://www.cnblogs.com/lwx11111/p/17191637.html