文章目录
简述一些注意事项代码示例OrdersMapper.xmlOrdersMapper.java
简述
resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的数千行代码。ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
一些注意事项
MyBatis 属于持久层 主要写的就是sql非查询一定要手动的使用commit()!产品依赖着产品类型 一个产品对应着一种产品类型
<//resultMap>是Mybatis的结果集封装,搭配<//select><//association>等标签的resultMap属性使用
必须有id必须是一个已存在的类<//association>标签只能放在<//resultMap>标签中的最后面
代码示例
我们这里以山科超市的订单模块为例: 订单表的大致逻辑是:orderid, uid, orderprice, addtime, ordersdetails,一个订单只对应一个用户,一个订单可以对应多个订单详情;一个订单详情表中对应了一个产品,一个产品又对应了一个产品类别
OrdersMapper.xml
<?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">
<mapper namespace="com.lazy.mappers.OrdersMapper">
<sql id="orderSql"> o.orderid,u.uname,o.orderprice,o.addtime,p.pcode,p.pname,od.pnum
</sql>
<sql id="orderUserSql">u.uname, o.orderid,o.orderprice,o.addtime,p.pcode,p.pname,od.pnum
</sql>
<insert id="addOrder">
INSERT INTO orders (orderid,uid,orderprice) VALUES(#{orders.orderid},#{orders.uid.uid},#{orders.orderprice})
</insert>
<resultMap id="resOrder" type="com.lazy.domain.Orders">
<id property="orderid" column="orderid"/>
<result property="orderprice" column="orderprice"/>
<result property="addtime" column="addtime"/>
<association property="uid" javaType="com.lazy.domain.Users">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
</association>
<collection property="details" ofType="com.lazy.domain.OrderDetails">
<id property="oddid" column="oddid"/>
<result property="pnum" column="pnum"/>
<association property="pid" javaType="com.lazy.domain.Products">
<id property="pid" column="pid"/>
<result property="pcode" column="pcode"/>
<result property="pname" column="pname"/>
<association property="ptid" javaType="com.lazy.domain.PType">
<id property="ptid" column="ptid"/>
<result property="ptname" column="ptname"/>
</association>
</association>
</collection>
</resultMap>
<select id="findByOrderId" resultMap="resOrder">
SELECT
<include refid="orderSql"/>
FROM orders o
INNER JOIN orderdetails od ON o.orderid = od.orderid
INNER JOIN products p ON p.pid = od.pid
INNER JOIN ptype pt ON p.ptid = pt.ptid
INNER JOIN users u ON u.uid = o.uid
WHERE o.orderid = #{orderId,jdbcType=VARCHAR}
</select>
<select id="findByUserId" resultMap="resOrder">
SELECT
<include refid="orderUserSql"/>
FROM orders o
INNER JOIN orderdetails od ON o.orderid = od.orderid
INNER JOIN products p ON p.pid = od.pid
INNER JOIN ptype pt ON p.ptid = pt.ptid
INNER JOIN users u ON u.uid = o.uid
WHERE u.uid = #{uid,jdbcType=VARCHAR}
</select>
</mapper>
OrdersMapper.java
这里一共定义了三个接口,分别用于以订单id查找、以用户id查找,以及添加订单
package com
.lazy
.mappers
;
import com
.lazy
.domain
.Orders
;
import org
.apache
.ibatis
.annotations
.Param
;
public interface OrdersMapper {
Orders
findByOrderId(@Param("orderId") String orderId
);
Orders
findByUserId(@Param("uid") String uid
);
int addOrder(@Param("orders") Orders orders
);
}