mybatis一对一 一对多关联关联

来源:互联网 发布:ubuntu更新软件命令 编辑:程序博客网 时间:2024/06/05 02:45

数据表结构:




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


MyBatis中使用collection标签来解决一对多的关联查询,collection标签可用的属性如下:
property:指的是集合属性的值
ofType:指的是集合中元素的类型
column:所对应的外键字段名称
select:使用另一个查询封装的结果

封装类

public class Clazz {private int cid;private String cname;private Teacher teacher;public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}}

public class Clazz {private int cid;private String cname;private Teacher teacher;private List<Student> students;public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}public List<Student> getStudents() {return students;}public void setStudents(List<Student> students) {this.students = students;}}


public class Teacher {private int tid;private String tname;public int getTid() {return tid;}public void setTid(int tid) {this.tid = tid;}public String getTname() {return tname;}public void setTname(String tname) {this.tname = tname;}}

GetSession类

public class GetSesson {public static SqlSession GetSesson(){String resource = "conf.xml"; //加载mybatis的配置文件(它也加载关联的映射文件)Reader reader = null;try {reader = Resources.getResourceAsReader(resource);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} //构建sqlSession的工厂SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);//创建能执行映射文件中sql的sqlSessionreturn sessionFactory.openSession();}}


conf.xml
<?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><!-- 为实体类定义别名,简化sql映射xml文件中的引用 --><typeAliases><typeAlias type="com.bean.Clazz" alias="_Clazz"/><typeAlias type="com.bean.Teacher" alias="_Teacher"/><typeAlias type="com.bean.Student" alias="_Student"/></typeAliases> <!-- 配置数据源相关的信息  -->  <environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/batis?characterEncoding=utf8" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><!-- 注册映射文件 --><mappers><mapper resource="com/dao/clazzMapper.xml"/></mappers></configuration>
clazzMapper.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="com.dao.clazzM"><!-- 一对一查询方法一 一个班级对应一个老师 --><select id="getClazz" parameterType="int" resultMap="OneToOne" >select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}</select><resultMap type="_Clazz" id="OneToOne"><id column="c_id" property="cid"/><result column="c_name" property="cname"/><!-- 一对一关联  --><association property="teacher"  column="teacher_id" javaType="_Teacher"><id column="t_id" property="tid"/><result column="t_name" property="tname"/></association></resultMap> <!-- 一对一查询方法2 方法二通过嵌套查询--><select id="getClazz1" resultMap="OneToOne1">select * from class where c_id=#{id}</select><select id="selectTeacher1" parameterType="int" resultType="_Teacher" >select t_id tid,t_name tname from teacher where t_id=#{id}</select><!-- type为java实体类;id为此resultMap的标识 --><resultMap type="_Clazz" id="OneToOne1"><id column="c_id" property="cid"/><result column="c_name" property="cname"/><!-- 一对一关系映射  property对应封装类中属性 --><association property="teacher" column="teacher_id" select="selectTeacher1" javaType="_Teacher" ></association></resultMap><!-- 一对多查询方法一  嵌套查询 一个班级对应多个学生SELECT * FROM class WHERE c_id=1;SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值SELECT * FROM student WHERE class_id=1  //1是第一个查询得到的c_id字段的值--><select id="getClazz2" parameterType="int" resultMap="OneToArr">select * from class where c_id=#{id}</select><select id="getStudent2" parameterType="int" resultType="_Student">select * from student where class_id=#{id}</select> <select id="selectTeacher2" parameterType="int" resultType="_Teacher">select t_id tid,t_name tname from teacher where t_id=#{id}</select> <resultMap type="_Clazz" id="OneToArr"><id column="c_id" property="cid"/><id column="c_name"  property="cname"/><association property="teacher" column="teacher_id" select="selectTeacher2" javaType="_Teacher" ></association> <!-- collection不能添加属性:javaType="属性类型",否则报错returned more than one row, where no more than one was expected(返回多于一行,不超过一个行)--><collection property="students" column="c_id" select="getStudent2" ofType="_Student"></collection></resultMap><!-- 一对多查询方法二 --><!--<select id="getClazz3"  parameterType="int" resultMap="OneToArr3">select * from class c,student s where c.c_id=s.class_id and c.c_id=#{id}</select><resultMap type="_Clazz" id="OneToArr3"><id column="c_id" property="cid"/><id column="c_name"  property="cname"/>ofType指定struts集合中的对象类型<collection property="students" ofType="_Student"  ><id column="s_id" property="s_id"/><result column="s_name" property="s_name"/></collection></resultMap>  --><select id="getClazz4"  parameterType="int" resultMap="OneToArr4">select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and  c.c_id=#{id}</select><resultMap type="_Clazz" id="OneToArr4"><id column="c_id" property="cid"/><id column="c_name"  property="cname"/><!-- ofType指定struts集合中的对象类型 --><association property="teacher"  column="teacher_id" javaType="_Teacher"><id column="t_id" property="tid"/><result column="t_name" property="tname"/></association><!-- ofType指定集合中的对象类型 --><!-- collection不能添加属性:javaType="属性类型",否则报错“argument type mismatch”(参数类型不匹配)--><collection property="students" column="c_id" ofType="_Student" ><id column="s_id" property="s_id"/><result column="s_name" property="s_name"/></collection></resultMap></mapper>
测试

public class clazzDao {public static void  OneToOne(){//String statement="com.dao.clazzM"+".getClazz1";String statement="com.dao.clazzM"+".getClazz";Clazz clazz=GetSesson.GetSesson().selectOne(statement,1);System.out.println("班级:"+clazz.getCname()+" 老师:"+clazz.getTeacher().getTname());}public static void OneToArr(){ //String statement="com.dao.clazzM"+".getClazz2";String statement="com.dao.clazzM"+".getClazz2";Clazz clazz=GetSesson.GetSesson().selectOne(statement,1);System.out.println("班级:"+clazz.getCname());Teacher teacher=clazz.getTeacher();System.out.println(teacher.getTname());List<Student> list=clazz.getStudents();for(Student student:list){System.out.println(student.getS_name());}}public static void main(String[] args) {clazzDao.OneToArr();}}




原创粉丝点击