MyBatis一对多,多对一(图文并茂)

代码下载

链接:https://pan.baidu.com/s/1FX4TmyKqZQx39ncn8TItfw 密码:1h1i

一对多(一个班级Class有多个学生Student)

前提:

1)所谓的一对多就是一方可以看见多方,至于多方是否能看见一方我们不管

2)对基本语法标签掌握,这里只对配置地方重点描述,别的地方不解释

3)把 数据库设计和类设计(在一方有一个多的集合)看懂了,很重要,很重要,很重要

数据库

数据库设计:一个班级有多个学生,在学生里有一个班级的外键classid

对应的实体类

Class实体类

package com.imooc.onetomany;

import java.util.ArrayList;
import java.util.List;

public class Class {

	private Integer cid;

	private String cname;

	// 多方集合,即学生集合
	private List<Student> students = new ArrayList<>();

   //getter ,setter ,构造方法

}

Student实体类

package com.imooc.onetomany;

public class Student {

	private Integer sid;

	private String sname;

   //getter,setter,构造方法

}

xxxMapper.java(dao层)

StudentMapper与之对应的xml

package com.imooc.onetomany;

public interface StudentMapper {

	//根据ID查询学生
	public Student selectStudent(Integer id);

	//根据classID查询学生
	public Student selectStudentByClassId(Integer classID);

}
<?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.imooc.onetomany.StudentMapper">


	<resultMap id="BaseResultMap" type="com.imooc.onetomany.Student">
		<id column="sid" property="sid" jdbcType="INTEGER" />
		<result column="sname" property="sname" jdbcType="VARCHAR" />
	</resultMap>


	<select id="selectStudent" resultMap="BaseResultMap">
		select sid,sname from student where sid = #{id}
	</select>

	<select id="selectStudentByClassId" resultMap="BaseResultMap">
		select sid,sname from student where classid = #{id}
	</select>



</mapper>

ClassMapper与之对应的XML(敲黑板了,重点XML配置来了)

package com.imooc.onetomany;

public interface ClassMapper {
	
	public Class selectClass(Integer id);

}


(重点重点重点在下面)

<?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.imooc.onetomany.ClassMapper">

	<resultMap id="BaseResultMap" type="com.imooc.onetomany.Class">
		<id column="cid" property="cid" jdbcType="INTEGER" />
		<result column="cname" property="cname" jdbcType="VARCHAR" />
		<collection property="students" ofType="com.imooc.onetomany.Student"
			column="cid" select="com.imooc.onetomany.StudentMapper.selectStudentByClassId"></collection>
	</resultMap>



	<select id="selectClass" resultMap="BaseResultMap">
		select cid,cname from class
		where cid = #{id}
	</select>




</mapper>

1 ) 在reslutMap中用collection标签

2 ) property属性对应一方(Class)里面的属性List<Student>  students

3 )ofType对应 对应多方的数据类型,注意写全类名

4) select 属性是调用哪个接口里的哪个方法,此文章中是调用com.imooc.onetomany包下的StudentMapper类的selectStudentByClassId方法

5)column 属性是向 select属性的接口传递的参数

多对一(多个城市City对应一个国家Country)

前提:

1)所谓的多对一就是多的一方可以看见一的一方,一的一方能不能看见多方我们不管

2)对基本语法标签掌握,这里只对配置地方重点描述,别的地方不解释

3)把 数据库设计和类设计(在多的一方有一个一的属性)看懂了,很重要,很重要,很重要

数据库设计

数据库解释:在多的一方有一个一的外键

 实体类

City多方

package com.imooc.manytoone;

public class City {

	private Integer cid;

	private String cname;

	//添加一个一方的属性
	private Country country;

	//getter setter 构造方法

}

Country一方

package com.imooc.manytoone;

public class Country {

	private Integer id;
	private String name;
    //getter setter 构造方法
	
}

xxxMapper.java(dao层)

CityMapper与之对应的xml(敲黑板,重点来了)

package com.imooc.manytoone;

public interface CityMapper {
	
	public City selectCity(Integer id);

}
<?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.imooc.manytoone.CityMapper">

	<resultMap id="BaseResultMap" type="com.imooc.manytoone.City">
		<id column="cid" property="cid" jdbcType="INTEGER" />
		<result column="cname" property="cname" jdbcType="VARCHAR" />
		<association property="country" javaType="com.imooc.manytoone.Country"
			select="com.imooc.manytoone.CountryMapper.selectCountry" column="countryid"></association>
	</resultMap>


	<select id="selectCity" resultMap="BaseResultMap">
		select cid,cname,countryid
		from city
		where cid = #{id}
	</select>




</mapper>

1) resultMap中用association标签

2)property属性是多方里的一方的属性  ,即City类里的country属性

3)javaType是你一方的类型

4)column是传给select属性里面的方法的参数

5)select属性是调用哪个方法

CountryMapper与之对应的xml

package com.imooc.manytoone;

public interface CountryMapper {
	
	public Country selectCountry(Integer id);

}
<?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.imooc.manytoone.CountryMapper">

	<resultMap id="BaseResultMap" type="com.imooc.manytoone.Country">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="name" property="name" jdbcType="VARCHAR" />
	</resultMap>


	<select id="selectCountry" resultMap="BaseResultMap">
		select id,name from country
		where id = #{id}
	</select>

</mapper>

一对一

类似多对一,多的一方剩下一个就是一对一

多对多

这个地方写的不好,建议读

数据库:3张表,一张关系表,都懂哈

我也百度了一些博客,总的思路就是:多个一对多就是多对多

这里没有什么标签,就是两个步骤:

1 修改xxxMapper.xml中的resultMap

2 在select标签中写SQL语句(多表查询的语句,没巧方法),  select xxx  from table1,table2,table3 where 巴拉巴拉,注意与resultMap中对应

猜你喜欢

转载自blog.csdn.net/qq_37171353/article/details/85761800