文章目录
- 项目场景
- 数据库设计
- POJO
- Dao
- Service
- Test
- 运行结果
- 问题及解决
项目场景
SpringBoot + MyBatis ,实现点餐系统的订单查询。
参考:SpringBoot+MyBatis多表联合查询
数据库设计
通常一个订单中会包含多个菜品,即一条 order 里含多个 item,而且 item 的外键是订单编号 orderid ,连接到订单表 orders。
订单表 orders:
先写一点数据用作测试:
订单菜品表 items:
同样先随便写点数据:
POJO
我个人认为 pojo 中变量的名字最好是和数据库中表里的字段名保持一致,这样之后写映射方便一些,不容易出错。
package com.example.spring.pojo;
import java.util.List;
public class Order {
// 订单ID、桌号
private int orderid,tableid;
// 顾客ID、消费时间、订单状态
private String consumerid,time,status;
// 已点菜品列表
private List<Item> itemList;
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public int getTableid() {
return tableid;
}
public void setTableid(int tableid) {
this.tableid = tableid;
}
public String getConsumerid() {
return consumerid;
}
public void setConsumerid(String consumerid) {
this.consumerid = consumerid;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Item> getItemList() {
return itemList;
}
public void setItemList(List<Item> itemList) {
this.itemList = itemList;
}
}
package com.example.spring.pojo;
public class Item {
// 订单列表
private int orderid;
// 菜名、菜品份量、图片
private String name,size,img;
// 单价、数量
private int price,num;
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
Dao
package com.example.spring.dao;
import com.example.spring.pojo.Order;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface OrderDao {
/**
* 一对多联合查询
* */
Order findByOrderid(int orderid);
}
多表查询的映射需要自定义一下结果对象 resultMap:
<?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.example.spring.dao.OrderDao">
<resultMap id="oni" type="com.example.spring.pojo.Order">
<!--重点1:这里的column对应的值为sql语句中对应字段的别名-->
<id column="orderid" property="orderid" javaType="int"></id>
<result column="tableid" property="tableid" javaType="int"></result>
<result column="consumerid" property="consumerid" javaType="String"></result>
<result column="time" property="time" javaType="String"></result>
<result column="status" property="status" javaType="String"></result>
<!--重点2:这里一定要用ofType不能用javaType了,因为对应的Item是一个List,如果用javaType的话就会报错数据类型不匹配-->
<collection property="itemList" ofType="com.example.spring.pojo.Item" >
<id column="orderid" property="orderid" javaType="int"></id>
<id column="name" property="name" javaType="String"></id>
<result column="size" property="size" javaType="String"></result>
<result column="img" property="img" javaType="String"></result>
<result column="price" property="price" javaType="int"></result>
<result column="num" property="num" javaType="int"></result>
</collection>
</resultMap>
<select id="findByOrderid" resultMap="oni" parameterType="int">
<!--重点3:我们查询出了一条记录所有的值,但是两个表有很多字段都是重名的怎么办,那就需要为同名字段起一个别名,别忘了修改resultMap对应字段的column值哦-->
select a.orderid,a.tableid,a.consumerid,a.time,a.status,b.orderid,b.name,b.size,b.img,b.price,b.num from orders a,items b where a.orderid=b.orderid and a.orderid=#{orderid}
</select>
</mapper>
Service
package com.example.spring.service;
import com.example.spring.pojo.Order;
import java.util.List;
public interface OrderService {
/**
* 根据订单编号查询订单
* */
public Order findByOrderid(int orderid);
}
package com.example.spring.service.impl;
import com.example.spring.pojo.Order;
import com.example.spring.dao.OrderDao;
import com.example.spring.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
/**
* 一对多联合查询
* */
public Order findByOrderid(int orderid){
return orderDao.findByOrderid(orderid);
}
}
Test
做个单元测试看看能不能正确访问数据并取出:
package com.example.spring.service.impl;
import com.example.spring.pojo.Item;
import com.example.spring.pojo.Order;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import java.util.List;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
class OrderServiceImplTest extends AbstractTestNGSpringContextTests {
@Autowired
OrderServiceImpl orderService;
@Test
public void findByOrderid() {
Order order = orderService.findByOrderid(10001);
System.out.println("订单编号:" + order.getOrderid());
System.out.println("桌号:" + order.getTableid());
System.out.println("时间:" + order.getTime());
List<Item> itemList = order.getItemList();
for(int i=0;i<itemList.size();i++){
System.out.println(itemList.get(i).getName() + "\t" + itemList.get(i).getSize() + "\t" + itemList.get(i).getNum() + "\t" + itemList.get(i).getPrice());
}
}
}
运行结果
问题及解决
- Cannot resolve symbol “RunWith”.
添加依赖。
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
参考:springboot 单元测试service遇到的坑(cannot resolve symbol “RunWith” )