首页 > 代码库 > Mybatis一对多,多对一中主子表字段名一致产生的问题及解决方案
Mybatis一对多,多对一中主子表字段名一致产生的问题及解决方案
数据库两张表的字段名都和两个类的属性名称一样,这时在关联查询出现一个问题。
学生类:
package com.pojo; public class Student { private int id; private String name; private int age; private ClassInfo classInfo; public Student() { } public Student(String name, int age) { super(); this.name = name; this.age = age; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setClassInfo(ClassInfo classInfo) { this.classInfo = classInfo; } public ClassInfo getClassInfo() { return classInfo; } }
package com.pojo; import java.util.Set; public class ClassInfo { private int id; private String name; private Set<Student> stus; public ClassInfo() { } public ClassInfo(String name) { super(); this.name = 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; } public void setStus(Set<Student> stus) { this.stus = stus; } public Set<Student> getStus() { return stus; } }
<?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"> <!-- 继承式多对一 --> <resultMap type="Student" id="student"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> </resultMap> <resultMap type="Student" id="StudentResultMap" extends="student"> <association property="classInfo" column="classInfo_id" javaType="ClassInfo"> <id property="id" column="id" /> <result property="name" column="name" /> </association> </resultMap> <select id="manyToOne" resultMap="StudentResultMap"> select s.*,c.* from student s left join classInfo c on s.classInfo_id = c.id </select> </mapper>
@Test public void manyToOne(){ SqlSession session = factory.openSession(); List<Student> stuList = session.selectList("com.dao.manyToOne"); for (Student stu : stuList) { System.out.println(stu.getId()+":"+stu.getName()+","+stu.getAge()); System.out.println(stu.getClassInfo().getId()+":"+stu.getClassInfo().getName()); } session.close(); }
输出结果:
2013-11-18 03:52:30,500 [main] DEBUG [com.dao.manyToOne] - ooo Using Connection [com.MySQL.jdbc.Connection@141fab6]
2013-11-18 03:52:30,500 [main] DEBUG [com.dao.manyToOne] - ==> Preparing: select s.*,c.* from student s left join classInfo c on s.classInfo_id = c.cid
2013-11-18 03:52:30,593 [main] DEBUG [com.dao.manyToOne] - ==> Parameters:
2013-11-18 03:52:30,656 [main] DEBUG [com.dao.manyToOne] - <== Total: 1
5:Wang,20
5:Wang
这里输出班级的编号和名称也编程了学生的编号和名称,由于学生表(Student)和班级表(ClassInfo)都有id和name字段,导致查询结果也一样,
如果是一对多映射查询出来都是班级的编号和名称。由于字段名相同,取到s.*(id和name)后面的c.*就不回取了,所以学生取完id和name没错,
班级也会取学生的字段,所以结果都是一样的。如果反过来换成Preparing: select c.*,s.*from student s left join classInfo c on s.classInfo_id = c.cid ,
结果会都为班级的信息。
解决方法:
查询给列取别名
select c.*,s.id sid,s.name sname,s.age from student s left join classInfo c on s.classInfo_id = c.id
映射的column也要对应查询时的别名:
<resultMap type="Student" id="student"> <id property="id" column="sid" /> <result property="name" column="sname" /> <result property="age" column="age" /> </resultMap>
此外还有一种方法就是将主子表的id值名称设置为不一样,虽然这种方式可以解决此问题,但是解决了问题却不知问题之所以然。所以建议还是使用以上方法。
本文参考转载自 http://blog.csdn.net/lee4037/article/details/16798941 感谢作者分享
Mybatis一对多,多对一中主子表字段名一致产生的问题及解决方案