springboot实战(二)——springdatajpa(持
2018-10-04 本文已影响0人
寻找大海的鱼
1.修改User实体类代码
@Entity // 实体
public class User {
@Id //主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //自增策略
private Long id; //实体一个唯一标识
private String name;
private String email;
protected User() { //无参构造函数;设为protected 防止直接使用
}
public User(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
2.修改UserRepository接口,并删除UserRepositoryImpl实现类
public interface UserRepository extends CrudRepository<User, Long> {
}
3.在mysql里面创建students数据库(不需要建表)
修改application.properties配置文件里的内容,username和password可根据自行修改
spring.datasource.url=jdbc:mysql://localhost:3306/students?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
4.修改UserController
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
/**
*@description: 查询所有的用户
*@author: 一条懒咸鱼
*@Date: 22:26 2018/10/2
**/
@GetMapping
public ModelAndView list(Model model){
System.out.println("=============list==============");
model.addAttribute("userList", userRepository.findAll());
model.addAttribute("title", "用户管理");
return new ModelAndView("list", "userModel",model);
}
/**
*@description: 根据id查询用户
*@author: 一条懒咸鱼
*@Date: 22:32 2018/10/2
**/
@GetMapping("{id}")
public ModelAndView view(@PathVariable("id") Long id, Model model){
User user = userRepository.findById(id).get();
model.addAttribute("user", user);
model.addAttribute("title", "查看用户");
return new ModelAndView("view", "userModel",model);
}
/**
*@description: 获取创建表单页面
*@author: 一条懒咸鱼
*@Date: 22:34 2018/10/2
**/
@GetMapping("/form")
public ModelAndView createForm(Model model){
model.addAttribute("user", new User(null,null, null));
model.addAttribute("title", "创建用户");
return new ModelAndView("form", "userModel",model);
}
/**
*@description: 保存或者修改用户
*@author:一条懒咸鱼
*@Date: 22:38 2018/10/2
**/
@PostMapping
public ModelAndView saveOrUpdateUser(User user){
userRepository.save(user);
return new ModelAndView("redirect:/users"); //重定向到list页面
}
/**
*@description: 删除用户
*@author: 一条懒咸鱼
*@Date: 23:15 2018/10/2
**/
@GetMapping("/delete/{id}")
public ModelAndView delete(@PathVariable("id") Long id){
userRepository.deleteById(id);
return new ModelAndView("redirect:/users"); //重定向到list页面
}
/**
*@description: 获取修改用户的界面
*@author: 一条懒咸鱼
*@Date: 21:39 2018/10/3
**/
@GetMapping("/modify/{id}")
public ModelAndView modify(@PathVariable("id") Long id, Model model){
User user = userRepository.findById(id).get();
model.addAttribute("user", user);
model.addAttribute("title", "修改用户");
return new ModelAndView("form","userModel", model);
}
}
5.启动测试,数据便会持久化到mysql数据库里面了。