开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第4天,点击查看活动详情
(一)结构结构说明
book表:
category表:
evaluation表:
member表:
member_read_state表:
user表:
(二)开发图书分类接口
1.创建entity
@TableName("category")
public class Category {
@TableId(type = IdType.AUTO)
private Long categoryId;
private String categoryName;
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@Override
public String toString() {
return "Category{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + ''' +
'}';
}
}
代码说明:
- @TableName:实体类对应的表名
- @TableId(type = IdType.AUTO):对应数据表主键自增ID
- private Long categoryId:对应数据表字段(一般情况下,整型默认采用Long类型)
- toString:方便控制台输出返回值
2.定义interface类 CategoryService
package com.imooc.reader.service;
import com.imooc.reader.entity.Category;
import java.util.List;
public interface CategoryService {
public List<Category> selectAll();
}
代码说明:
- interface:定义接口
- selectAll:定义获取图书分类的方法
3.定义mapper接口类CategoryMapper
package com.imooc.reader.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.Category;
public interface CategoryMapper extends BaseMapper<Category> {
}
代码说明:
- BaseMapper:继承BaseMapper
- BaseMapper<Category>:范型Category
4.定义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.imooc.reader.mapper.CategoryMapper">
</mapper>
代码说明:这个xml文件用来写相关的sql的增删改查语句
5.新增impl实现包并且新增categoryServiceImpl
package com.imooc.reader.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Category;
import com.imooc.reader.mapper.CategoryMapper;
import com.imooc.reader.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("categoryService")
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class CategoryServiceImpl implements CategoryService {
@Resource
CategoryMapper categoryMapper;
@Override
public List<Category> selectAll() {
List<Category> list = categoryMapper.selectList(new QueryWrapper<Category>());
return list;
}
}
代码说明
- @Service("categoryService"):声明service
- @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true):不开启事物,只读,因为我们只是做查询操作
- implements CategoryService:实现CategoryService
- selectAll:实现方法
- categoryMapper.selectList(new QueryWrapper<Category>()):因为我们不需要查询条件,所以是空查询
6.新增测试用例
查询出对应的数据