发表于: 2020-07-11 18:44:10
1 1185
编写controller
写了四个
@Controller
@RequestMapping("/banner")
public class BannerController {
@Autowired
BannerService bannerService;
private static final Logger log= LogManager.getLogger(BannerController.class);
@RequestMapping(value = "/toBannerIndex")
public String findBanner(Banner banner) {
return "bannerIndex";
}
/**
* 添加banner
* @param banner
* @param modelAndView
* @return
*/
@RequestMapping(value ="addBanner",method = RequestMethod.POST)
public ModelAndView addBanner(Banner banner, ModelAndView modelAndView){
log.info(banner);
if (bannerService.insert(banner)){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","添加成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
/**
* 删除banner
* @param id
* @return
*/
@RequestMapping(value = "/deleteBanner/{id}",method = RequestMethod.DELETE)
public ModelAndView deleteBanner(@PathVariable Long id, ModelAndView modelAndView){
if(bannerService.deleteByPrimaryKey(id)){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","更新成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
/**
* 更改banner
* @param banner
* @param modelAndView
* @return
*/
@RequestMapping(value = "updateBanner/id={id}",method = RequestMethod.PUT)
public ModelAndView updateBanner(Banner banner,@PathVariable long id,ModelAndView modelAndView){
if(bannerService.updateByPrimaryKey(banner)){
log.info(banner.getImg());
log.info(banner.getUrl());
modelAndView.addObject("code",200);
modelAndView.addObject("msg","更新成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
/**
* 查询单条banner
* @param id
* @param modelAndView
* @return
*/
@RequestMapping(value = "/selectBanner/id={id}",method = RequestMethod.POST)
public ModelAndView getBanner(@PathVariable Long id, ModelAndView modelAndView){
Banner banner=bannerService.selectByPrimaryKey(id);
if (banner!=null){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","查询成功");
modelAndView.addObject("banner",banner);
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("bannerJson1");
return modelAndView;
}
/**
* 查询所有banner列表
* @param modelAndView
* @return
*/
@RequestMapping(value = "/bannerList",method = RequestMethod.GET)
public ModelAndView getBannerList(ModelAndView modelAndView){
List<Banner> bannerList=bannerService.selectAllBanner();
log.info(bannerList);
if (bannerList!=null){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","查询成功");
modelAndView.addObject("bannerList",bannerList);
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("bannerJson2");
return modelAndView;
}
/**
* 设为上架
* @param banner
* @param modelAndView
* @return
*/
@RequestMapping(value = "updateBannerSetting/id={id}",method = RequestMethod.PUT)
public ModelAndView updateBannerSetting(Banner banner,@PathVariable long id,ModelAndView modelAndView){
if(bannerService.updateByPrimaryKeyForStatus(banner)){
banner.setStatus(0);
modelAndView.addObject("code",200);
modelAndView.addObject("msg","上架成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
/**
* 设为下架
* @param banner
* @param modelAndView
* @return
*/
@RequestMapping(value = "updateBannerCancel/id={id}",method = RequestMethod.PUT)
public ModelAndView updateBannerCancel(Banner banner,@PathVariable long id,ModelAndView modelAndView){
if(bannerService.updateByPrimaryKeyForStatus(banner)){
banner.setStatus(1);
modelAndView.addObject("code",200);
modelAndView.addObject("msg","下架成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
/**
* 查询作品集列表(默认上架,在前台展示,上架status=0,下架status=1)
* @param modelAndView
* @return
*/
@RequestMapping(value = "/frontDeskList",method = RequestMethod.GET)
public ModelAndView getBannerListFrontDesk(ModelAndView modelAndView){
List<Banner> bannerList=bannerService.selectAllBannerFrontDesk();
log.info(bannerList);
if (bannerList!=null){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","查询成功(前台)");
modelAndView.addObject("bannerList",bannerList);
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("bannerJson2");
return modelAndView;
}
/**
* 查询作品集列表(后台展示所有作品集列表)
* @param modelAndView
* @return
*/
@RequestMapping(value = "/backDeskList",method = RequestMethod.GET)
public ModelAndView getBannerListBackDesk(ModelAndView modelAndView){
List<Banner> bannerList=bannerService.selectAllBannerBackDesk();
log.info(bannerList);
if (bannerList!=null){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","查询成功(后台)");
modelAndView.addObject("bannerList",bannerList);
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("bannerJson2");
return modelAndView;
}
}
复习一下动态SQL
Mybatis的动态SQL if, choose, when, otherwise, trim, where, set, foreach标签,可组合成非常灵活的SQL语句
<select id="selectWorks" resultType="Works" parameterType="Works">
select * from works
where 1=1
<if test="name != null">
name like CONCAT('%', #{name}, '%')
</if>
<if test="state != null">
and state=#{state}
</if>
</select>
修改了部分接口文档 加了一个Workshop表
今日问题暂无
评论