在spring boot中使用JPA詳解
阿新 • • 發佈:2018-11-06
1.首先在pom.xml檔案中匯入jar包
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.0.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
2.在resources下建立application.yml檔案,在裡面設定資料庫連線,如下:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true username: root password: ROOT mvc: static-path-pattern: /static/** server: servlet: context-path: /s
3.建立UserDao介面,繼承JpaRepository介面,如下所示:
public interface UserDao extends JpaRepository<User,Integer> {
}
4.建立UserController類,在類中宣告一個成員屬性UserDao,通過UserDao呼叫方法,實現CRUD
(1)查詢 queryAll()
(2)新增 save
(3)修改
User user = userDao.getOne(id); user.setName(name); return userDao.save(user);
(4)刪除 deleteById
具體實現程式碼如下:
package com.boot.bootdemo.controller;
import com.boot.bootdemo.dao.UserDao;
import com.boot.bootdemo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/index")
public String index(){
return "/index";
}
@RequestMapping("/queryAll")
@ResponseBody
public List<User> query(){
return userDao.findAll();
}
@RequestMapping("/update/{id}/{name}")
@ResponseBody
public User update(@PathVariable("id") Integer id, @PathVariable("name") String name){
User user = userDao.getOne(id);
user.setName(name);
return userDao.save(user);
}
@RequestMapping("/del/{id}")
@ResponseBody
public void del(@PathVariable("id") Integer id){
userDao.deleteById(id);
}
@RequestMapping("/add")
@ResponseBody
public User add(User user){
return userDao.save(user);
}
//自定義方法
@RequestMapping("/queryAge/{age}")
@ResponseBody
public List<User> findByAge(@PathVariable("age") Integer age){
return userDao.queryByAge(age);
}
}
5.UserDao自定義方法
UserDao中程式碼實現
public interface UserDao extends JpaRepository<User,Integer> {
public List<User> queryByAge(Integer age);
}
UserController中程式碼實現
//自定義方法
@RequestMapping("/queryAge/{age}")
@ResponseBody
public List<User> findByAge(@PathVariable("age") Integer age){
return userDao.queryByAge(age);
}