【MyBatis】-----【MyBatis】---表级联系【一对一】--增删改查

【MyBatis】-----【MyBatis】---表级联系【一对一】--增删改查

一、核心配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 最开始加载引用db.properties文件 -->
<properties resource="db.properties"/>
<!-- 取别名 以下情况别名为类名 -->
<typeAliases>
<package name="com.myBatis.entity"/>
</typeAliases>
<!--
development : 开发模式
work : 工作模式
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 引用映射文件 -->
<mappers >
<mapper resource="com/myBatis/mapper/classesMapper.xml"/>
<mapper resource="com/myBatis/mapper/teacherMapper.xml"/>
</mappers>
</configuration>

二、实体类

  2.1Teacher

 package com.myBatis.entity;

 public class Teacher {
// 定义实体类的属性,与teacher表中的字段对应
private int id; // id===>t_id
private String name; // name===>t_name
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
public Teacher() {
super();
}
public Teacher(int id) {
super();
this.id = id;
} }

  2.2Classes

package com.myBatis.entity;

public class Classes {
private int id; // id===>c_id
private String name; // name===>c_name
/**
* class表中有一个teacher_id字段,所以在Classes类中定义一个teacher属性,
* 用于维护teacher和class之间的一对一关系,通过这个teacher属性就可以知道这个班级是由哪个老师负责的
*/
private Teacher teacher;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
}
public Classes(String name, Teacher teacher) {
super();
this.name = name;
this.teacher = teacher;
}
public Classes() {
super();
} }

三、映射文件

teacherMapper.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,
这样就能够保证namespace的值是唯一的
-->
<mapper namespace="com.myBatis.mapper.teacherMapper">
<!-- 根据id查询得到一个user对象 -->
<select id="getteacher" parameterType="int" resultMap="TeacherResultMap">
select * from classes c, teacher t where c.t_id=t.t_id and t.t_id=#{id}
</select>
<!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
<resultMap type="teacher" id="TeacherResultMap">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</resultMap> <insert id="addTeacher" parameterType="Teacher">
insert into teacher values(#{t_name})
</insert> </mapper>

classesMapper.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,
这样就能够保证namespace的值是唯一的
-->
<mapper namespace="com.myBatis.mapper.classesMapper">
<!-- 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
封装联表查询的数据(去除重复的数据)
根据id查询得到一个user对象 -->
<select id="getclassess" parameterType="int" resultMap="ClassResultMap">
select * from classes c, teacher t where c.t_id=t.t_id and c.c_id=#{id}
</select>
<!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
<resultMap type="Classes" id="ClassResultMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!-- 根据id查询删除一个class 的t_id -->
<delete id="deleteClasses" parameterType="Classes">
delete from classes where c_id=#{id}
</delete>
<!-- 添加(Create) -->
<insert id="addClasses" parameterType="Classes">
insert into classes values(null,#{name},#{teacher.id})
</insert>
<!-- 修改班级所对应的老师(Update) -->
<update id="updateClasses" parameterType="Classes">
update classes set t_id=#{teacher.id} where c_id=#{id}
</update> </mapper>

四、测试类

 package myBatisTest;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test; import com.myBatis.entity.Classes;
import com.myBatis.entity.Teacher;
import com.myBatis.util.myBtaisUtil; public class TestUser {
//修改
@Test
public void testUpdateClasses() {
SqlSession sqlSession=myBtaisUtil.getsqlSession();
//先查找
String selectclasses = "com.myBatis.mapper.classesMapper.getclassess";
Classes classes =sqlSession.selectOne(selectclasses, 3);
System.out.println(classes);
//再修改
Teacher teacher = new Teacher(4);
System.out.println();
classes.setTeacher(teacher);
String update = "com.myBatis.mapper.classesMapper.updateClasses";
int i = sqlSession.update(update, classes);
System.out.println(i);
Classes classes2= sqlSession.selectOne(selectclasses, 3);
System.out.println(classes2);
} //添加
@Test
public void testAddClasses() {
SqlSession sqlSession=myBtaisUtil.getsqlSession();
String add = "com.myBatis.mapper.classesMapper.addClasses";
Classes classes = new Classes();
Teacher teacher= new Teacher(4);
classes.setName("1111");
classes.setTeacher(teacher);
int i = sqlSession.insert(add, classes);
System.out.println(i);
} //删除
@Test
public void testDeleteClasses() {
SqlSession sqlSession=myBtaisUtil.getsqlSession();
String delete = "com.myBatis.mapper.classesMapper.deleteClasses";
int i= sqlSession.delete(delete, 5);
System.out.println(i);
} //查询
@Test
public void testGetClasses() {
SqlSession sqlSession=myBtaisUtil.getsqlSession();
String select = "com.myBatis.mapper.classesMapper.getclassess";
Classes classes = sqlSession.selectOne(select, 7);
System.out.println(classes);
}
}

MyBatis一对一关联查询总结

  MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:

  • property:对象属性的名称
  • javaType:对象属性的类型
  • column:所对应的外键字段名称
  • select:使用另一个查询封装的结果

参考:MyBatis学习总结(五)——实现关联表查询

上一篇:shell命令工作总结


下一篇:项目一:项目第二天 Jquery ztree使用展示菜单数据 2、 基础设置需求分析 3、 搭建项目框架环境--ssh(复习) 4、 SpringData-JPA持久层入门案例(重点) 5、 Easyui menubutton菜单按钮使用 6、 Easyui messager消息框使用