链表查询
order表
user表
1,创建表的实体类 User表和Order表
order类中的 private User user 用来关系两个类(连接起来)
查询:根据order订单号查询订单和用户所有信息
创建orderdao方法:
创建配置文件和映射文件 映射文件应放在mapper文件夹中 后缀为(xml)
配置文件用来连接数据库和引用配置文件
mapper文件:
测试
结果
嵌套查询
userdao方法:
order方法:
//column用于第二条收sql语句判断用的列名 select:引用userdao中的查询方法
<select id="selectByid" resultMap="map">
select <include refid="sql01"/> from users u join orders o on u.user_id=o.user_id where o.order_id=#{id}
</select>
<resultMap id="map1" type="com.rqk.qy145.ninth.rqk.entity.Order">
<id property="id" column="order_id"/>
<result property="no" column="order_no"/>
<result property="num" column="order_num"/>
<result property="price" column="order_price"/>
<collection property="user" javaType="com.rqk.qy145.ninth.rqk.entity.User"
column="user_id" select="com.rqk.qy145.ninth.rqk.dao.Userdao.selectById">
</collection>
</resultMap>
<select id="selectByid2" resultMap="map1">
select * from orders where order_id=#{id}
</select>
userdao的映射文件
从order的映射文件中的sql语句查找到user_id user的sq{id} 接受到user_id然后进行判断查询