当前位置:   article > 正文

Spring Data JPA与Hibernate,Spring Boot集成,实战_spring-data-jpa+hibernate

spring-data-jpa+hibernate

1.build.gradle

我们需要添加Spring Data JPA及Mysql连接的驱动的依赖.Spring Boot已经提供了相关的Starter来实现Spring Data JPA开箱及用的功能,只需要在build.gradle文件中添加Spring Data JPA的starter的库即可.

  1. dependencies {
  2. compile('org.springframework.boot:spring-boot-starter-web')
  3. //添加Spring Data JPA 的依赖
  4. compile('org.springframework.boot:spring-boot-starter-data-jpa')
  5. //添加Mysql连接驱动依赖
  6. compile('mysql:mysql-connector-java:6.0.5')
  7. testCompile('org.springframework.boot:spring-boot-starter-test')
  8. }

spring-boot-starter-data-jpa库提供了以下依赖:hibernate,Spring Data JPA,Spring ORM

2.集成H2

Spring Boot的自动配置功能会自动检测有无指定的数据库类型.如果没有指定,Spring Boot会认为用户想要配置一个内嵌的数据库,此时会扫描classpath有相应的内嵌数据库.

  1. dependencies {
  2. //....
  3. //添加h2依赖
  4. runtime('com.h2database:h2:1.4.196')
  5. //.....
  6. }

其中"runtime"表明该依赖对于运行时是必要的.

3.下载安装mysql

这里不在详述,资料横多

4.实战

将在之前项目上建立,和之前的博客(Thymeleaf)相对应,之前已有代码只修改不重写了

a.User类不修改,可以直接用

 

b.修改用户资源接口,继承自CrudRepository

  1. import org.springframework.data.repository.CrudRepository;
  2. public interface UserRepository extends CrudRepository<User, Long> {
  3. }

由于Spring Data JPA已经帮助用户做了实现,因此用户不需要做任何实现.

 

c.修改控制器

UserController实现的方法全部换成JPA默认实现.

  1. import java.util.Optional;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.web.servlet.ModelAndView;
  10. @RestController
  11. @RequestMapping("/users")
  12. public class UserController {
  13. @Autowired
  14. private UserRepository userRepository;
  15. /**
  16. * 查询所有用户
  17. * @param model
  18. * @return
  19. */
  20. @GetMapping
  21. public ModelAndView list(Model model) {
  22. model.addAttribute("userList",userRepository.findAll());
  23. model.addAttribute("title","用户管理");
  24. return new ModelAndView("users/list","userModel",model);
  25. }
  26. /**
  27. * 根据id查询用户
  28. * @param id
  29. * @param model
  30. * @return
  31. */
  32. @GetMapping("{id}")
  33. public ModelAndView view(@PathVariable("id") Long id,Model model) {
  34. Optional<User> user = userRepository.findById(id);
  35. model.addAttribute("user",user.get());
  36. model.addAttribute("title","查看用户");
  37. return new ModelAndView("users/view","userModel",model);
  38. }
  39. /**
  40. * 获取创建表单页面
  41. * @param model
  42. * @return
  43. */
  44. @GetMapping("/form")
  45. public ModelAndView createForm(Model model) {
  46. model.addAttribute("user", new User(null,null,null));
  47. model.addAttribute("title","创建用户");
  48. return new ModelAndView("users/form","userModel",model);
  49. }
  50. /**
  51. * 保存或者修改用户
  52. * @param user
  53. * @return
  54. */
  55. @PostMapping
  56. public ModelAndView saveOrUpdateUser(User user) {
  57. user = userRepository.save(user);
  58. return new ModelAndView("redirect:/users");
  59. }
  60. /**
  61. * 废除用户
  62. * @param id
  63. * @return
  64. */
  65. @GetMapping(value = "delete/{id}")
  66. public ModelAndView delete(@PathVariable("id") Long id) {
  67. userRepository.deleteById(id);
  68. return new ModelAndView("redirect:/users");
  69. }
  70. /**
  71. * 获取修改用户的界面
  72. * @param id
  73. * @param model
  74. * @return
  75. */
  76. @GetMapping(value = "modify/{id}")
  77. public ModelAndView modifyForm(@PathVariable("id") Long id,Model model) {
  78. Optional<User> user = userRepository.findById(id);
  79. model.addAttribute("user",user.get());
  80. model.addAttribute("title","修改用户");
  81. return new ModelAndView("users/form","userModel",model);
  82. }
  83. }

d.创建数据库

首先创建数据库,编码为utf-8

  1. mysql> drop database if exists xchange;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> create database xchange default charset utf8 collate utf8_general_ci;
  4. Query OK, 1 row affected (0.00 sec)

修改application.properties,增加下面几项配置.

  1. #DataSource
  2. spring.datasource.url=jdbc:mysql://localhost/project?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
  3. spring.datasource.username=root
  4. spring.datasource.password=123456
  5. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  6. #JPA
  7. spring.jpa.show-sql = true
  8. spring.jpa.hibernate.ddl-auto=create-drop

spring.jpa.hibernate.ddl-auto=create-drop,指每次启动 时,都会自动废除并创建数据库中的表,在开发时非常方便.

e.打开网页测试结束.

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/815261
推荐阅读
相关标签
  

闽ICP备14008679号