MyBatis(7)高级查询
本次全部学习内容:MyBatisLearning

具体用到那个类再去说明类的内容
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();
}
MyBatis(7)高级查询的更多相关文章
- MyBatis高级查询
-------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...
- mybatis中的高级查询
Mybatis作为一个ORM框架,肯定是支持sql高级查询的. 下面的一个案例来为大家详细讲解mybatis中的高级查询. 案例说明: 此案例的业务关系是用户,订单,订单详情与商品之间的关系. 以订单 ...
- MyBatis从入门到精通(第6章):MyBatis 高级查询->6.1.2高级结果映射之一对多映射
jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.IntelliJ IDEA 2019.3.1 本章主要包含的内容为 MyBatis 的高级结果映射,主要处理数据库一对一.一对多的 ...
- MyBatis从入门到精通(第6章):MyBatis 高级查询->6.1.1高级结果映射之一对一映射
jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.IntelliJ IDEA 2019.2.4 本章主要包含的内容为 MyBatis 的高级结果映射,主要处理数据库一对一.一对多的 ...
- 持久层之 MyBatis: 第三篇 :缓存 And 高级查询
MyBatis入门到精通3 缓存机制 Mybatis一级缓存测试 Mybatis二级缓存测试 高级查询 表关系说明 一对一查询 一对多查询 多对多查询 缓存机制 正如大多数持久层框架一样,MyBati ...
- MyBatis 高级查询之多对多查询(十一)
高级查询之多对多查询 查询条件:根据玩家名,查询游戏信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据玩家名查询游戏 * @param name ...
- MyBatis 高级查询之一对多查询(十)
高级查询之一对多查询 查询条件:根据游戏名称,查询游戏账号信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据游戏名查询游戏账号 * @param ...
- MyBatis 高级查询之一对一查询(九)
高级查询之一对一查询 查询条件:根据游戏角色ID,查询账号信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据角色ID查询账号信息 * @para ...
- MyBatis 高级查询环境准备(八)
MyBatis 高级查询 之前在学习 Mapper XML 映射文件时,说到 resultMap 标记是 MyBatis 中最重要最强大也是最复杂的标记,而且还提到后面会详细介绍它的高级用法. 听到高 ...
随机推荐
- HTML5坦克大战(韩顺平版本)
HTML5坦克大战(韩顺平版本) 2017-3-22 22:46:22 by SemiconductorKING 去年暑假学习了一下HTML5实现简单的坦克大战,觉得对JavaScript初学者来说, ...
- Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)
正如你所看到的,Blend是一个非常强大的节约时间的设计工具,在Blend下能够设计出很多满意的动画作品,或许他具体是怎么实现的,通过什么方式实现的我们还是一无所知.本篇将续前面几篇基础动画之上,详细 ...
- window.open()被浏览器拦截问题汇总
一.问题描述 最近在做项目的时候碰到了使用window.open被浏览器拦截的情况,虽然在自己的环境可以对页面进行放行,但是对用户来说,不能要求用户都来通过拦截.何况当出现拦截时,很多用户根本不知道发 ...
- 2017年11月27日 C#MDI窗体创建&记事本打印&记事本查找、自动换行
MDI窗体第一个父窗体 把属性里的IsMdiContainer设置为true就可以了 父窗体连接子窗体 //创建一个新的类,用来连接别的窗体,并且别的窗体为唯一窗体 List<Form> ...
- centos自带python2.6升级到python2.7。并解决yum pip easy_install pip等模块兼容性问题
参考原文: https://www.cnblogs.com/kimyeee/p/7250560.html https://www.cnblogs.com/galaxy-gao/p/5796488 ...
- 三种角度解释href/src/link/import区别
网上查到的几种不同但比较容易理解的解释 解释一: href是Hypertext Reference的缩写,表示超文本引用.用来建立当前元素和文档之间的链接.常用的有:link.a.例如: <li ...
- flask接收前台的ajax的post数据
html <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...
- bootstrap-datepicker汉化
bootstrap-datepicker 是一个非常优秀的时间选择插件,默认是英文显示日期的,通过下面几个小修改让其支持默认中文 1.首先将 bootstrap-datepicker.js 另存为 u ...
- vue-router 的使用
vue-router 是 vue 的 一个特色. 下面介绍vue-router 的使用: 一.先将vue-router作为vue 的一个插件使用 import Vue from 'vue' imp ...
- awk日志分析
前言 今天我们来讲讲如何用awk进行网站日志分析,得到页面平均耗时排行 文件 [xingxing.dxx@30_28_6_20 ~]$ cat logs /Oct/::: +] GET /pages/ ...