数据库

攻略分类表

CREATE TABLE `strategy_catalog` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`dest_id` bigint DEFAULT NULL,
`state` int DEFAULT NULL,
`seq` int DEFAULT NULL,
`dest_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC;

实体类

找到模块:trip-article-api,找到包com.swx.article.domain,创建攻略分类实体类

StrategyCatalog
@Getter
@Setter
@TableName("strategy_catalog")
public class StrategyCatalog {
public static final int STATE_NORMAL = 1;
public static final int STATE_DISABLE = 0;

@TableId(type = IdType.AUTO)
private Long id;
private String name; // 类别名称
private Long destId; // 目的地
private String destName; // 目的地名称
private Integer state = STATE_NORMAL; // 类别状态
private Integer seq; // 类别序号

@TableField(exist = false)
private List<Strategy> strategies = new ArrayList<>();
}

基础服务

分页查询

接口信息

路径地址 http://localhost:9000/article/strategies/catalogs/query
请求方式 GET
请求参数 Page
返回结果 Page

Controller

StrategyCatalogController
@RestController
@RequestMapping("/strategies/catalogs")
public class StrategyCatalogController {

private final StrategyCatalogService strategyCatalogService;

public StrategyCatalogController(StrategyCatalogService strategyCatalogService) {
this.strategyCatalogService = strategyCatalogService;
}

@GetMapping("/query")
public R<Page<StrategyCatalog>> pageList(Page<StrategyCatalog> page) {
return R.ok(strategyCatalogService.page(page));
}
}

主键查询

接口信息

路径地址 http://localhost:9000/article/strategies/catalogs/detail
请求方式 GET
请求参数 id
返回结果 StrategyCatalog

Controller

StrategyCatalogController
@GetMapping("/detail")
public R<StrategyCatalog> getById(Long id) {
return R.ok(strategyCatalogService.getById(id));
}

保存攻略类型

在保存攻略类型时,需要填充其对应目的地的名称

接口信息

路径地址 http://localhost:9000/article/strategies/catalogs/save
请求方式 POST
请求参数 StrategyCatalog
返回结果

Controller

StrategyCatalogController
@RestController
@RequestMapping("/strategies/catalogs")
public class StrategyCatalogController {

private final StrategyCatalogService strategyCatalogService;
private final DestinationService destinationService;

public StrategyCatalogController(StrategyCatalogService strategyCatalogService, DestinationService destinationService) {
this.strategyCatalogService = strategyCatalogService;
this.destinationService = destinationService;
}

@PostMapping("/save")
public R<?> save(StrategyCatalog strategyCatalog) {
Destination destination = destinationService.getById(strategyCatalog.getDestId());
strategyCatalog.setDestName(destination.getName());
strategyCatalogService.save(strategyCatalog);
return R.ok();
}
}

更新攻略分类

接口信息

路径地址 http://localhost:9000/article/strategies/catalogs/update
请求方式 POST
请求参数 StrategyCatalog
返回结果

Controller

StrategyCatalogController
@PostMapping("/update")
public R<?> update(StrategyCatalog strategyCatalog) {
strategyCatalogService.updateById(strategyCatalog);
return R.ok();
}

删除攻略分类

接口信息

路径地址 http://localhost:9000/article/strategies/catalogs/delete/{id}
请求方式 POST
请求参数 id
返回结果

Controller

StrategyCatalogController
@PostMapping("/delete/{id}")
public R<?> delete(@PathVariable Long id) {
strategyCatalogService.removeById(id);
return R.ok();
}

分组查询类别

根据目的地分组查询类别

对应的前端页面如下:每个目的地下面都有对应的攻略类别

接口信息

路径地址 http://localhost:9000/article/strategies/catalogs/groups
请求方式 GET
请求参数 id
返回结果

Mapper

找到:StrategyCatalogMapper,定义分组查询方法

StrategyCatalogMapper
public interface StrategyCatalogMapper extends BaseMapper<StrategyCatalog> {
List<StrategyCatalogGroup> selectGroupList();
}

找到:StrategyCatalogMapper.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.swx.article.mapper.StrategyCatalogMapper">

<resultMap id="BaseResultMap" type="com.swx.article.domain.StrategyCatalog">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="destId" column="dest_id" />
<result property="destName" column="dest_name" />
<result property="seq" column="seq" />
<result property="state" column="state" />
</resultMap>

<resultMap id="CatalogGroupMap" type="com.swx.article.vo.StrategyCatalogGroup">
<id property="destId" column="dest_id" />
<result property="destName" column="dest_name" />
<collection property="catalogList" resultMap="BaseResultMap" columnPrefix="sc_">

</collection>
</resultMap>

<select id="selectGroupList" resultMap="CatalogGroupMap">
SELECT t.dest_id,
t.dest_name,
sc.id sc_id,
sc.name sc_name,
sc.seq sc_seq,
sc.state sc_state
FROM (SELECT dest_id, dest_name
FROM strategy_catalog
GROUP BY dest_id, dest_name) t
JOIN strategy_catalog sc ON t.dest_id = sc.dest_id
WHERE sc.state = 0
</select>
</mapper>

Service

找到:StrategyCatalogService,定义分组查询方法

public interface StrategyCatalogService extends IService<StrategyCatalog> {
/**
* 根据目的地分组查询类别
* @return 所有目的地类别分组
*/
List<StrategyCatalogGroup> findGroupList();
}

找到:StrategyCatalogServiceImpl,实现上述方法

StrategyCatalogServiceImpl
@Service
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper, StrategyCatalog> implements StrategyCatalogService {

/**
* 根据目的地分组查询类别
*
* @return 所有目的地类别分组
*/
@Override
public List<StrategyCatalogGroup> findGroupList() {
return baseMapper.selectGroupList();
}
}

Controller

StrategyCatalogController
@PostMapping("/delete/{id}")
public R<?> delete(@PathVariable Long id) {
strategyCatalogService.removeById(id);
return R.ok();
}