概述
前言
这几天轮播图想用mongdb开发,然后就有了一下代码
效果图
源码如下
package jkw.pojo;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* 轮播图
*/
@Data
@Document("banner")
public class Banner {
@Id
private String _id;//mongdb的主键
private Integer bannerId;//自己定义一个主键,用于删改查,记得添加时要随机生成主键,保证唯一性
private String bannerName;//轮播图名称
private String bannerUrl;//地址
}
package jkw.service;
import jkw.pojo.Banner;
import jkw.vo.PageResult;
import org.springframework.data.domain.Page;
import java.util.List;
public interface BannerService {
void add(Banner banner);
void update(Banner banner);
void delete(Integer id);
Banner findById(Integer id);
PageResult search(int page, int size);
List<Banner> findAll();
}
package jkw.service.impl;
import jkw.pojo.Banner;
import jkw.service.BannerService;
import jkw.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BannerServiceImpl implements BannerService {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void add(Banner banner) {
mongoTemplate.save(banner);
}
@Override
public void update(Banner banner) {
//org.springframework.data.mongodb.core.query.Query/Criteria/Update
Query query = new Query();
Criteria criteria = new Criteria();
// 设置查询条件
criteria.and("bannerId").is(banner.getBannerId());
query.addCriteria(criteria);
//创建Update,修改字段 例如把title修改为params.getTitle()
Update update = new Update().set("bannerName", banner.getBannerName()).set("bannerUrl", banner.getBannerUrl());
//执行修改操作,返回修改成功的条数
mongoTemplate.updateMulti(query, update, Banner.class);
}
@Override
public void delete(Integer id) {
//org.springframework.data.mongodb.core.query.Query/Criteria
Query query = new Query();
Criteria criteria = new Criteria();
// 设置查询条件
criteria.and("bannerId").is(id);
query.addCriteria(criteria);
mongoTemplate.remove(query, Banner.class);
}
@Override
public Banner findById(Integer id) {
//org.springframework.data.mongodb.core.query.Query/Criteria
Query query = new Query();
Criteria criteria = new Criteria();
// 设置查询条件
criteria.and("bannerId").is(id);
query.addCriteria(criteria);
return mongoTemplate.findOne(query, Banner.class);
}
@Override
public PageResult search(int page, int size) {
// 查询总数
long totals = mongoTemplate.count(new Query(), Banner.class);
//查询全部数据
List<Banner> banners = mongoTemplate.findAll(Banner.class);
// 分页查询
PageResult<Banner> bannerPage = new PageResult<>();
bannerPage.setRecords(banners);
bannerPage.setTotal(totals);
bannerPage.setSize(size);
bannerPage.setCurrent(page);
return bannerPage;
}
@Override
public List<Banner> findAll() {
return mongoTemplate.findAll(Banner.class);
}
}
package jkw.controller;
import jkw.pojo.Banner;
import jkw.service.BannerService;
import jkw.vo.BaseResult;
import jkw.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 轮播图控制器
*/
@RestController
@RequestMapping("/website/banner")
@CrossOrigin
public class BannerController {
@Autowired
private BannerService bannerService;
/**
* 新增
*
* @param banner
* @return
*/
@PostMapping("/add")
public BaseResult add(Banner banner) {
Integer radom=(int)(Math.random() * 10000 + 1);
banner.setBannerId(radom);
bannerService.add(banner);
return BaseResult.ok();
}
/**
* 修改
*
* @param banner
* @return
*/
@PostMapping("/update")
public BaseResult update(Banner banner) {
System.out.println(banner);
bannerService.update(banner);
return BaseResult.ok();
}
/**
* 根据bannerId删除
*
* @param
* @return
*/
@DeleteMapping("/delete")
public BaseResult delete(Integer bannerId) {
bannerService.delete(bannerId);
return BaseResult.ok();
}
/**
* 根据bannerId查询
*
* @param bannerId
* @return
*/
@GetMapping("/findById")
public BaseResult findById(Integer bannerId) {
Banner banner = bannerService.findById(bannerId);
return BaseResult.ok(banner);
}
/**
* 分页查询
*
* @param page
* @param size
* @return
*/
@GetMapping("/search")
public BaseResult search(int page, int size) {
PageResult<Banner> bannerPage = bannerService.search(page, size);
return BaseResult.ok(bannerPage);
}
}