mybatis高级查询1

mybatis 同时被 2 个专栏收录
3 篇文章 0 订阅
9 篇文章 0 订阅

 

总结下mybatis高级查询使用

案列:采用 用户,订单,订单详情,商品。

一对一:用户和订单

一对多:订单和订单详情

多对多:订单详情和商品

准备工作:

创建四张表和数据 TB_USER,TB_ORDER,TB_ORDERDETAIL,TB_PRODUCT 

创建对应的实体对象。

见文章末尾。

1 编写OrderMapper接口

@Mapper
public interface OrderMapper{
    Order oneToOne(String orderNumber);
    Order oneToMany(String orderNumber);
    Order manyToMany(String orderNumber);
}

2 编写OrderMapper.xml,这里注意extends可以继承restMap,

<?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.pian.dao.OrderMapper">
    <resultMap id="orderResultMap" type="com.pian.dto.Order" autoMapping="true">
        <id column="oid" property="oid"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="order_number" property="orderNumber" jdbcType="VARCHAR"/>
        <association property="user" javaType="User" autoMapping="true">
            <id column="user_id" property="userid" jdbcType="INTEGER"/>
            <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        </association>
    </resultMap>
    <select id="oneToOne" resultMap="orderResultMap">
      SELECT TB_USER.*,TB_ORDER.* FROM TB_USER,TB_ORDER
      WHERE TB_USER.USERID=TB_ORDER.USER_ID
      AND TB_ORDER.ORDER_NUMBER = #{orderNumber,jdbcType=VARCHAR}
    </select>
    <resultMap id="orderResultMap2" type="com.pian.dto.Order" extends="orderResultMap">
        <collection property="orderDetais" javaType="list" ofType="OrderDetail" autoMapping="true">
            <id column="detail_id" property="detailId"></id>
        </collection>
    </resultMap>
    <select id="oneToMany" resultMap="orderResultMap2">
      SELECT TB_USER.*,TB_ORDER.*,TB_ORDERDETAIL.* FROM TB_USER,TB_ORDER,TB_ORDERDETAIL
      WHERE TB_USER.USERID=TB_ORDER.USER_ID
      AND TB_ORDER.OID=TB_ORDERDETAIL.ORDER_ID
      AND TB_ORDER.ORDER_NUMBER = #{orderNumber,jdbcType=VARCHAR}
    </select>

    <resultMap id="orderResultMap3" type="com.pian.dto.Order" extends="orderResultMap">
        <collection property="orderDetais" javaType="list" ofType="OrderDetail" autoMapping="true">
            <id column="detail_id" property="detailId"></id>
            <association property="product" javaType="Product" autoMapping="true">
                <id column="pid" property="pid"/>
            </association>
        </collection>
    </resultMap>
    <select id="manyToMany" resultMap="orderResultMap3">
        SELECT TB_USER.*,TB_ORDER.*,TB_ORDERDETAIL.*,TB_PRODUCT.* FROM TB_USER,TB_ORDER,TB_ORDERDETAIL,TB_PRODUCT
        WHERE TB_USER.USERID=TB_ORDER.USER_ID
        AND TB_ORDER."OID"=TB_ORDERDETAIL.ORDER_ID
        AND TB_PRODUCT.PID=TB_ORDERDETAIL.PRODUCT_ID
        AND TB_ORDER.ORDER_NUMBER = #{orderNumber,jdbcType=VARCHAR}
    </select>
</mapper>

3 编写service

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    public Order oneToOne(String orderNumber){
        return orderMapper.oneToOne(orderNumber);
    }

    public Order oneToMany(String orderNumber){
        return orderMapper.oneToMany(orderNumber);
    }

    public Order manyToMany(String orderNumber){
        return orderMapper.manyToMany(orderNumber);
    }
}

4 编写测试方法

