接下来总结一下mybatis的一对多的关联。
其实上一篇博文中,一对一的关联实际上就是一对多的形式,只是现在提笔开始写的时候才发现,原本一堆多的许多点都提到了前面的博文中,这里几乎没有什么可以补充的了,暂且就先将配置和测试发上来吧,有兴趣的朋友可以copy下去测试。
依旧采用地区(District)---街道(Street) 这两个表来做关联,一个地区下有多个街道,街道表中存有地区的id.
实体类:District
package com.entity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class District {
private Integer id;
private String name;
private String manager;
private String square;
private Integer population;
private List<Street> streets = new ArrayList<Street>();
public District() {
super();
}
public District(String name, String manager, String square,
Integer population) {
super();
this.name = name;
this.manager = manager;
this.square = square;
this.population = population;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public String getSquare() {
return square;
}
public void setSquare(String square) {
this.square = square;
}
public Integer getPopulation() {
return population;
}
public void setPopulation(Integer population) {
this.population = population;
}
public List<Street> getStreets() {
return streets;
}
public void setStreets(List<Street> streets) {
this.streets = streets;
}
}
Street:
package com.entity;
public class Street {
private Integer id;
private District district;
private String name;
private String length;
private String manager;
private String creater;
public Street(String name, String length, String manager, String creater) {
super();
this.name = name;
this.length = length;
this.manager = manager;
this.creater = creater;
}
public Street() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public District getDistrict() {
return district;
}
public void setDistrict(District district) {
this.district = district;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public String getCreater() {
return creater;
}
public void setCreater(String creater) {
this.creater = creater;
}
}
District.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.entity.District">
<resultMap type="com.entity.District" id="SimpleDistrict">
<id property="id" column="ID" javaType="Integer" jdbcType="INTEGER" />
<result property="name" column="NAME" javaType="String"
jdbcType="VARCHAR" />
<result property="manager" column="MANAGER" javaType="String"
jdbcType="VARCHAR" />
<result property="square" column="SQUARE" javaType="String"
jdbcType="VARCHAR" />
<!--ofType指collection包含的元素的类型,此属性不可少。
column属性指把上述的getById的select语句中的教师id列的值作为参数
传递给将要引用到的下述的selectStreetsByDistrictId的select语句,此属性不可少。
引用的形式为:命名空间.select语句id-->
</resultMap>
<resultMap type="com.entity.District" id="DistrictResultMap" extends="SimpleDistrict">
<collection property="streets" column="ID" javaType="ArrayList"
ofType="com.entity.Street" jdbcType="INTEGER" select="com.entity.Street.selectStreetsByDistrictId"/>
</resultMap>
<!-- 使用resultMap镶嵌式的时候,select中的sql语句要写成左外链接型 -->
<resultMap type="com.entity.District" id="DistrictResultMap2" extends="SimpleDistrict">
<collection property="streets" column="ID" javaType="ArrayList"
ofType="com.entity.Street" jdbcType="INTEGER" resultMap="com.entity.Street.SimpleStreet" />
</resultMap>
<!-- 一对多的时候,一的一条sql语句执行先执行,然后执行resultMap中的collection的select对应的多的一方的sql语句,
如果多的一方的sql语句的返回类型包含一的一方,比如这里,selectStreetsByDistrictId的返回类型resultMap如果是StreetResultMap
则从district得到的street的list集合中的street的district值仍然可以读到,但是并没有多余的sql语句执行,
其实可以无限的获取下去,但是并没有什么意义;
如果返回值类型是SimpleStreet,则list集合中的street中的district将为空-->
<select id="getDistrictById" resultMap="DistrictResultMap" parameterType="int">
select * from District where id=#{id}
</select>
<select id="getDistrictById2" resultMap="DistrictResultMap2" parameterType="int">
select d.*,s.id sid,s.name sname from District d,Street s where d.id=s.district_id and d.id=#{id}
</select>
<!--这是resultMap的另一种写法,在中间写一个构造器-->
<!-- <resultMap type="StudentEntity" id="studentResultMap">
<constructor>
<idArg javaType="String" column="STUDENT_ID" />
<arg javaType="String" column="STUDENT_NAME" />
<arg javaType="String" column="STUDENT_SEX" />
<arg javaType="Date" column="STUDENT_BIRTHDAY" />
</constructor>
</resultMap> -->
</mapper>
上面xml最后写了另外一种resultMap的写法,是我在一篇文章中偶尔看到的,但是并没有用过,感觉上不如之前的写法灵活。
Street.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.entity.Street">
<resultMap type="com.entity.Street" id="SimpleStreet">
<id property="id" column="sID" javaType="Integer" jdbcType="INTEGER" />
<result property="name" column="sNAME" javaType="String"
jdbcType="VARCHAR" />
<result property="length" column="length" javaType="String"
jdbcType="VARCHAR" />
<result property="manager" column="manager" javaType="String"
jdbcType="VARCHAR" />
<result property="creater" column="creater" javaType="String"
jdbcType="VARCHAR" />
</resultMap>
<resultMap type="com.entity.Street" id="StreetResultMap" extends="SimpleStreet">
<association property="district" column="DISTRICT_ID"
select="com.entity.District.getDistrictById" />
</resultMap>
<select id="selectStreetsByDistrictId" resultMap="SimpleStreet"
parameterType="int">
select * from Street where district_id =#{id}
</select>
<select id="selectStreetById" resultMap="StreetResultMap"
parameterType="int">
select * from Street where id =#{id}
</select>
</mapper>
测试类:
package com.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.entity.District;
import com.entity.Street;
import com.util.SqlSessionFactoryUtil;
public class TestOne2Many {
public static void main(String[] args) {
TestOne2Many test = new TestOne2Many();
test.test1();
}
public void testOne2Many(){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
District district = session.selectOne("getDistrictById",4);
System.out.println(district.getName());
List<Street> streets = district.getStreets();
for(int i =0;i<streets.size();i++){
Street street = streets.get(i);
System.out.println(street.getName()+"--------"+street.getDistrict().getName());
}
session.close();
}
public void testOne2Many2(){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
District district = session.selectOne("getDistrictById2",4);
System.out.println(district.getName());
List<Street> streets = district.getStreets();
for(int i =0;i<streets.size();i++){
Street street = streets.get(i);
System.out.println(street.getName()+"--------"+street.getDistrict().getName());
}
session.close();
}
public void test(){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
Street street = session.selectOne("selectStreetById",4);
System.out.println(street.getName()+"====="+street.getDistrict().getName());
/*List<Street> streets = street.getDistrict().getStreets();
for(int i =0;i<streets.size();i++){
Street street1 = streets.get(i);
System.out.println(street1.getName());
}*/
session.close();
}
public void test1(){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
List<Street> streets = session.selectList("selectStreetsByDistrictId",4);
System.out.println(streets.size());
session.close();
}
}
其他的一些注释,在前文中都已经提到过,就不再说明了,有问题的朋友可以留言。