上篇介绍了如何通过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进行数据库的操作。