社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
SpringData 为我们提供使用统一的 API 来对数据访问层进行操作。这主要是 SpringData Commons 项目来实现的。SpringData Commons 让我们在使用关系型或者非关系型数据访问技术时都基于 Spring 提供的统一标准,标准包含了 CRUD(创建、获取、更新、删除)、查询、排序和分页的相关操作
Repository<T, ID extends Serializable> 统一接口
RevisionRepository<T, ID extends Serializable, N extends Number & Comparable> 基于乐观锁机制
CurdRepository<T, ID extends Serializable> 基本 CRUD 操作
PagingAndSortingRepository<T, ID extends Serializable> 基本 CRUD 及分页
编写一个实体类(bean)和数据表进行映射,并且配置好映射
package com.example.demo.entity;
import javax.persistence.*;
/**
* @author Woo_home
* create by 2020-05-16 20:30
*/
// 配置映射关系
@Entity // 告诉 JPA 这是一个实体类(和数据表映射的类)
@Table(name = "tbl_user") // @Table 指定和哪个数据表对应,如果省略,那么默认表名就是类名的小写
public class User {
@Id // 这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // 自增主键
private Integer id;
@Column(name = "last_name", length = 50) // 这是和数据表对应的一个列
private String lastName;
@Column // 省略默认列名就是属性名
private String email;
}
编写一个 Dao 接口来操作实体类对应的数据表(Repository)
package com.example.demo.repository;
import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author Woo_home
* create by 2020-05-16 20:37
*/
// 继承 JpaRepository 来完成对数据库的操作
public interface UserRepository extends JpaRepository<User, Integer> {
}
application.properties 配置
# 更新或者创建数据表结构
spring.jpa.hibernate.ddl-auto=update
# 控制台显示 SQL
spring.jpa.show-sql=true
控制台会输出以下内容,这是创建了一个表
打开 Navicat 可以发现,将我们的 tbl_user 表创建成功了
字段也是根据我们设置的一样
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Woo_home
* create by 2020-05-16 21:32
*/
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
/**
* 查询数据
* @param id 根据 ID 查询
* @return
*/
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id) {
User user = userRepository.findById(id).get();
return user;
}
/**
* 插入数据
* @param user
* @return
*/
@GetMapping("/user")
public User insertUser(User user) {
User save = userRepository.save(user);
return save;
}
/**
* 删除数据
* @param id 根据 id 删除
*/
@GetMapping("/delete/{id}")
public void deleteUser(@PathVariable("id") Integer id) {
userRepository.deleteById(id);
}
}
/**
* 插入数据
* @param user
* @return
*/
@GetMapping("/user")
public User insertUser(User user) {
User save = userRepository.save(user);
return save;
}
在浏览器中输入 http://localhost:8080/user?lastName=zhangsan&email=aa ,即可插入一条数据
刷新数据库
控制台输出:
/**
* 查询数据
* @param id 根据 ID 查询
* @return
*/
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id) {
User user = userRepository.findById(id).get();
return user;
}
在浏览器中输入 http://localhost:8080/user/1 即可获得以下数据
控制台输出:
/**
* 删除数据
* @param id 根据 id 删除
*/
@GetMapping("/delete/{id}")
public void updateUser(@PathVariable("id") Integer id) {
userRepository.deleteById(id);
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!