关于Java工程mybatis实现多表查询的过程,我会为你提供详细的攻略。
什么是mybatis
先了解什么是mybatis,MyBatis是一个开源的、基于 Java 的持久层框架。通过XML描述符或注释来将对象与存储过程或 SQL 语句绑定起来,实现了将程序中的 Java 对象和数据库中的数据进行映射,使得数据的操作和 Java 代码的操作可以分离。
如何实现多表查询
在mybatis中实现多表查询需要用到联表查询的知识,而联表查询需要遵循以下步骤:
步骤一:建立实体类
首先创建需要查询的实体类,这里以订单和订单项为例,建立两个实体类Order和OrderItem。
步骤二:建立Mapper
在mapper接口中建立查询订单及订单项的方法,方法中通过sql语句使用联表查询,如下:
public interface OrderMapper {
/**
* 根据订单ID查询订单及订单项,使用联表查询
*
* @param orderId 订单ID
* @return 订单及订单项列表
*/
List<OrderAndItem> queryOrderAndItemById(int orderId);
}
在mapper XML文件夹中编写查询语句,如下:
<select id="queryOrderAndItemById" resultMap="OrderAndItemMap">
SELECT
o.*,
oi.*
FROM
tb_order as o
LEFT JOIN tb_order_item as oi ON o.id = oi.order_id
WHERE
o.id = #{orderId}
</select>
步骤三:建立ResultMap
在mapper XML文件夹中创建ResultMap,来映射联表查询后的结果,如下:
<resultMap id="OrderAndItemMap" type="com.example.OrderAndItem">
<result property="orderId" column="id"/>
<result property="orderNo" column="order_no"/>
...
<result property="itemId" column="id"/>
<result property="orderItemId" column="order_id"/>
...
</resultMap>
步骤四:建立业务处理类
最后,在业务处理类中调用mapper的查询方法,并将结果映射到对应的实体类中。如下:
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public OrderAndItem queryOrderAndItemById(int orderId){
List<OrderAndItem> list = orderMapper.queryOrderAndItemById(orderId);
OrderAndItem orderAndItem = null;
if(list.size() > 0){
orderAndItem = list.get(0);
}
return orderAndItem;
}
}
示例
示例一
假设有订单和订单项两张表,同时需要查询订单及订单项的详细信息,可以使用以下代码:
public interface OrderMapper {
/**
* 根据订单ID查询订单及订单项,使用联表查询
*
* @param orderId 订单ID
* @return 订单及订单项列表
*/
List<OrderAndItem> queryOrderAndItemById(int orderId);
}
@Mapper
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public OrderAndItem queryOrderAndItemById(int orderId){
List<OrderAndItem> list = orderMapper.queryOrderAndItemById(orderId);
OrderAndItem orderAndItem = null;
if(list.size() > 0){
orderAndItem = list.get(0);
}
return orderAndItem;
}
}
@Data
public class Order {
private Integer id;
private String orderNo;
...
}
@Data
public class OrderItem {
private Integer id;
private Integer orderId;
...
}
@Data
public class OrderAndItem {
private Integer orderId;
private String orderNo;
...
private Integer itemId;
private Integer orderItemId;
...
}
<select id="queryOrderAndItemById" resultMap="OrderAndItemMap">
SELECT
o.*,
oi.*
FROM
tb_order as o
LEFT JOIN tb_order_item as oi ON o.id = oi.order_id
WHERE
o.id = #{orderId}
</select>
<resultMap id="OrderAndItemMap" type="com.example.OrderAndItem">
<result property="orderId" column="id"/>
<result property="orderNo" column="order_no"/>
...
<result property="itemId" column="id"/>
<result property="orderItemId" column="order_id"/>
...
</resultMap>
示例二
假设有商品表、订单表和订单项表三张表,需要查询订单详细信息及对应的商品信息,可以使用以下代码:
public interface OrderMapper {
/**
* 查询订单详情及对应商品信息
*
* @param orderId 订单ID
* @return 订单详情及对应商品信息
*/
List<OrderDetails> queryOrderDetailsById(int orderId);
}
@Mapper
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public OrderDetails queryOrderDetailsById(int orderId){
List<OrderDetails> list = orderMapper.queryOrderDetailsById(orderId);
OrderDetails orderDetails = null;
if(list.size() > 0){
orderDetails = list.get(0);
}
return orderDetails;
}
}
@Data
public class Order {
private Integer id;
private String orderNo;
...
}
@Data
public class OrderItem {
private Integer id;
private Integer orderId;
private Integer goodsId;
...
}
@Data
public class Goods {
private Integer id;
private String goodsName;
...
}
@Data
public class OrderDetails {
private Integer orderId;
private String orderNo;
...
private Integer goodsId;
private String goodsName;
...
}
<select id="queryOrderDetailsById" resultMap="OrderDetailsMap">
SELECT
o.*,
oi.*,
g.*
FROM
tb_order as o
LEFT JOIN tb_order_item as oi ON o.id = oi.order_id
LEFT JOIN tb_goods as g ON oi.goods_id = g.id
WHERE
o.id = #{orderId}
</select>
<resultMap id="OrderDetailsMap" type="com.example.OrderDetails">
<result property="orderId" column="id"/>
<result property="orderNo" column="order_no"/>
...
<result property="goodsId" column="id"/>
<result property="goodsName" column="goods_name"/>
...
</resultMap>
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java工程mybatis实现多表查询过程详解 - Python技术站