今天的任务是在昨天的开发基础上实现新闻栏目和tag的增删改查, 下面将给出关键代码
model层
Tag.java
package com.psychedelicghost.po;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
@Entity
@Table(name="t_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层
TagRepository.java
package com.psychedelicghost.dao;
import com.psychedelicghost.po.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TagRepository extends JpaRepository<Tag, Long> {
Tag findByName(String name);
}
service层
TagService 接口文件
package com.psychedelicghost.service;
import com.psychedelicghost.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 updateTag(Long id, Tag tag);
}
TagServiceImpl.java 实现文件
package com.psychedelicghost.service.impl;
import com.psychedelicghost.dao.TagRepository;
import com.psychedelicghost.po.Tag;
import com.psychedelicghost.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@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