用户与用户订单
关系分析:一个用户对应多个订单,多个订单对应一个用户。(1...n)
实现步骤:
- 导入jar包;
- 创建数据库数据表 。在多的一方,添加一的一方的主键作为外键,利用外键关联;
- 创建pojo类;
- 编写dao层接口;
- 根据pojo类创建相应的xxxMapper.xml文件,完成持久化类与数据表的映射,以及数据表与表之间的关系映射,并根据dao层方法定义持久化类的操作;
- 创建mybatis-config.xml文件,完成连接池创建、与数据库的连接;
- 测试操作
1、实体类创建 :
一的一方(放置多的一方的集合) 多的一方(放置一的一方的对象)
Class A(){ Class B(){
Set<B> bs = new HashSet<B>() ; A a;
}
2、映射文件配置:
2.1OrderMapper.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.jjxy.mapper.OrderMapper">
<!-- 配置数据库字段和实体属性之间的映射关系
定义resultMap-->
<resultMap id="order_list_map" type="order">
<!-- id用于映射主键 -->
<id property="id" column="id"/>
<!-- 普通属性映射 -->
<result property="userId" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<!--配置多对一的关系 :引入一的一方属性-->
<association property="user" javaType="com.jjxy.pojo.User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
</association>
</resultMap>
<!--多的一方查询:根据user的id查询订单信息,多表连接返回resultMap-->
<select id="getOrderList" parameterType="int" resultMap="order_list_map">
SELECT *from `order` o,user u WHERE o.user_id = u.id and o.user_id = #{id}
</select>
</mapper>
2.2UserMapper.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">
<!--
采用动态代理的方式实现dao:不需要接口,只需要实现类
1.命名空间:必须是接口的全路径名
2.接口方法名必须与sql id一致
3.接口的入参与parameterType 类型一致
4.接口的返回值与resultType 类型一致
-->
<mapper namespace="com.jjxy.mapper.UserMapper">
<!-- 配置 resultMap 数据库映射pojo属性-->
<resultMap id="user_list_map" type="user">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<!--设置一对多的关系。在一的一方设置多的一方 :集合
property :pojo类设置的集合对象属性
select :在执行根据id查询用户的时候,级联查询用户的订单。
column:传入的入参 id
-->
<collection property="listOrder" javaType="List" column="id" ofType="com.jjxy.pojo.Order"
select="com.jjxy.mapper.OrderMapper.getOrderList" fetchType="lazy">
<id property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</collection>
</resultMap>
<!--一对多的关系要引入resultMap-->
<select id="getUserById" parameterType="int" resultMap="user_list_map">
SELECT *from user where id = #{id}
</select>
</mapper>
3、测试类
public class OneToManyTest {
@Test
public void oneTomanyTest(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println("用户有:");
System.out.println("user:"+user);
List<Order> listOrder = user.getListOrder();
System.out.println("订单有:");
for(Order order:listOrder){
System.out.println("Order:"+order);
}
}
}