/**
 * @Auther: Administrator
 * @Date: 2020/2/8 0008 21:14
 * @Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {App.class})
public class AppTest {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Test
    public void testSelectById(){
        User user = userService.selectById(2);
        System.out.println(user);
    }

    @Test
    public void testUpdateUser(){
        User user = new User();
        user.setUserid(2);
        user.setUserName("老虎");
        user.setSex("女");
        userService.updateUser(user);
    }

    @Test
    public void testOneToOne(){
        Order order = orderService.oneToOne("20200201");
        System.out.println(order);
    }

    @Test
    public void testOneToMany(){
        Order order = orderService.oneToMany("20200201");
        System.out.println(order);
    }

    @Test
    public void testManyToMany(){
        Order order = orderService.manyToMany("20200201");
        System.out.println(order);
    }

}

执行最终的结果:

Order(oid=1, userId=2, orderNumber=20200201, user=User(userid=2, userName=老虎, pwd=null, age=10, sex=女, birthday=Sun Feb 09 21:26:23 CST 2020), orderDetais=[OrderDetail(detailId=7001, orderId=1, productId=6001, price=100, status=1, product=Product(pid=6001, pName=书包, price=100, proDetail=书包)), OrderDetail(detailId=8001, orderId=1, productId=6002, price=200, status=1, product=Product(pid=6002, pName=书包2, price=200, proDetail=书包2))])

 

/*
Navicat Oracle Data Transfer
Oracle Client Version : 11.1.0.7.0

Source Server         : person_oracle
Source Server Version : 110200
Source Host           : 127.0.0.1:1521
Source Schema         : SCOTT

Target Server Type    : ORACLE
Target Server Version : 110200
File Encoding         : 65001

Date: 2020-02-10 12:07:45
*/


