DAY9 基于SpringBoot实现CRUD

分类管理
查询分类
查询分类之前开发分页功能的时候已经实现过了,回忆一下开发思路

1.新建dao目录下文件
因为很多方法jpa中已经自动给我们封装好了,所以相比之前的ssm项目来看,springboot方便许多,dao目录下新建的TypeRepository接口如下
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
}

2.新建service文件
在Service文件下新建两个接口方法,分别用来列举所有type和通过名字查询
Page listType(Pageable pageable);
Type getTypeByName(String name);
再在Impl文件中实现该方法
@Autowired
private TypeRepository typeRepository;

@Override
public Page listType(Pageable pageable) {
return typeRepository.findAll(pageable); //findAll方法来自jpa
}

@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}

3.实现controller
在controller中调用service中的方法,再连接上前端的路由
@Autowired
private TypeService typeService;

@RequestMapping("/types")
public String type(@PageableDefault(size = 3,sort = {“id”},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
model.addAttribute(“page”,typeService.listType(pageable));
return “admin/types”;
}

添加分类
因为在jpa中已经将插入数据的方法封装了,所以我们不需要再在dao文件下新建一个方法接口,直接调用jpa中的save方法即可

1.新建service接口、实现impl接口方法
在TypeService中声明一个saveType的接口方法
Type saveType(Type type);
在Impl中实现该方法
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
这里的save方法来自jpa

2.新增controller方法
思路为声明一个input方法将输入框中输入的内容放到一个model中的type传回前端,然后判端内容是否存在有id,无则跳转到添加方法,有即跳转到更新方法

    <div class="ui negative message" th:if="${#fields.hasErrors('name')}">
      <i class="close icon"></i>
      <p th:errors="*{name}">提交信息不符合规则</p>
    </div>
    <div class="ui right aligned container">
      <button type="button" class="ui button" onclick="window.history.go(-1)" >返回</button>
      <button class="ui teal submit button">提交</button>
    </div>

  </form>

当跳转到add时,将存入的数据传过来判断数据库中是否存在该名称的分类
@RequestMapping("/types/input")
public String input (Model model){
model.addAttribute(“type”,new Type());
return “admin/types-input”;
}

@RequestMapping("/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";

}
在这里插入图片描述
删除分类
删除方法在jpa中也有封装,所以直接从service开始

1.在Service中新建方法接口、在impl中实现
void delete(Long id);
@Override
public void delete(Long id) {
typeRepository.deleteById(id);
}

2.实现controller中方法
先在html中找到删除方法传回的路由路径
删除

再根据路由设置@RequestMapping

修改分类
修改的方法和之前的ssm类似,需要现在数据库中查到该条数据信息进行显示,再将修改后的信息传回数据库

1.新建Service接口方法并在impl中实现
//编辑
Type getType(Long id);
Type updateType(Long id,Type type);

用到的方法也都在jpa中封装有
@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);
}

2.实现controller
和之前新增类似,这里需要额外声明一个方法将查询到的type传到model中,这样再html中进行有无id判断时就会判断有id,进而跳转到更新界面
@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";

}

标签管理
标签管理的CRUD方法和分类管理是一样的,详细过程参考分类管理

添加实体类
在po目录下新建Tag实体类
Tag.java
@Entity
@Table(name = “t_tag”)
public class Tag {

@Id
@GeneratedValue(strategy =  GenerationType.IDENTITY)  //自增
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;

// @ManyToMany

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目录下新建一个TagRepository接口方法类、在Service下先建一个TagService接口方法类、在Service.impl下新建接口方法实现类TagServiceImpl.java、在web.admin下新建控制器TagController.java

Repository类
TagRepository.interface
public interface TagRepository extends JpaRepository<Tag,Long> {
Tag findByName(String name);
}

Service类和Impl类
TagService.interface
public interface TagService {

//查询
Page<Tag> listTag(Pageable pageable);

//新增
Tag saveTag(Tag tag);

Tag getTagByName(String name);

//删除
void deleteTag(Long id);

//编辑
Tag getTag(Long id);

Tag update(Long id,Tag tag);

}

TagServiceImpl.java
@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 Tag getTagByName(String name) {
    return tagRepository.findByName(name);
}

@Override
public void deleteTag(Long id) {
    tagRepository.deleteById(id);
}

@Override
public Tag getTag(Long id) {
    return tagRepository.findById(id).orElse(null);
}

@Override
public Tag update(Long id, Tag tag) {
    Tag tag1 = tagRepository.findById(id).orElse(null);
    if(tag1==null){
        System.out.println("获取更新对象出错");
        return null;
    }
    BeanUtils.copyProperties(tag,tag1);
    return tagRepository.save(tag1);
}

}

Controller类
TagController.java
@Controller
@RequestMapping("/admin")
public class TagController {

@Autowired
private TagService tagService;

@RequestMapping("/tags")
public String Tags(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
    model.addAttribute("page",tagService.listTag(pageable));
    return "admin/tags";
}

@RequestMapping("/tags/input")
public String input(Model model){
    model.addAttribute("tag",new Tag());
    return "admin/tags-input";
}

@RequestMapping("/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.update(id,tag);
    if(tag2!=null){
        attributes.addFlashAttribute("message","更新成功");
    }
    else {

        attributes.addFlashAttribute("message","更新失败");
    }
    return "redirect:/admin/tags";
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值