<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="85FajrCa-1729422932142" src="https://player.bilibili.com/player.html?aid=113339355107350"></iframe>
实验报告3-数据库框架实现数据操作1
一、实现思路
使用Spring Boot整合MyBatis-Plus完成开支分析案例的后台数据整合功能。要求:
1、根据静态页面的echarts数据,设计返回前端数据包装类CategoryVo。
2、多表关联查询,实现根据订单id查询商品订单信息。
3、使用xml配置文件通过MybatisPlus实现数据封装。
4、测试数据封装。
5、实现service层功能。
6、测试service层功能。
二、实验步骤
1、搭建数据库环境
springbootdata数据库,t_category表、t_goods表、t_order表、t_order_goods表
2、新建项目
Name:Exp3,GroupID:com.sw
引入pom.xml文件、定义SpringBoot入口函数
3、实现功能
(1)项目配置信息
classpath*目录,application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdata?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis-plus:
mapper-locations: classpath*:mybatis/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
(2)实体类
pojo包,Category.java
@Data
@TableName("t_category")
public class Category {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
}
pojo.vo包,CategoryVo.java
@Data
public class CategoryVo {
private Integer id;
private String name;
private BigDecimal cost;
}
(3)Mapper接口
mapper包,CategoryMapper.java
public interface CategoryMapper extends BaseMapper<Category> {
List<CategoryVo> getCategoryVoList();
}
classpath*:mapper目录,CategoryMapper.xml
<select id="getCategoryVoList" resultType="com.sw.pojo.vo.CategoryVo">
SELECT c.*, SUM(g.price*og.amount) cost
FROM t_category c LEFT JOIN t_goods g ON g.category_id=c.id
LEFT JOIN t_order_goods og ON g.id=og.goods_id
GROUP BY c.id
</select>
SpringBoot入口函数
@MapperScan("com.sw.mapper")
(4)测试CategoryMapper
mapper包,CategoryMapper.java,getCategoryVoById()右键选择Generate→ Test.....
@SpringBootTest
class CategoryMapperTest {
@Autowired
private CategoryMapper categoryMapper;
@Test
void getCategoryVoList() {
List<CategoryVo> categoryVoList = categoryMapper.getCategoryVoList();
for (CategoryVo categoryVo : categoryVoList) {
System.out.println(categoryVo);
}
}
}
(5)service层
service包,CategoryService.java
public interface CategoryService extends IService<Category> {
List<CategoryVo> getCategoryVoList();
}
service.impl包,CategoryServiceImpl.java
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public List<CategoryVo> getCategoryVoList() {
List<CategoryVo> categoryVoList = categoryMapper.getCategoryVoList();
for (CategoryVo categoryVo : categoryVoList) {
if(ObjectUtils.isEmpty(categoryVo.getCost())){
categoryVo.setCost(new BigDecimal(0));
}
}
return categoryVoList;
}
}
(6)测试CategoryService
service包,CategoryService.java,getCategoryVoList()右键选择Generate→ Test.....
@SpringBootTest
class CategoryServiceTest {
@Autowired
private CategoryService categoryService;
@Test
void getCategoryVoList() {
List<CategoryVo> categoryVoList = categoryService.getCategoryVoList();
for (CategoryVo categoryVo : categoryVoList) {
System.out.println(categoryVo);
}
}
}
标签:categoryVoList,java,框架,数据库,private,categoryVo,getCategoryVoList,实验报告,id
From: https://blog.csdn.net/u010288901/article/details/143096958