一、核心配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 最开始加载引用db.properties文件 --> 6 <properties resource="db.properties"/> 7 <!-- 取别名 以下情况别名为类名 --> 8 <typeAliases> 9 <package name="com.myBatis.entity"/> 10 </typeAliases> 11 <!-- 12 development : 开发模式 13 work : 工作模式 14 --> 15 <environments default="development"> 16 <environment id="development"> 17 <transactionManager type="JDBC" /> 18 <!-- 配置数据库连接信息 --> 19 <dataSource type="POOLED"> 20 <property name="driver" value="${driver}" /> 21 <property name="url" value="${url}" /> 22 <property name="username" value="${username}" /> 23 <property name="password" value="${password}" /> 24 </dataSource> 25 </environment> 26 </environments> 27 <!-- 引用映射文件 --> 28 <mappers > 29 <mapper resource="com/myBatis/mapper/classesMapper.xml"/> 30 <mapper resource="com/myBatis/mapper/teacherMapper.xml"/> 31 </mappers> 32 </configuration>
二、实体类
2.1Teacher
1 package com.myBatis.entity; 2 3 public class Teacher { 4 // 定义实体类的属性,与teacher表中的字段对应 5 private int id; // id===>t_id 6 private String name; // name===>t_name 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 @Override 20 public String toString() { 21 return "Teacher [id=" + id + ", name=" + name + "]"; 22 } 23 public Teacher() { 24 super(); 25 } 26 public Teacher(int id) { 27 super(); 28 this.id = id; 29 } 30 31 32 }
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
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 5 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名, 6 这样就能够保证namespace的值是唯一的 7 --> 8 <mapper namespace="com.myBatis.mapper.teacherMapper"> 9 <!-- 根据id查询得到一个user对象 --> 10 <select id="getteacher" parameterType="int" resultMap="TeacherResultMap"> 11 select * from classes c, teacher t where c.t_id=t.t_id and t.t_id=#{id} 12 </select> 13 <!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> 14 <resultMap type="teacher" id="TeacherResultMap"> 15 <id property="id" column="t_id"/> 16 <result property="name" column="t_name"/> 17 </resultMap> 18 19 <insert id="addTeacher" parameterType="Teacher"> 20 insert into teacher values(#{t_name}) 21 </insert> 22 23 </mapper> 24
classesMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 5 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名, 6 这样就能够保证namespace的值是唯一的 7 --> 8 <mapper namespace="com.myBatis.mapper.classesMapper"> 9 <!-- 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 10 封装联表查询的数据(去除重复的数据) 11 根据id查询得到一个user对象 --> 12 <select id="getclassess" parameterType="int" resultMap="ClassResultMap"> 13 select * from classes c, teacher t where c.t_id=t.t_id and c.c_id=#{id} 14 </select> 15 <!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> 16 <resultMap type="Classes" id="ClassResultMap"> 17 <id property="id" column="c_id"/> 18 <result property="name" column="c_name"/> 19 <association property="teacher" javaType="Teacher"> 20 <id property="id" column="t_id"/> 21 <result property="name" column="t_name"/> 22 </association> 23 </resultMap> 24 <!-- 根据id查询删除一个class 的t_id --> 25 <delete id="deleteClasses" parameterType="Classes"> 26 delete from classes where c_id=#{id} 27 </delete> 28 <!-- 添加(Create) --> 29 <insert id="addClasses" parameterType="Classes"> 30 insert into classes values(null,#{name},#{teacher.id}) 31 </insert> 32 <!-- 修改班级所对应的老师(Update) --> 33 <update id="updateClasses" parameterType="Classes"> 34 update classes set t_id=#{teacher.id} where c_id=#{id} 35 </update> 36 37 </mapper> 38
四、测试类
1 package myBatisTest; 2 import org.apache.ibatis.session.SqlSession; 3 import org.junit.Test; 4 5 import com.myBatis.entity.Classes; 6 import com.myBatis.entity.Teacher; 7 import com.myBatis.util.myBtaisUtil; 8 9 public class TestUser { 10 //修改 11 @Test 12 public void testUpdateClasses() { 13 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 14 //先查找 15 String selectclasses = "com.myBatis.mapper.classesMapper.getclassess"; 16 Classes classes =sqlSession.selectOne(selectclasses, 3); 17 System.out.println(classes); 18 //再修改 19 Teacher teacher = new Teacher(4); 20 System.out.println(); 21 classes.setTeacher(teacher); 22 String update = "com.myBatis.mapper.classesMapper.updateClasses"; 23 int i = sqlSession.update(update, classes); 24 System.out.println(i); 25 Classes classes2= sqlSession.selectOne(selectclasses, 3); 26 System.out.println(classes2); 27 } 28 29 //添加 30 @Test 31 public void testAddClasses() { 32 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 33 String add = "com.myBatis.mapper.classesMapper.addClasses"; 34 Classes classes = new Classes(); 35 Teacher teacher= new Teacher(4); 36 classes.setName("1111"); 37 classes.setTeacher(teacher); 38 int i = sqlSession.insert(add, classes); 39 System.out.println(i); 40 } 41 42 //删除 43 @Test 44 public void testDeleteClasses() { 45 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 46 String delete = "com.myBatis.mapper.classesMapper.deleteClasses"; 47 int i= sqlSession.delete(delete, 5); 48 System.out.println(i); 49 } 50 51 //查询 52 @Test 53 public void testGetClasses() { 54 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 55 String select = "com.myBatis.mapper.classesMapper.getclassess"; 56 Classes classes = sqlSession.selectOne(select, 7); 57 System.out.println(classes); 58 } 59 }
MyBatis一对一关联查询总结
MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:
- property:对象属性的名称
- javaType:对象属性的类型
- column:所对应的外键字段名称
- select:使用另一个查询封装的结果