标签:strong 完成 res gets list 结果 分享图片 msn 语句
具体用到那个类再去说明类的内容
public class OrdersCustom extends Orders{ //添加用户属性 /*USER.username, USER.sex, USER.address */ private String username; private String sex; private String address; ....... }
orders.java
public class Orders { private Integer id; private Integer userId; private String number; private Date createtime; private String note; //用户信息 private User user; ............ }
首先在本次的xml文件中,OrderMapperCustomer.xml文件中
<!-- 一对一查询 --> <!-- resultType --> <select id="findOrderUsers" resultType="com.MrChengs.po.OrdersCustom"> select orders.*,user.username,user.sex,user.address from orders ,user where orders.user_id = user.id; </select>
在OrderMapperCustomerj接口类中
//resultType //查询订单关联用户信息 public List<OrdersCustom> findOrderUsers() throws Exception;
实现类:
//resultType //实现用户订单关联信息 @Test public void testfindOrderUsers() throws Exception { SqlSession sqlSession = getSqlSessionFactory().openSession(); //代理对象 OrderMapperCustomer mapper = sqlSession.getMapper(OrderMapperCustomer.class); //测试findOrderUsers List<OrdersCustom> orders = mapper.findOrderUsers(); for(OrdersCustom o : orders){ System.out.println(o); } sqlSession.close(); }
结果:
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3ecd23d9] 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 OrdersCustom [username=王五, sex=2, address=null, getUsername()=王五, getSex()=2, getAddress()=null,...... OrdersCustom [username=王五, sex=2, address=null, getUsername()=王五, getSex()=2, getAddress()=null,..... OrdersCustom [username=张三, sex=1, address=北京市, getUsername()=张三, getSex()=1, getAddress()=北京市,.....
<!-- resultMap --> <!-- 映射到Orders这个表中 --> <!-- Orders需要添加user的信息 --> <resultMap type="com.MrChengs.po.Orders" id="odersandUser"> <!-- 订单信息的唯一 标识 --> <!-- id;配置查询列的唯一标识,订单信息中的唯一标识,如果有多个则配置多个id属性 --> <!-- result:把订单列中的普通列进行映射 --> <!-- column:数据库查询的列,property:映射表中的属性 --> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <!-- 配置映射关联的用户信息 --> <!-- association:用于映射关联关系的对象信息 --> <!-- id:关联用户的唯一标识 --> <!-- column:指定唯一标识的用户信息列,数据库的查询列--> <!-- javaType:映射user的那个属性 --> <association property="user" javaType="com.MrChengs.po.User"> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> </association> </resultMap> <select id="findOrderUserByMap" resultMap="odersandUser"> select orders.*,user.username,user.sex,user.address from orders ,user where orders.user_id = user.id; </select>
接口类:
//resultMap //查询订单关联用户信息 public List<Orders> findOrderUserByMap() throws Exception;
测试类:
//resultType //实现用户订单关联信息 @Test public void testfindOrderUserByMap() throws Exception { SqlSession sqlSession = getSqlSessionFactory().openSession(); //代理对象 OrderMapperCustomer mapper = sqlSession.getMapper(OrderMapperCustomer.class); //测试findOrderUsers List<Orders> orders = mapper.findOrderUserByMap(); for(Orders order : orders){ System.out.println(order); } sqlSession.close(); }
结果:
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3ecd23d9] 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 Orders [id=3, userId=1, number=1000010, createtime=Wed Feb 04 13:22:35 CST 2015, note=null, user=User [id=1, username=王五, birthday=null, sex=2, address=null], orderdetails=null] Orders [id=4, userId=1, number=1000011, createtime=Tue Feb 03 13:22:41 CST 2015, note=null, user=User [id=1, username=王五, birthday=null, sex=2, address=null], orderdetails=null] Orders [id=5, userId=10, number=1000012, createtime=Thu Feb 12 16:13:23 CST 2015, note=null, user=User [id=10, username=张三, birthday=null, sex=1, address=北京市], orderdetails=null]
SELECT orders.*, USER.username, USER.sex, USER.address, //防止id段重复 orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id FROM orders, USER, orderdetail WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
首先在接口类:
//查询订单及订单明细 //一对多 public List<Orders> findOrderUserDetailResultMap() throws Exception;
xml文件中;
<!-- orders & User在之前写过,此时可以不在累赘,使用继承 --> <resultMap type="com.MrChengs.po.Orders" id="OrderUserDetailResultMap" extends="odersandUser"> <!-- orders --> <!-- User --> <!-- 此时使用了继承 --> <!-- 订单明细 --> <!-- 一个订单对应多条明细 --> <!-- property:映射到Orders的那个属性 --> <!-- ofType:指映射集合pojo的类型 --> <collection property="orderdetails" ofType="com.MrChengs.po.Orderdetail"> <!-- id:订单明细的唯一标识 --> <!-- property:将订单映射到订单唯一标识的 com.MrChengs.po.Orderdetail的那个属性 --> <id column="orderdetail_id" property="id" /> <result column="items_id" property="itemsId"/> <result column="items_num" property="itemsNum"/> <result column="orders_id" property="ordersId"/> </collection> </resultMap> <select id="findOrderUserDetailResultMap" resultMap="OrderUserDetailResultMap"> SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id FROM orders, USER, orderdetail WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id </select>
orders.java类中添加属性:
//订单明细 private List<Orderdetail> orderdetails;
测试类中:
//实现用户订单关联信息 //多对多 @Test public void testfindOrderUserByMap() throws Exception { SqlSession sqlSession = getSqlSessionFactory().openSession(); //代理对象 OrderMapperCustomer mapper = sqlSession.getMapper(OrderMapperCustomer.class); //测试findOrderUsers List<Orders> orders = mapper.findOrderUserDetailResultMap(); for(Orders order : orders){ System.out.println(order); } sqlSession.close(); }
DEBUG [main] - ==> Preparing: SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id FROM orders, USER, orderdetail WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 4 Orders [id=3, userId=1, number=1000010, createtime=Wed Feb 04 13:22:35 CST 2015, note=null, user=User [id=1, username=王五, birthday=null, sex=2, address=null], orderdetails=[Orderdetail [id=1, ordersId=3, itemsId=1, itemsNum=1], Orderdetail [id=2, ordersId=3, itemsId=2, itemsNum=3]]] Orders [id=4, userId=1, number=1000011, createtime=Tue Feb 03 13:22:41 CST 2015, note=null, user=User [id=1, username=王五, birthday=null, sex=2, address=null], orderdetails=[Orderdetail [id=3, ordersId=4, itemsId=3, itemsNum=4], Orderdetail [id=4, ordersId=4, itemsId=2, itemsNum=3]]]
SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id, items.name items_name, items.detail items_detail, items.price items_price FROM orders, USER, orderdetail, items WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id
//多对多findByMany public List<User> findByMany() throws Exception;
xml文件中:
<!-- 查询用户及购买商品 --> <resultMap type="com.MrChengs.po.User" id="UserOrdaerEtc"> <!-- User --> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> <!-- 订单信息 一个用户对应多个订单,使用collection映射 --> <collection property="ordersList" ofType="com.MrChengs.po.Orders"> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <!-- 订单明细 --> <collection property="orderdetails" ofType="com.MrChengs.po.Orderdetail"> <id column="orderdetail_id" property="id"/> <result column="items_id" property="itemsId"/> <result column="items_num" property="itemsNum"/> <result column="orders_id" property="ordersId"/> <!-- 商品信息 一个订单明细对应一个商品 --> <association property="items" javaType="com.MrChengs.po.Items"> <id column="items_id" property="id"/> <result column="items_name" property="name"/> <result column="items_detail" property="detail"/> <result column="items_price" property="price"/> </association> </collection> </collection> </resultMap> <select id="findByMany" resultMap="UserOrdaerEtc"> SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id, items.name items_name, items.detail items_detail, items.price items_price FROM orders, USER, orderdetail, items WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id </select>
测试类:
//多对多的查询 @Test public void testfindByMany() throws Exception { SqlSession sqlSession = getSqlSessionFactory().openSession(); //代理对象 OrderMapperCustomer mapper = sqlSession.getMapper(OrderMapperCustomer.class); //测试findOrderUsers List<User> orders = mapper.findByMany(); for(User order : orders){ System.out.println(order); } sqlSession.close(); }
标签:strong 完成 res gets list 结果 分享图片 msn 语句
原文地址:https://www.cnblogs.com/Mrchengs/p/9763795.html