线上实习第八天回顾
今天是线上实习的第八天,任务是完成分类的增删改查功能以及对标签完成相应的增删改查功能。
源码展示
Type的相关源码
①dao.TypeRepository
package com.zr0726.news.dao;
import com.zr0726.news.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
}
②service.TypeService接口新增保存、根据名称获得类别、删除、根据id获得类别、更新类别的函数声明
Type saveType(Type type);
Type getTypeByName(String name);
void delete(Long id);
Type getType(Long id);
Type updateType(Long id,Type type);
③TypeServiceImpl类完成对这些函数的具体实现
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
@Override
public void delete(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类
@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","不能添加重复的标签");
System.out.println(result);
}
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){
System.out.println("id"+id);
model.addAttribute("tag",tagService.getTag(id));
System.out.println("根据id查得数据为"+tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
System.out.println("传入tag"+tag);
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";
}
⑤types-input.html也做了一些修改,方便提示出错误信息(line60-64)
<div class="ui error message"></div>
<div class="ui negative message" th:if="${#fields.hasErrors('name')}">
<i class="close icon"></i>
<p th:errors="*{name}">提交信息不符合规则</p>
</div>
Tag的相关源码
①po包下新建一个tag类实体
package com.zr0726.news.po;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
@Entity
@Table(name = "t_tag")
public class Tag {
@Id
@GeneratedValue
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;
public Tag() {
}
@Override
public String toString() {
return "Tags{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
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;
}
}
②dao包下新建TagRepository类
package com.zr0726.news.dao;
import com.zr0726.news.po.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TagRepository extends JpaRepository<Tag,Long> {
Tag findByName(String name);
}
③service包下创建TagService接口
package com.zr0726.news.service;
import com.zr0726.news.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);
void deleteTag(Long id);
Tag getTagByName(String name);
Tag getTag(Long id);
Tag updateTag(Long id,Tag tag);
}
④新建TagServiceImpl类实现该接口
package com.zr0726.news.service.impl;
import com.zr0726.news.dao.TagRepository;
import com.zr0726.news.po.Tag;
import com.zr0726.news.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.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(tag1==null){
System.out.println("未获得更新对象");
return null;
}
BeanUtils.copyProperties(tag,tag1);
return tagRepository.save(tag1);
}
}
⑤web.admin包内新建TagController类
package com.zr0726.news.web.admin;
import com.zr0726.news.po.Tag;
import com.zr0726.news.po.Type;
import com.zr0726.news.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
@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";
}
@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","不能添加重复的标签");
System.out.println(result);
}
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){
System.out.println("id"+id);
model.addAttribute("tag",tagService.getTag(id));
System.out.println("根据id查得数据为"+tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
System.out.println("传入tag"+tag);
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";
}
}
⑥tags-input.html也做了一些修改,方便提示出错误信息(line59-63)
<div class="ui error message"></div>
<div class="ui negative message" th:if="${#fields.hasErrors('name')}">
<i class="close icon"></i>
<p th:errors="*{name}">提交信息不符合规则</p>
</div>
项目运行结果
①新增分类
新增之前
新增之后,查看分类发现添加成功
若新增重复的分类或者新增空分类,会有相关错误提示
②编辑分类
修改之前新增的体育分类为篮球
查看发现修改成功
若修改后有重复分类或者空分类,会有相关错误提示,同上面新增操作的错误提示,此处不再展示。
③删除分类
删除之前新增的分类,发现删除成功
④查看标签
⑤新增标签足球
查看发现新增成功
若新增重复的标签或者新增空白标签,会有相关错误提示
⑥编辑新增的足球标签为欧冠标签
发现编辑成功
若修改后有重复标签或者空白标签,会有相关错误提示,同上面新增操作的错误提示,此处不再展示。
⑦删除新增的标签
一些知识点
使用注解的好处
使用注解的优势:
1.采用纯java代码,不在需要配置繁杂的xml文件
2.在配置中也可享受面向对象带来的好处
3.类型安全对重构可以提供良好的支持
4.减少复杂配置文件的同时亦能享受到springIoC容器提供的功能
Spring boot常见注解
@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:
@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。
@ResponseBody:表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。在使用@RequestMapping后,返回值通常解析为跳转路径,加上@esponsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@Responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用。
@Controller:用于定义控制器类,在spring项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。
@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集。
@RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射。
@EnableAutoConfiguration:SpringBoot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。
@ComponentScan:表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。
@Configuration:相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。
@Import:用来导入其他配置类。
@ImportResource:用来加载xml配置文件。
@Autowired:自动导入依赖的bean
@Service:一般用于修饰service层的组件
@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。
@Bean:用@Bean标注方法等价于XML中配置的bean。
@Value:注入Spring boot application.properties配置的属性的值。示例代码:
@Inject:等价于默认的@Autowired,只是没有required属性;
@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Bean:相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。
@AutoWired:自动导入依赖的bean。byType方式。把配置好的Bean拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。当加上(required=false)时,就算找不到bean也不报错。
@Qualifier:当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用。@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:
@Resource(name=”name”,type=”type”):没有括号内内容的话,默认byName。与@Autowired干类似的事。
SpringBoot注解最全详解(整合超详细版本)