SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id
package cn.edu.hpu.mybatis.PO; import java.util.Date; public class Orders { private Integer id; private Integer userId; private String number; private Date creattime; private String note; //get和set方法省略... }
package cn.edu.hpu.mybatis.PO; //Orders订单的拓展类 //通过此类映射订单和用户查询的结果,让此类继承包括字段较多的pojo类 public class OrdersCustom extends Orders{ //添加用户属性 private String username; private String sex; private String address; //get和set方法省略... }
<?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命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 --> <mapper namespace="cn.edu.hpu.mybatis.mapper.OrdersCustomMapper"> <!-- 查询订单关联查询用户信息 --> <select id="findOrdersUser" resultType="cn.edu.hpu.mybatis.PO.OrdersCustom"> SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id </select> </mapper>在SqlMapConfig.xml中我们扫描了mapper包所有的Mapper映射文件,所以这里就不在SqlMapConfig.xml中单独加载Mapper映射文件了。
package cn.edu.hpu.mybatis.mapper; import java.util.List; import cn.edu.hpu.mybatis.PO.OrdersCustom; //订单mapper public interface OrdersCustomMapper { //查询订单关联查询用户信息 public List<OrdersCustom> findOrdersUser() throws Exception; }
@Test public void testFindOrdersUser() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); //创建代理对象 OrdersCustomMapper ordersMapperCustom=sqlSession.getMapper(OrdersCustomMapper.class); //调用mapper的方法 List<OrdersCustom> list=ordersMapperCustom.findOrdersUser(); for (int i = 0; i < list.size(); i++) { OrdersCustom oc=list.get(i); System.out.println(oc.getUsername()+"|"+oc.getSex()+"|"+oc.getAddress()); } sqlSession.close(); }
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 21573890. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1493102] DEBUG [main] - ==> Preparing: SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 3
package cn.edu.hpu.mybatis.PO; import java.util.Date; public class Orders { private Integer id; private Integer userId; private String number; private Date creattime; private String note; //用户信息 private User user; ... }
<!-- 订单查询关联用户的resultMap 将整个查询结果映射到cn.edu.hpu.mybatis.PO.Orders中--> <resultMap type="cn.edu.hpu.mybatis.PO.Orders" id="OrdersUserResultMap"> <!-- 配置映射的订单信息 --> <!-- id:指定查询中的唯一标识,订单信息中的唯一标识,如果有多个列组成唯一标识, 那么就要配置多个id 。 column:订单信息的唯一标识列。 property:订单信息的唯一标识列所映射到Orderds中哪个属性--> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="number" property="number"/> <result column="creattime" property="creattime"/> <result column="note" property="note"/> <!-- 配置关联用户的用户信息 --> <!-- association:用于映射关联查询单个对象的信息。 property:要将关联查询的用户信息映射到Orders中哪个属性--> <association property="user" javaType="cn.edu.hpu.mybatis.PO.User"> <!-- id:关联查询用户的唯一标识 指定唯一标识用户信息的列 javaType:映射到User的哪个属性 --> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> <result/> </association> </resultMap>
<!-- 查询订单关联查询用户信息,使用resultMap --> <select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap"> SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id </select>
package cn.edu.hpu.mybatis.mapper; import java.util.List; import cn.edu.hpu.mybatis.PO.Orders; import cn.edu.hpu.mybatis.PO.OrdersCustom; //订单mapper public interface OrdersCustomMapper { //查询订单关联查询用户使用resultMap public List<Orders> findOrdersUserResultMap() throws Exception; }
测试:
@Test public void testFindOrdersUserResultMap() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); //创建代理对象 OrdersCustomMapper ordersMapperCustom=sqlSession.getMapper(OrdersCustomMapper.class); //调用mapper的方法 List<Orders> list=ordersMapperCustom.findOrdersUserResultMap(); for (int i = 0; i < list.size(); i++) { Orders o=list.get(i); User u=o.getUser(); System.out.println(u.getUsername()+"|"+u.getSex()+"|"+u.getAddress()); } sqlSession.close(); }
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 21443983. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@147358f] DEBUG [main] - ==> Preparing: SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 3
resultMap可以实现延迟加载,resultType无法实现延迟加载。
转载请注明出处:http://blog.csdn.net/acmman/article/details/46602553