springboot基础实操记录
了解一下三层架构及其作用:
Dao层(持久层):封装一些关于数据库操作在此,但是实际实现不在这。JPA/MyBatis
只要关于数据库操作都在这里定义,里面写的方法都交给service层调用,这样service完全不需要接触数据库,实现了高耦合。
Service层(业务层):具体实现都在这,增删改查等
一般是定义接口service,然后serviceImpl去实现这些方法。
Web(controller)层(表现层):前端页面的控制器:调用service的方法来实现业务
以博客项目的type为例:
先创建一个实体类type:
package com.chen.po;
import org.hibernate.validator.constraints.NotBlank;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity // @Entity :表明是一个实体类
@Table(name = "t_type") //@Table :对应的数据表名
public class Type {
@Id // @Id :主键
@GeneratedValue //@GeneratedValue:主键生成策略
private Long id;
@NotBlank(message = "分类名称不能为空")
private String name;
@OneToMany(mappedBy = "type")
private List<Blog> blogs = new ArrayList<>();
public Type() { //构造器
}
public Long getId() {
return id;
}
public void setId(Long id) { //生成get、set方法
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
@Override
public String toString() { //重写toString方法
return "Type{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Dao:typeRepository:
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
}
Service:typeService:
public interface TypeService {
Type saveType(Type type);
void deleteType(Long id);
Type updateType(Long id,Type type);
Type getType(Long id);
}
typeServiceImpl:
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository typeRepository;//注入后就可以使用repository封装好的方法
@Transactional //@Transactional 是声明式事务管理 编程中使用的注解 只能在service實現中使用
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
@Transactional
@Override
public void deleteType(Long id) {
typeRepository.deleteById(id);
}
@Transactional
@Override
public Type updateType(Long id, Type type) {
Type t = typeRepository.getOne(id);
if (t == null) {
throw new NotFoundException("不存在该类型");
}
BeanUtils.copyProperties(type,t);
return typeRepository.save(t);
}
@Transactional
@Override
public Type getType(Long id) {
return typeRepository.getById(id);
// return typeRepository.findOne(id);
}
}
Controller:typeController
package com.chen.web.admin;
import ...;
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService; //注入service
@GetMapping("/types") //pageable在jpa中用于分頁查詢
public String types(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model) {
model.addAttribute("page",typeService.listType(pageable)); //model.addattribute(K,V)往前台传数据,可以传对象,可以传List,通过el表达式 ${}可以获取到
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", typeService.getType(id));
return "admin/types-input";
}
//增加
@PostMapping("/types")
public String post(@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 t = typeService.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 = typeService.getTypeByName(type.getName());
if (type1 != null) {
result.rejectValue("name","nameError","不能添加重复的分类");
}
if (result.hasErrors()) {
return "admin/types-input";
}
Type t = typeService.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) {
typeService.deleteType(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/types";
}
}