赞
踩
我们需要添加Spring Data JPA及Mysql连接的驱动的依赖.Spring Boot已经提供了相关的Starter来实现Spring Data JPA开箱及用的功能,只需要在build.gradle文件中添加Spring Data JPA的starter的库即可.
- dependencies {
- compile('org.springframework.boot:spring-boot-starter-web')
- //添加Spring Data JPA 的依赖
- compile('org.springframework.boot:spring-boot-starter-data-jpa')
- //添加Mysql连接驱动依赖
- compile('mysql:mysql-connector-java:6.0.5')
-
-
- testCompile('org.springframework.boot:spring-boot-starter-test')
- }
spring-boot-starter-data-jpa库提供了以下依赖:hibernate,Spring Data JPA,Spring ORM
Spring Boot的自动配置功能会自动检测有无指定的数据库类型.如果没有指定,Spring Boot会认为用户想要配置一个内嵌的数据库,此时会扫描classpath有相应的内嵌数据库.
- dependencies {
- //....
- //添加h2依赖
- runtime('com.h2database:h2:1.4.196')
- //.....
- }
其中"runtime"表明该依赖对于运行时是必要的.
这里不在详述,资料横多
将在之前项目上建立,和之前的博客(Thymeleaf)相对应,之前已有代码只修改不重写了
a.User类不修改,可以直接用
b.修改用户资源接口,继承自CrudRepository
- import org.springframework.data.repository.CrudRepository;
-
-
- public interface UserRepository extends CrudRepository<User, Long> {
-
- }
由于Spring Data JPA已经帮助用户做了实现,因此用户不需要做任何实现.
c.修改控制器
UserController实现的方法全部换成JPA默认实现.
-
-
- import java.util.Optional;
-
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.ui.Model;
- 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.bind.annotation.RestController;
- import org.springframework.web.servlet.ModelAndView;
-
- @RestController
- @RequestMapping("/users")
- public class UserController {
-
- @Autowired
- private UserRepository userRepository;
- /**
- * 查询所有用户
- * @param model
- * @return
- */
- @GetMapping
- public ModelAndView list(Model model) {
- model.addAttribute("userList",userRepository.findAll());
- model.addAttribute("title","用户管理");
- return new ModelAndView("users/list","userModel",model);
- }
- /**
- * 根据id查询用户
- * @param id
- * @param model
- * @return
- */
- @GetMapping("{id}")
- public ModelAndView view(@PathVariable("id") Long id,Model model) {
- Optional<User> user = userRepository.findById(id);
- model.addAttribute("user",user.get());
- model.addAttribute("title","查看用户");
- return new ModelAndView("users/view","userModel",model);
- }
- /**
- * 获取创建表单页面
- * @param model
- * @return
- */
- @GetMapping("/form")
- public ModelAndView createForm(Model model) {
- model.addAttribute("user", new User(null,null,null));
- model.addAttribute("title","创建用户");
- return new ModelAndView("users/form","userModel",model);
- }
- /**
- * 保存或者修改用户
- * @param user
- * @return
- */
- @PostMapping
- public ModelAndView saveOrUpdateUser(User user) {
- user = userRepository.save(user);
- return new ModelAndView("redirect:/users");
- }
- /**
- * 废除用户
- * @param id
- * @return
- */
- @GetMapping(value = "delete/{id}")
- public ModelAndView delete(@PathVariable("id") Long id) {
- userRepository.deleteById(id);
- return new ModelAndView("redirect:/users");
- }
- /**
- * 获取修改用户的界面
- * @param id
- * @param model
- * @return
- */
- @GetMapping(value = "modify/{id}")
- public ModelAndView modifyForm(@PathVariable("id") Long id,Model model) {
- Optional<User> user = userRepository.findById(id);
- model.addAttribute("user",user.get());
- model.addAttribute("title","修改用户");
- return new ModelAndView("users/form","userModel",model);
- }
-
-
-
-
- }
d.创建数据库
首先创建数据库,编码为utf-8
- mysql> drop database if exists xchange;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql> create database xchange default charset utf8 collate utf8_general_ci;
- Query OK, 1 row affected (0.00 sec)
修改application.properties,增加下面几项配置.
- #DataSource
- spring.datasource.url=jdbc:mysql://localhost/project?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
- #JPA
- spring.jpa.show-sql = true
- spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=create-drop,指每次启动 时,都会自动废除并创建数据库中的表,在开发时非常方便.
e.打开网页测试结束.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。