基于SpringBoot框架的新闻后台管理系统02
新闻后台管理系统2.0
本次项目内容
- 本次项目开发是在新闻后台管理1.0的基础上进行的
- 本次开发主要是实现新闻类型和标签的新增、删除、修改功能
项目开发过程
- 在TypeRepository类中添加方法,以实现Type需要的查询方法
public interface TypeRepository extends JpaRepository<Type, Long> {
Type findByName(String name);
}
- 在TypeService接口中添加方法,以实现Type的新增等服务
public interface TypeService {
Page<Type> listType(Pageable pageable);
Type saveType(Type type);
Type getTypeByName(String name);
void deleteType(Long id);
Type getType(Long id);
Type updateType(Long id, Type type);
}
- 在TypeServiceImpl类中实现TypeService接口中新增的方法
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository typeRepository;
@Override
public Page<Type> listType(Pageable pageable) {
return typeRepository.findAll(pageable);
}
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
@Override
public void deleteType(Long id) {
typeRepository.deleteById(id);
}
@Override
public Type getType(Long id) {
return typeRepository.findById(id).orElse(null);
}
@Override
public Type updateType(Long id, Type type) {
Type type1 = typeRepository.findById(id).orElse(null);
if(type1 == null){
System.out.println("未获取更新对象");
return null;
}
BeanUtils.copyProperties(type, type1);
return typeRepository.save(type1);
}
}
- 在TypeController类中添加方法,以实现Type的新增等功能
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
@RequestMapping("/types")
public String types(@PageableDefault(size = 5,sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable, Model model){
model.addAttribute("page", typeService.listType(pageable));
return "admin/types";
}
@GetMapping("/types/input")
public String input (Model model){
model.addAttribute("type", new Type());
return "admin/types-input";
}
@PostMapping("/types/add")
public String add(@Valid Type type, BindingResult result, RedirectAttributes attributes){
Type type1 = typeService.getTypeByName(type.getName());
if(type1 != null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if(result.hasErrors()){
return "admin/types-input";
}
Type type2 = typeService.saveType(type);
if(type2 == null){
attributes.addFlashAttribute("message", "新增失败");
}else{
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/types";
}
@RequestMapping("/types/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
typeService.deleteType(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
@RequestMapping("/types/{id}/toUpdate")
public String toUpdate(@PathVariable Long id, Model model){
model.addAttribute("type", typeService.getType(id));
return "admin/types-input";
}
@RequestMapping("/types/update/{id}")
public String update(@Valid Type type, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
Type type1 = typeService.getTypeByName(type.getName());
if(type1 != null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if(result.hasErrors()){
return "admin/types-input";
}
Type type2 = typeService.updateType(id, type);
if(type2 != null){
attributes.addFlashAttribute("message", "更新成功");
}else{
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/types";
}
}
-
通过以上操作,便实现了Type的新增、删除、修改功能。接下来我们将继续实现Tag的添加、删除、修改功能,我在这里推荐读者可以自己动手,模仿着Type的相关代码来实现Tag的功能,想不起了再看以下过程。
-
在po包创建Tag类
@Entity
@Table(name="tag")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;
public Tag() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Tag{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
- 在dao包下创建TagRepositoty接口
public interface TagRepository extends JpaRepository<Tag, Long> {
Tag findByName(String name);
}
- 在Service包下创建TagService接口
public interface TagService {
Page<Tag> listTag(Pageable pageable);
Tag saveTag(Tag tag);
void deleteTag(Long id);
Tag getTagByName(String name);
Tag getTag(Long id);
Tag updateTag(Long id, Tag tag);
}
- 在impl包下创建TagServiceImpl类并实现TagService接口
@Service
public class TagServiceImpl implements TagService {
@Autowired
private TagRepository tagRepository;
@Override
public Page<Tag> listTag(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Override
public Tag saveTag(Tag tag) {
return tagRepository.save(tag);
}
@Override
public void deleteTag(Long id) {
tagRepository.deleteById(id);
}
@Override
public Tag getTagByName(String name) {
return tagRepository.findByName(name);
}
@Override
public Tag getTag(Long id) {
return tagRepository.findById(id).orElse(null);
}
@Override
public Tag updateTag(Long id, Tag tag) {
Tag tag1 = tagRepository.findById(id).orElse(null);
if(tag == null){
System.out.println("未获取更新对象");
return null;
}
BeanUtils.copyProperties(tag, tag1);
return tagRepository.save(tag1);
}
}
- 在admin包下创建TagController类
@Controller
@RequestMapping("/admin")
public class TagController {
@Autowired
private TagService tagService;
@RequestMapping("/tags")
public String tags(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable, Model model){
model.addAttribute("page", tagService.listTag(pageable));
return "admin/tags";
}
@GetMapping("/tags/input")
public String input(Model model){
model.addAttribute("tag", new Tag());
return "admin/tags-input";
}
@PostMapping("/tags/add")
public String add(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
Tag tag1 = tagService.getTagByName(tag.getName());
if(tag1 != null){
result.rejectValue("name","nameError","不能添加重复的标签");
}
if(result.hasErrors()){
return "admin/tags-input";
}
Tag tag2 = tagService.saveTag(tag);
if(tag2 == null){
attributes.addFlashAttribute("message", "新增失败");
}else{
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
tagService.deleteTag(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/toUpdate")
public String toUpdate(@PathVariable Long id, Model model){
model.addAttribute("tag", tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
Tag tag1 = tagService.getTagByName(tag.getName());
if(tag1 != null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if(result.hasErrors()){
return "admin/tags-input";
}
Tag tag2 = tagService.updateTag(id, tag);
if(tag2 != null){
attributes.addFlashAttribute("message", "更新成功");
}else{
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/tags";
}
}
- 运行程序,并打开浏览器并在网址栏内输入"localhost:8081/admin",请将这里的8081替换你使用的端口号。因为SpringBoot项目使用的是内置的Tomcat服务器,它不会自动给我们打开后台页面,所以需要我们手动输入网址。
项目演示
- Type新增、修改页面
- Tag管理页面
- Tag新增、修改页面
项目下载
链接:news2