Mybatis多对一关系映射处理三种方式:
数据库表:
t_class
t_student
实体类:
Student.java
public class Student {
private Integer sid;
private String name;
private Cla cla;
public Student() {
}
public Student(Integer sid, String name) {
this.sid = sid;
this.name = name;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cla getCla() {
return cla;
}
public void setCla(Cla cla) {
this.cla = cla;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", name='" + name + '\'' +
", cla=" + cla +
'}';
}
}
Cla.java
package com.example.mybatis;
public class Cla {
private Integer cid;
private String cname;
public Cla() {
}
public Cla(Integer cid, String cname) {
this.cid = cid;
this.cname = cname;
}
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;
}
@Override
public String toString() {
return "Cla{" +
"cid=" + cid +
", cname=" + cname +
'}';
}
}
一、级联方式处理
StudentoneMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
public interface StudentoneMapper {
Student queryfind(@Param("sid") Integer sid);
}
StudentoneMapper.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.example.mybatis.StudentoneMapper">
<resultMap id="qu" type="com.example.mybatis.Student">
<id property="sid" column="sid"></id>
<result property="name" column="name"></result>
<result property="cla.cid" column="cid"></result>
<result property="cla.cname" column="cname"></result>
</resultMap>
<select id="queryfind" resultMap="qu">
select student.*,cla.* from t_student student left join t_class cla on student.cid = cla.cid where student.sid = #{sid}
</select>
</mapper>
test.java
@Test
public void query() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
StudentoneMapper mapper = sqlSession.getMapper(StudentoneMapper.class);
Student queryfind = mapper.queryfind(1);
System.out.println(queryfind);
}
二、association处理
StudentoneMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
public interface StudentoneMapper {
Student queryfind(@Param("sid") Integer sid);
}
StudentoneMapper.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.example.mybatis.StudentoneMapper">
<resultMap id="qu" type="com.example.mybatis.Student">
<id property="sid" column="sid"></id>
<result property="name" column="name"></result>
<association property="cla" javaType="com.example.mybatis.Cla">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
</association>
</resultMap>
<select id="queryfind" resultMap="qu">
select student.*,cla.* from t_student student left join t_class cla on student.cid = cla.cid where student.sid = #{sid}
</select>
</mapper>
test.java
@Test
public void query() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
StudentoneMapper mapper = sqlSession.getMapper(StudentoneMapper.class);
Student queryfind = mapper.queryfind(1);
System.out.println(queryfind);
}
三、分步查询
StudentoneMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
public interface StudentoneMapper {
Student queryone(@Param("sid") Integer sid);
}
StudentoneMapper.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.example.mybatis.StudentoneMapper">
<resultMap id="student" type="com.example.mybatis.Student">
<id property="sid" column="sid"></id>
<result property="name" column="name"></result>
<association property="cla" select="com.example.mybatis.ClatwoMapper.querytwo"
column="cid"></association>
</resultMap>
<select id="queryone" resultMap="student">
select * from t_student where sid = #{sid}
</select>
</mapper>
ClatwoMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
public interface ClatwoMapper {
Cla querytwo(@Param("cid") Integer cid);
}
ClatwoMapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis.ClatwoMapper">
<select id="querytwo" resultType="com.example.mybatis.Cla">
select * from t_class where cid = #{cid}
</select>
</mapper>
test.java
@Test
public void mapquery() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
StudentoneMapper mapper = sqlSession.getMapper(StudentoneMapper.class);
Student queryone = mapper.queryone(1);
System.out.println(queryone.getName());
}