-- ----------------------------
-- Table structure for TB_ORDER
-- ----------------------------
DROP TABLE "TB_ORDER";
CREATE TABLE "TB_ORDER" (
"OID" NUMBER(10) NULL ,
"USER_ID" NUMBER(4) NULL ,
"ORDER_NUMBER" VARCHAR2(20 BYTE) NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;

-- ----------------------------
-- Records of TB_ORDER
-- ----------------------------
INSERT INTO "TB_ORDER" VALUES ('1', '2', '20200201');
INSERT INTO "TB_ORDER" VALUES ('2', '3', '20200202');
INSERT INTO "TB_ORDER" VALUES ('3', '4', '20200203');
INSERT INTO "TB_ORDER" VALUES ('4', '5', '20200204');

-- ----------------------------
-- Table structure for TB_ORDERDETAIL
-- ----------------------------
DROP TABLE "TB_ORDERDETAIL";
CREATE TABLE "TB_ORDERDETAIL" (
"DETAIL_ID" NUMBER(4) NULL ,
"ORDER_ID" NUMBER(10) NULL ,
"PRODUCT_ID" NUMBER(10) NULL ,
"PRICE" VARCHAR2(10 BYTE) NULL ,
"STATUS" VARCHAR2(4 BYTE) NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;

-- ----------------------------
-- Records of TB_ORDERDETAIL
-- ----------------------------
INSERT INTO "TB_ORDERDETAIL" VALUES ('7001', '1', '6001', '100', '1');
INSERT INTO "TB_ORDERDETAIL" VALUES ('8001', '1', '6002', '200', '1');

-- ----------------------------
-- Table structure for TB_PRODUCT
-- ----------------------------
DROP TABLE "TB_PRODUCT";
CREATE TABLE "TB_PRODUCT" (
"PID" NUMBER(10) NULL ,
"PNAME" VARCHAR2(50 BYTE) NULL ,
"PRICE" VARCHAR2(10 BYTE) NULL ,
"PRO_DETAIL" CLOB NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;

-- ----------------------------
-- Records of TB_PRODUCT
-- ----------------------------
INSERT INTO "TB_PRODUCT" VALUES ('6001', '书包', '100', '书包');
INSERT INTO "TB_PRODUCT" VALUES ('6002', '书包2', '200', '书包2');
INSERT INTO "TB_PRODUCT" VALUES ('6003', '书包3', '300', '书包3');
INSERT INTO "TB_PRODUCT" VALUES ('6004', '书包4', '400', '书包4');
INSERT INTO "TB_PRODUCT" VALUES ('6005', '书包5', '500', '书包5');

-- ----------------------------
-- Table structure for TB_USER
-- ----------------------------
DROP TABLE "TB_USER";
CREATE TABLE "TB_USER" (
"USERID" NUMBER(4) NOT NULL ,
"USER_NAME" VARCHAR2(100 BYTE) NOT NULL ,
"PWD" VARCHAR2(100 BYTE) NULL ,
"AGE" NUMBER(3) NULL ,
"SEX" VARCHAR2(2 BYTE) NULL ,
"BIRTHDAY" DATE NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;

-- ----------------------------
-- Records of TB_USER
-- ----------------------------
INSERT INTO "TB_USER" VALUES ('2', '老虎', null, '10', '女', TO_DATE('2020-02-09 21:26:23', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "TB_USER" VALUES ('3', '李四', '123456', '10', '男', TO_DATE('2020-02-08 20:40:45', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "TB_USER" VALUES ('4', '王五', '123456', '10', '男', TO_DATE('2020-02-08 20:40:45', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "TB_USER" VALUES ('5', '赵六', '123456', '10', '男', TO_DATE('2020-02-08 20:40:45', 'YYYY-MM-DD HH24:MI:SS'));

-- ----------------------------
-- Sequence structure for MYSEQ
-- ----------------------------
DROP SEQUENCE "MYSEQ";
CREATE SEQUENCE "MYSEQ"
 INCREMENT BY 1
 MINVALUE 1
 MAXVALUE 9999999999999999999999999999
 START WITH 21
 CACHE 20;

-- ----------------------------
-- Sequence structure for SEQ_USER
-- ----------------------------
DROP SEQUENCE "SEQ_USER";
CREATE SEQUENCE "SEQ_USER"
 INCREMENT BY 1
 MINVALUE 1
 MAXVALUE 9999999999999999999999999999
 START WITH 21
 CACHE 20;

-- ----------------------------
-- Uniques structure for table TB_ORDER
-- ----------------------------
ALTER TABLE "TB_ORDER" ADD UNIQUE ("OID");

-- ----------------------------
-- Uniques structure for table TB_ORDERDETAIL
-- ----------------------------
ALTER TABLE "TB_ORDERDETAIL" ADD UNIQUE ("DETAIL_ID");

-- ----------------------------
-- Uniques structure for table TB_PRODUCT
-- ----------------------------
ALTER TABLE "TB_PRODUCT" ADD UNIQUE ("PID");

-- ----------------------------
-- Indexes structure for table TB_USER
-- ----------------------------

-- ----------------------------
-- Uniques structure for table TB_USER
-- ----------------------------
ALTER TABLE "TB_USER" ADD UNIQUE ("USER_NAME");

-- ----------------------------
-- Checks structure for table TB_USER
-- ----------------------------
ALTER TABLE "TB_USER" ADD CHECK ("USERID" IS NOT NULL);
ALTER TABLE "TB_USER" ADD CHECK ("USER_NAME" IS NOT NULL);

-- ----------------------------
-- Primary Key structure for table TB_USER
-- ----------------------------
ALTER TABLE "TB_USER" ADD PRIMARY KEY ("USERID");

-- ----------------------------
-- Foreign Key structure for table "TB_ORDERDETAIL"
-- ----------------------------
ALTER TABLE "TB_ORDERDETAIL" ADD FOREIGN KEY ("ORDER_ID") REFERENCES "TB_ORDER" ("OID");
ALTER TABLE "TB_ORDERDETAIL" ADD FOREIGN KEY ("PRODUCT_ID") REFERENCES "TB_PRODUCT" ("PID");

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值