mybatis一对一,一对多

一对一实例:

主要实现代码:

/**
	 * 查询所有订单信息
	 * @return
	 */
	public List<Order> selectAll2();
<?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">
	
<!-- namespace 命名  #{}:点位符,相当于jdbc的?-->
<mapper namespace="cn.xxs.mapper.OrderMapper">


	<!-- id: sql中语句的唯一标识
	    parameterType :入参的数据类型
		resultType:返回结果的数据类型
	 -->	
	<resultMap type="order" id="order_map">
		<id column="id" property="id"/>
		<result column="stu_id" property="stuId"/>
		<result column="orderAddress" property="orderAddress"/>
		<result column="orderDate" property="orderDate"/>
		
		<!-- one to one 
		association:配置一对一关联映射
		property:order类中的student对象属性
		javaType:student对象的类型
		-->		
		<association property="student" javaType="student">
			<id column="stu_id" property="id"/>
			<result column="studentName" property="studentName"/>
			<result column="age" property="age"/>
			<result column="studentNo" property="studentNo"/>
			<result column="birthDay" property="birthDay"/>
		</association>
	</resultMap>
	
	
	<select id="selectAll2" resultMap="order_map">
		select 
			o.id,
			o.stu_id,
			o.orderAddress,
			o.orderDate,
			u.id,
			u.studentName,
			u.age,
			u.studentNo,
			u.birthDay
		from t_order o left join t_student u on o.id = u.id;
 		
	</select>
	
</mapper>

测试类:

@org.junit.Test
	public void selectAll2() throws IOException {
		
		//通过输入流创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();		
		//创建SqlSession对象
		SqlSession ss = sqlSessionFactory.openSession();		
		//多态的运用,为掉方法做准备			
		OrderMapper userMapper = ss.getMapper(OrderMapper.class);	
		
		List<Order> l = userMapper.selectAll2();	
		//控制台输出
		for(Order o: l) {
			System.out.println(o.toString());
			System.out.println(o.getStudent());
		}		
		//释放资源
		ss.close();	
	}	

控制台打印结果为:

一个订单对应一个学生。

一对多实例:

主要实现代码:

/**
	 * 一次查询多条数据
	 * @return
	 */
	public List<Student> selectStudents();
	
<?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">
<!-- namespace 命名  #{}:点位符,相当于jdbc的?-->
<mapper namespace="cn.xxs.mapper.UserMapper">

	<!-- id: sql中语句的唯一标识
	    parameterType :入参的数据类型
		resultType:返回结果的数据类型
	 -->	
	<resultMap type="student" id="student_map">
		<id column="id" property="id"/>
		<result column="studentName" property="studentName"/>
		<result column="age" property="age"/>
		<result column="studentNo" property="studentNo"/>
		<result column="birthDay" property="birthDay"/>
		
		<!-- on to many
		collection:配置一对多
		property:student类中order集合的属性名字
		 -->
		<collection property="orderList" ofType="order">
			<id column="oId" property="id"/>
			<result column="stu_id" property="stuId"/>
			<result column="orderAddress" property="orderAddress"/>
			<result column="orderDate" property="orderDate"/>		
		</collection>
	</resultMap>
	
	<select id="selectStudents" resultMap="student_map">
		select 
			u.id,
			u.studentName,
			u.age,
			u.studentNo,
			u.birthDay,
			o.id as oId,
			o.stu_id,
			o.orderAddress,
			o.orderDate		
		from t_student u left join t_order o on u.id = o.stu_id;		
	</select>
</mapper>

测试类:

/**
	 * 查询学生信息
	 * @throws IOException 
	 */
	@org.junit.Test
	public void selectStudents() throws IOException {
		
		//通过输入流创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();		
		//创建SqlSession对象
		SqlSession ss = sqlSessionFactory.openSession();		
		//多态的运用,为掉方法做准备			
		UserMapper userMapper = ss.getMapper(UserMapper.class);					
		//查询学生信息
		List<Student> list = userMapper.selectStudents();
		for(Student stu:list) {
			//控制台输出
			System.out.println(stu);
			for(Order order:stu.getOrderList()) {
				System.out.println(order);
			}
		}			
		//释放资源
		ss.close();	
	}	

控制台打印结果为:

一个学生对应多个订单。

猜你喜欢

转载自blog.csdn.net/qq_43560721/article/details/89486884