1、person.java
package cn.itcast.domain;
import java.util.List;
public class Person {
private Integer id;
private String name;
private Integer age;
private String remark;
private PersonInfo personInfo;//对一关联
private List<Book> books;//对多关联
。。。
}
2、Book.java
package cn.itcast.domain;
public class Book {
private Integer id;
private String name;
private Double money;
...
}
3、PersonInfo.java
package cn.itcast.domain;
import java.util.Date;
public class PersonInfo {
private Integer id;
private String station;
private Date joinDate;
...
}
}
4、personInfoMapper.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="cn.itcast.mapper.PersonInfoMapper">
<!-- 中间,对象的属性和结果集的字段之间的对应关系 -->
<resultMap type="cn.itcast.domain.Person" id="personInfoRM">
<!-- 主键映射 -->
<id property="id" column="ID"/>
<!-- 普通字段 property指实体的属性;column结果集的字段名称 -->
<result property="name" column="USERNAME"/>
<result property="age" column="AGE"/>
<result property="remark" column="REMARK"/>
</resultMap>
<!-- 对象关联 -->
<!-- (1)使用继承方式,配置对一关联关系 -->
<resultMap type="cn.itcast.domain.Person" id="personPersonInfoRM" extends="personInfoRM">
<!-- javaType配置对一关联关系,对应association -->
<association property="personInfo" javaType="cn.itcast.domain.PersonInfo">
<id property="id" column="INFOID"/>
<result property="station" column="STATION"/>
<result property="joinDate" column="JOIN_DATE"/>
</association>
</resultMap>
<!-- (2)使用继承方式,配置对多关联关系 -->
<resultMap type="cn.itcast.domain.Person" id="personBookRM" extends="personPersonInfoRM">
<!-- ofType配置配置对多关联,对应collection -->
<collection property="books" ofType="cn.itcast.domain.Book">
<id property="id" column="BOOK_ID"/>
<result property="name" column="BOOK_NAME"/>
<result property="money" column="MONEY"/>
</collection>
</resultMap>
<!-- (2)查询 莫个人的信息包含扩展信息以及书籍信息-->
<select id="findPesonInfoBook" parameterType="map" resultMap="personBookRM">
SELECT p.*,book.id AS book_id,book.money,book.name AS book_name
FROM
(
SELECT
person.*,person_info.id AS infoid,person_info.join_date,person_info.station
FROM
person LEFT JOIN person_info ON person.id=person_info.id
)p
LEFT JOIN book ON p.id=book.person_id
<where>
<if test="name!=null">username = #{name}</if>
</where>
</select>
</mapper>
5、测试:
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import cn.itcast.domain.Person;
public class PersonInfoTest {
private SqlSessionFactory sqlSessionFactory;
//获取sqlSession
@Before
public void get() throws IOException{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 根据Person与PersonInfo之间的对一映射以及person与book之间的对多关系查询person信息
*/
@Test
public void testPersonInfoBook(){
SqlSession session = sqlSessionFactory.openSession();
Map map = new HashMap();
List<Person> persons = session.selectList("cn.itcast.mapper.PersonInfoMapper.findPesonInfoBook", map);
for (Person person : persons) {
System.out.println(person.toString());
}
}
}
转载于:https://blog.51cto.com/12295883/1893342