1)、 按照条件查询标签:
① 在controller种添加方法
【确保表中有数据】
/**
* 根据条件查询
*/
@PostMapping("/search")
public Result findSearch(@RequestBody Label label){
List<Label> list = service.findSearch(label);
return new Result(true,StatusCode.OK,"查询成功",list);
}
② 在service 中完善方法
/**
* 根据条件查询标签
*/
public List<Label> findSearch(Label label) {
//用到了dao的findAll()的重载方法 传入的参数是Specification的对象,写一个匿名内部类
return labelDao.findAll(new Specification<Label>() {
/**
*
* @param root 根对象,也就是把哪个对象封装到条件中 where label.id = id
* @param criteriaQuery 封装的都是查询关键字 例如 group by order by
* @param cb 用来封装条件对象的
* @return
*/
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
ArrayList list = new ArrayList();
if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
Predicate predicate = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");//where labelname like "小明"
list.add(predicate);
}
if (label.getId() != null && !"".equals(label.getId())) {
Predicate predicate = cb.equal(root.get("id").as(String.class), label.getId());
list.add(predicate);
}
Predicate[] pre = new Predicate[list.size()];
list.toArray(pre);
return cb.and(pre);
}
});
}
③ 在API中进行测试
# 根据A模糊查询
{
"labelname": "A"
}
返回结果:
{
"flag": true,
"code": 20000,
"message": "查询成功",
"data": [
{
"id": "1098215343807037440",
"labelname": "JAVA",
"state": "1",
"count": 0,
"fans": null,
"recommend": "1"
}
]
}
2)、根据含分页条件查询标签
①contrller添加方法,注意请求方式及路径
【确保表中有数据】
/**
* 根据条件查询 含分页信息
*/
@PostMapping("/search/{page}/{size}")
public Result queryPage(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
Page pageData = service.queryPage(label,page,size);
return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageData.getTotalElements(),pageData.getContent()))
}
② 完善service层的方法
/**
* 含有分页条件的查询
* 使用到了Pageable这一工具
* 注意page-1
*/
public Page queryPage(Label label, int page, int size) {
Pageable pageable = PageRequest.of(page-1,size);
return labelDao.findAll(new Specification<Label>() {
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
ArrayList list = new ArrayList();
if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
Predicate predicate = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");//where labelname like "小明"
list.add(predicate);
}
if (label.getState() != null && !"".equals(label.getState())) {
Predicate predicate = cb.equal(root.get("state").as(String.class), label.getState());//where labelname like "小明"
list.add(predicate);
}
Predicate[] pre = new Predicate[list.size()];
list.toArray(pre);
return cb.and(pre);
}
},pageable);
}
③ 在API中进行测试
页码:1
size:2
{
"labelname": "H"
}
#测试结果:
{
"flag": true,
"code": 20000,
"message": "查询成功",
"data": {
"total": 2,
"rows": [
{
"id": "1098215421804314624",
"labelname": "PHP",
"state": "2",
"count": 0,
"fans": null,
"recommend": "1"
},
{
"id": "1098227211468578816",
"labelname": "PYTHON",
"state": "2",
"count": 0,
"fans": null,
"recommend": "1"
}
]
}
}
3)、招聘微服务
3.1)、热门企业列表
需求分析:查询企业表ishot字段为1的记录 【代码较为简单,三层如下】
controller层:
@GetMapping("/search/hotlist")
public Result findHotEnterprise(){
return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findHotEnterprise());
}
service层:
/**
* 查询热门企业
*/
public List<Enterprise> findHotEnterprise(){
return enterpriseDao.findAllByIshot("1");
}
dao层:
public List<Enterprise> findAllByIshot(String ishot);
//省略测试部分
3.2)、推荐职位列表
需求分析:查询状态为2并以创建日期降序排序,查询前4条记录
//基础代码:三和一
dao层:
/**
* 热门职位:查询状态码为2 的
*/
public List<Recruit> findTop6ByStateOrderByCreatetime(String state);
//where state= ? order by creatime
service层:
/**
* 查询热门职位
*/
public List<Recruit> hotJob(){
return recruitDao.findTop6ByStateOrderByCreatetime("2");
}
controller:
/**
* 查询热门职位
*/
@GetMapping("/search/recommend")
public Result hotJob(){
return new Result(true,StatusCode.OK,"查询成功",recruitService.hotJob());
}
3.3)、最新职位列表
需求分析:查询状态不为0并以创建日期降序排序,查询前12条记录
//基础代码,三合一:
dao层:
/**
* 最新职位:
* 查询状态码不为0 的 按照日期排序
*/
public List<Recruit> findTop6ByStateNotOrderByCreatetime(String state);
//where state != ? order by creatime
service层:
/**
* 查询最新职位
*/
public List<Recruit> newJob(){
return recruitDao.findTop6ByStateNotOrderByCreatetime("0");
}
controller层:
/**
* 查询最新职位
*/
@GetMapping("/search/newlist")
public Result newJob(){
return new Result(true,StatusCode.OK,"查询成功",recruitService.newJob());
}
4)、问答微服务
4.1)、最新回答列表
4.2)、热门回答列表
4.3)、等待回答列表
dao层:注意返回对象是Page
/**
* 最新回答
*/
@Query(value = "SELECT * FROM tb_problem,tb_pl WHERE id = labelid AND labelid=? ORDER BY replytime DESC",nativeQuery = true)
public Page<Problem> newList(String labelid, Pageable pageable);
/**
* 热门回答
*/
@Query(value = "SELECT * FROM tb_problem,tb_pl WHERE id = labelid AND labelid=? ORDER BY reply DESC",nativeQuery = true)
public Page<Problem> hotList(String labelid, Pageable pageable);
/**
* 等待回答
*/
@Query(value = "SELECT * FROM tb_problem,tb_pl WHERE id = labelid AND labelid=? AND reply=0 ORDER BY createtime DESC",nativeQuery = true)
public Page<Problem> waitList(String labelid, Pageable pageable);
service层:
public Page<Problem> newList(String labelid, Integer page,Integer size){
Pageable pageable = PageRequest.of(page-1,size);
return problemDao.newList(labelid,pageable);
}
public Page<Problem> hotList(String labelid, Integer page,Integer size){
Pageable pageable = PageRequest.of(page-1,size);
return problemDao.hotList(labelid,pageable);
}
public Page<Problem> waitList(String labelid, Integer page,Integer size){
Pageable pageable = PageRequest.of(page-1,size);
return problemDao.waitList(labelid,pageable);
}
controller层:
@GetMapping("/newlist/{label}/{page}/{size}")
public Result newList( @PathVariable String label,@PathVariable Integer page,@PathVariable Integer size){
Page<Problem> newList = problemService.newList(label, page, size);
return new Result(true,StatusCode.OK,"查询成功",newList);
}
@GetMapping("/hotlist/{label}/{page}/{size}")
public Result hotList(@PathVariable String label,@PathVariable Integer page,@PathVariable Integer size){
Page<Problem> hotList = problemService.hotList(label, page, size);
return new Result(true,StatusCode.OK,"查询成功",hotList);
}
@GetMapping("/waitlist/{label}/{page}/{size}")
public Result waitList(@PathVariable String label,@PathVariable Integer page, @PathVariable Integer size){
Page<Problem> waitList = problemService.waitList(label, page, size);
return new Result(true,StatusCode.OK,"查询成功",waitList);
}
5)、文章微服务
5.1)、审核功能实现
5.2)、点赞功能实现
dao层:
//修改表的内容,注意在方法前加上Modifying注解
@Modifying
@Query(value = "UPDATE `tb_article` SET state=1 WHERE id = ?",nativeQuery = true)
public void updateState(String id);
@Modifying
@Query(value = "UPDATE `tb_article` SET thumbup=thumbup+1 WHERE id = ?",nativeQuery = true)
public void thumbUp(String id);
service层:
//注意事务的控制。加上Transactional注解
public void updateState(String id){
articleDao.updateState(id);
}
public void thumbUp(String id){
articleDao.thumbUp(id);
}
6)、缓存的处理
6.1)、使用Redis实现
① 开启一个Redis容器
docker run -di --name=tensquare_redis -p 6379:6379 redis
②导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
③service层代码实现
@Autowired
private RedisTemplate redisTemplate;
//查询:第一次查到null,放进缓存,以后从缓存中取得
public Article findById(String id) {
Article article = (Article) redisTemplate.opsForValue().get("article_" + id);
if (article==null){
article = articleDao.findById(id).get();
redisTemplate.opsForValue().set("article_"+id,article);
}
return article ;
}
/**
* 修改:将原有的缓存删除
* @param article
*/
public void update(Article article) {
redisTemplate.delete("article_"+article.getId());
articleDao.save(article);
}
/**
* 删除:将原有的缓存删除
* @param id
*/
public void deleteById(String id) {
redisTemplate.delete("article_"+id);
articleDao.deleteById(id);
}
④Redis设置过期时间
redisTemplate.opsForValue().set("article_"+id,article,10, TimeUnit.DAYS);
6.2)、使用Spring Cache实现
在活动微服务中实现
①开启缓存机制
//在application上添加注解:表示开启缓存
@EnableCaching
public class GatheringApplication {
public static void main(String[] args) {
SpringApplication.run(GatheringApplication.class, args);
}
②在service层中的方法上添加注解使用缓存
//添加缓存
@Cacheable(value = "gathering",key = "#id")
public Gathering findById(String id) {
return gatheringDao.findById(id).get();
}
//删除缓存
@CacheEvict(value = "gathering",key = "#gathering.id")
public void update(Gathering gathering) {
gatheringDao.save(gathering);
}