上篇介紹瞭如何通過Springboot完成一個rest服務接口開發,這篇主要介紹Springboot如何集成JPA完成數據庫映射,實現數據庫的增刪改查。
首先,第一步,毋庸置疑,將相關包進行導入,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
將包導入成功後,首先編寫實現實體類實現:
相信瞭解過JPA的同學,都知道,JPA能夠實現數據庫與實體類自動映射。
package com.example.springboot.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="name")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接下來編寫持久層代碼實現:
package com.example.springboot.domain.user;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.springboot.domain.User;
//持久層
@Repository
public interface IUserDao extends JpaRepository<User, Integer>{
//自定義實現方法,不需要編寫sql,主要遵循JPA規範,編寫函數名稱即可
public List<User> findUserByUsername(String username);
}
如上,上述接口代碼繼承JpaRepository接口,就能夠省掉開發人員編寫sql語言的操作,簡單方便,這裏是遵循JPA規範開發的,大體就是遵循Spring Data Jpa函數命名規範,如需更深入瞭解,可參考文章https://www.cnblogs.com/jaejaking/p/7994233.html。
接下來開始編寫業務層接口,如下:
package com.example.springboot.service;
import java.util.List;
import com.example.springboot.domain.User;
/**
* 業務層接口
*/
public interface IUserService {
public List<User> findAllUser();
public void saveUser();
public void deleteUserByid(Integer id);
public List<User> findUserByUserName(String username);
public User addUser(User user);
}
編寫了業務層接口,就需要對業務層接口進行實現,如下;
package com.example.springboot.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springboot.domain.User;
import com.example.springboot.domain.user.IUserDao;
import com.example.springboot.service.IUserService;
/**
* 業務層實現
* @author lenovo
*
*/
@Service("userService")
public class UserServiceImpl implements IUserService{
@Autowired
private IUserDao userDao;
@Override
public List<User> findAllUser() {
return userDao.findAll();
}
@Override
public void saveUser() {
User user = new User();
user.setName("蘭州大學");
user.setPassword("123456");
user.setUsername("tan313");
userDao.save(user);
}
@Override
public void deleteUserByid(Integer id) {
userDao.deleteById(id);
}
@Override
public List<User> findUserByUserName(String username) {
return userDao.findUserByUsername(username);
}
@Override
public User addUser(User user) {
return userDao.save(user);
}
}
這裏注意在業務層接口實現上面,增加註解@Service。可看到,在業務層實現方法中,調用了持久層接口進行實現。第一次接觸會感到很奇怪,userDao接口我並沒有聲明findAll()方法等,這就是JPA規範開發的強大,這都是內部封裝好的方法,也可以在持久層自定義方法實現,需要遵循JPA規範開發即可。
完成業務層接口開發,接下來完成,控制層開發:
package com.example.springboot.control;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.springboot.domain.User;
import com.example.springboot.service.IUserService;
/**
* 控制層
* @author lenovo
*
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll")
public List<User> findAllUser(){
List<User> users = userService.findAllUser();
return users;
}
///
@RequestMapping(value="/addUser", method=RequestMethod.POST)
public User addUser(User user){
return userService.addUser(user);
}
@RequestMapping("/saveUser")
public int saveUser(){
userService.saveUser();
return 0;
}
@RequestMapping("/deleteUser/{id}")
public int deleteUserById(@PathVariable(name="id",required=true) Integer id){
userService.deleteUserByid(id);
return 0;
}
@RequestMapping(value="/findUserByName/{username}",method=RequestMethod.GET)
public List<User> findUserByName(@PathVariable(name="username",required=true) String name){
return userService.findUserByUserName(name);
}
}
最後附加下application.properties文件:
########################################################
###\u6570\u636E\u5E93\u8FDE\u63A5\u4FE1\u606F
########################################################
#\u8FDE\u63A5\u5730\u5740
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
#\u6570\u636E\u5E93\u8D26\u6237
spring.datasource.username=root
#\u6570\u636E\u5E93\u5BC6\u7801
spring.datasource.password=123
#\u6570\u636E\u5E93\u9A71\u52A8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
########################################################
### Java Persistence Api JPA\u76F8\u5173\u914D\u7F6E
########################################################
#\u6307\u5B9A\u6570\u636E\u5E93\u7C7B\u578B
spring.jpa.database=mysql
#\u63A7\u5236\u53F0\u6253\u5370sql
spring.jpa.show-sql=true
#\u5EFA\u8868\u7B56\u7565\uFF0C\u8FD9\u91CC\u7528update\uFF0C\u5373\u6839\u636E\u5B9E\u4F53\u66F4\u65B0\u8868\u7ED3\u6784
spring.jpa.hibernate.ddl-auto=update
#\u8868\u4E2D\u5B57\u6BB5\u547D\u540D\u7B56\u7565,\u8FD9\u91CC\u8981\u5F15\u5165hibernate\u7684\u6838\u5FC3\u5305\uFF0C\u4E0D\u7136\u8FD9\u4E2A\u547D\u540D\u7B56\u7565\u4F1A\u62A5\u9519
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
#\u65B9\u8A00
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
代碼全部貼出來了下面進行測試,使用postman工具進行測試
測試如下:
1、添加用戶:
2、查詢出所有用戶:
3、通過username進行查詢用戶:
這篇主要介紹了Springboot集成JPA完成數據庫操作,下一篇將介紹,Springboot集成mybatis進行數據庫的操作。