一、持久层接口 pictureDao
@Mapper
@Repository
public interface PictureDao {
//查询照片
List<Picture> listPicture();
//添加图片
int savePicture(Picture picture);
//根据id查询照片
Picture getPicture(Long id);
//编辑修改相册
int updatePicture(Picture picture);
//删除照片
void deletePicture(Long id);
}
二、相册管理mapper
<?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.star.dao.PictureDao">
<!--查询所有照片-->
<select id="listPicture" resultType="com.star.entity.Picture">
select * from myblog.t_picture order by t_picture.id desc
</select>
<!--添加图片-->
<insert id="savePicture" parameterType="com.star.entity.Picture">
insert into myblog.t_picture (picturename,picturetime,pictureaddress,picturedescription)
values (#{picturename},#{picturetime},#{pictureaddress},#{picturedescription})
</insert>
<!--根据id查询照片-->
<select id="getPicture" resultType="com.star.entity.Picture">
select * from myblog.t_picture p where p.id = #{id}
</select>
<!--编辑修改相册-->
<update id="updatePicture" parameterType="com.star.entity.Picture">
update myblog.t_picture
set picturename = #{picturename}, picturetime = #{picturetime}, pictureaddress = #{pictureaddress}, picturedescription = #{picturedescription}
where id = #{id};
</update>
<!--删除照片-->
<delete id="deletePicture" >
delete from myblog.t_picture where id = #{id}
</delete>
</mapper>
三、业务层接口+实现类
public interface PictureService {
//查询照片
List<Picture> listPicture();
//添加图片
int savePicture(Picture picture);
//根据id查询照片
Picture getPicture(Long id);
//编辑修改相册
int updatePicture(Picture picture);
//删除照片
void deletePicture(Long id);
}
四、控制器
@Controller
@RequestMapping("/admin")
public class PictureController {
@Autowired
private PictureService pictureService;
// 查询照片列表
@GetMapping("/pictures")
public String pictures(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
PageHelper.startPage(pageNum,10);
List<Picture> listFriendLink = pictureService.listPicture();
PageInfo<Picture> pageInfo = new PageInfo<Picture>(listFriendLink);
model.addAttribute("pageInfo",pageInfo);
return "admin/pictures";
}
// 跳转新增页面
@GetMapping("/pictures/input")
public String input(Model model) {
model.addAttribute("picture", new Picture());
return "admin/pictures-input";
}
// 照片新增
@PostMapping("/pictures")
public String post(@Valid Picture picture, BindingResult result, RedirectAttributes attributes){
if(result.hasErrors()){
return "admin/pictures-input";
}
int P = pictureService.savePicture(picture);
if (P == 0 ) {
attributes.addFlashAttribute("message", "新增失败");
} else {
attributes.addFlashAttribute("message", "新增成功");
}
return "redirect:/admin/pictures";
}
// 跳转照片编辑页面
@GetMapping("/pictures/{id}/input")
public String editInput(@PathVariable Long id, Model model) {
model.addAttribute("picture", pictureService.getPicture(id));
return "admin/pictures-input";
}
// 编辑相册
@PostMapping("/pictures/{id}")
public String editPost(@Valid Picture picture, RedirectAttributes attributes) {
int P = pictureService.updatePicture(picture);
if (P == 0 ) {
attributes.addFlashAttribute("message", "编辑失败");
} else {
attributes.addFlashAttribute("message", "编辑成功");
}
return "redirect:/admin/pictures";
}
// 删除照片
@GetMapping("/pictures/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
pictureService.deletePicture(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/pictures";
}
}