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 中最重要最强大也是最复杂的标记,而且还提到后面会详细介绍它的高级用法. 听到高 ...
随机推荐
- 总结—angularjs项目
我毕业了-------有点期待生活,又点害怕生活. 总结下最近一个月做的这个项目,项目的开发形式也比较新颖,采用的是前后端分离的形式.我负责前端的管理系统开发,另一个哥们负责利用ABP创建接口,整合后 ...
- pm2在node中的应用
pm2 是一个带有负载均衡功能的Node应用的进程管理器,当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, pm2是完美的. 主要特性: 内建负载均衡(使用Nod ...
- office2007安装时显示安装程序找不到 office.zh-cn\officeLR.cab怎么办
根本原因是和VS2008有关解决方法如下:1. 找到vs2008安装程序(光盘,镜像文件,解压文件都一样),找到WCU文件夹在他里面找到WebDesignerCore文件夹,然后打开它找到WebDes ...
- 数据库字段值为null利用setInc方法无法直接写入
1.数据库字段值为null利用setInc方法无法直接写入,先判断是否为空,再写入. if($points->add($dataList)){ $user=M('cuser'); $null=$ ...
- css3之背景属性之background-size
一.相关属性: background-image: url(“./img/a.jpg”); //设置元素背景图片 background-repeat: repeat/no-repeat: //设置背景 ...
- SPOJ QTREE7
题意 一棵树,每个点初始有个点权和颜色 \(0 \ u\) :询问所有\(u,v\) 路径上的最大点权,要满足\(u,v\) 路径上所有点的颜色都相同 $1 u \(:反转\)u$ 的颜色 \(2 ...
- webapp 的简单开发
web app 的技术平台很多,如adobe phonegap.sencha touch.appcan(国产).dcloud(国产)平台.我选择了dcloud平台,原因:简单,容易上手. web ap ...
- ORM(Object Relational Mapping)框架
ORM(Object Relational Mapping)框架 ORM(Object Relational Mapping)框架采用元数据来描述对象一关系映射细节,元数据一般采用XML格式,并且存放 ...
- redis 存取问题
今天在写短信接口时候,要把验证码存到缓存里面.因为之前别人已经写的有案例,按照之前写的,获取 值.存到数据库,存到redis. 因为有过期时间,需要传过期时间.但是怎么都是不出来... 源码: @Ov ...
- 微信小程序scroll-view隐藏滚动条方法
在wxss里加入以下代码: ::-webkit-scrollbar{ width: 0; height: 0; color: transparent; } 源链接:https://blog.csd ...