分类管理
本次的内容为博客系统的分类关系部分,包括标签和类型。
针对其的增删改查
首先是dao层:
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
}
service:
public interface TypeService {
Type saveType(Type type);
Type getType(Long id);
Page<Type> listType(Pageable pageable);
Type updateType(Long id, Type type);
void deleteType(Long id);
Type getTypeByName(String name);
}
serviceImpl:
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository repository;
/**
* 保存(添加)
* @param type
* @return
*/
@Transactional
@Override
public Type saveType(Type type) {
return repository.save(type);
}
/**
* 根据id查询
* @param id
* @return
*/
@Transactional
@Override
public Type getType(Long id) {
return repository.getOne(id);
}
/**
* 分页查询全部
* @param pageable
* @return
*/
@Transactional
@Override
public Page<Type> listType(Pageable pageable) {
return repository.findAll(pageable);
}
/**
* 更新
* @param id
* @param type
* @return
*/
@Transactional
@Override
public Type updateType(Long id, Type type) {
Type t = repository.getOne(id);
if (t==null){
throw new NotFoundException("不存在该类型");
}
BeanUtils.copyProperties(type,t);
return repository.save(t);
}
/**
* 删除
* @param id
*/
@Transactional
@Override
public void deleteType(Long id) {
repository.deleteById(id);
}
/**
* 根据类型名称查询(用来查重)
* @param name
* @return
*/
@Override
public Type getTypeByName(String name) {
return repository.findByName(name);
}
}
controller:
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService service;
@GetMapping("/types")
// 分页查询每夜三条、根据id倒序。
public String list(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
// 将page对象传给前端界面
model.addAttribute("page",service.listType(pageable));
return "admin/types";
}
@GetMapping("/types/input")
public String input(Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@GetMapping("/types/{id}/input")
public String editInput(@PathVariable Long id, Model model) {
model.addAttribute("type", service.getType(id));
return "admin/types-input";
}
@PostMapping("/types")
public String post(@Valid Type type, BindingResult result, RedirectAttributes attributes){
// 查重
Type type1 = service.getTypeByName(type.getName());
if (type1 != null) {
result.rejectValue("name","nameError","不能添加重复的分类");
}
if (result.hasErrors()){
return "admin/types-input";
}
Type t = service.saveType(type);
if (t == null){
attributes.addFlashAttribute("message","新增失败");
}else {
attributes.addFlashAttribute("message", "新增成功");
}
return "redirect:/admin/types";
}
@PostMapping("/types/{id}")
public String editPost(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes attributes) {
Type type1 = service.getTypeByName(type.getName());
if (type1 != null) {
result.rejectValue("name","nameError","不能添加重复的分类");
}
if (result.hasErrors()) {
return "admin/types-input";
}
Type t = service.updateType(id,type);
if (t == null ) {
attributes.addFlashAttribute("message", "更新失败");
} else {
attributes.addFlashAttribute("message", "更新成功");
}
return "redirect:/admin/types";
}
@GetMapping("/types/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes){
service.deleteType(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
}
下面是thymleaf获取值以及传值的方式:
将查询到的类型名称以分页的形式传到界面
<tbody>
<tr th:each="type,iterStat : ${page.content}">
<td th:text="${iterStat.count}">序号</td>
<td th:text="${type.name}">类型名称</td>
<td>
<a href="#" th:href="@{/admin/types/{id}/input(id=${type.id})}" class="ui mini teal basic button">编辑</a>
<a href="#" th:href="@{/admin/types/{id}/delete(id=${type.id})}" class="ui mini red basic button">删除</a>
</td>
</tr>
</tbody>
判断是否操作成功:
<div class="ui success message" th:unless="@{#strings.isEmpty(message)}">
<i class="close icon"></i>
<div class="header">提示:</div>
<p th:text="${message}">恭喜操作成功!</p>
</div>
这里是判断是否有错误发生;再提交存储的时候可以使用双层判断来使得存入的东西不能为空。
前端是一成;第二段可以再创建数据库的文件中来使用@NotBlank(message = "标签名称不能为空") private String name;
来声明不能为空;如果为空时可以
<!--/*/
<div class="ui negative message" th:if="${#fields.hasErrors('name')}" >
<i class="close icon"></i>
<div class="header">验证失败</div>
<p th:errors="*{name}">提交信息不符合规则</p>
</div>
/*/-->
感谢B站的视频,总结自B站视频,如有错误,感谢指证。