springboot 数据访问之 JPA

JPA 的底层用的 Hibernate 来实现,只要熟悉 Hibernate 或者其他 ORM 框架,在使用JPA时会发现其实非常容易上手,在 springboot 中使用也非常简单方便。
加入依赖 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xiao</groupId>
  <artifactId>springbootJPA</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>2.1.4.RELEASE</version>
  </parent>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-data-jpa</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-jdbc</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<scope>runtime</scope>
  	</dependency>
  	
  </dependencies>
  
  <build>
  	<plugins>
  		<!-- 将应用打包成一个可执行的 jar 包 -->
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  	</plugins>
  </build>
</project>

写实体类,这是重点,注意属性与字段的映射,表名与类名的映射
注意加上 @JsonIgnoreProperties 注解,不加这个注解我这里是报错了

import javax.persistence.*;

@JsonIgnoreProperties(value = {
    
     "hibernateLazyInitializer", "handler" })
@Entity
@Table(name="computers")  // 不指定表名默认是类名小写
public class Computer {
    
    

	@Id  //主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) // 自增主键
	private Integer cid;
	
	@Column(name="c_type",length=20) // 指定表中的列名与属性的映射
	private String ctype;
	
	@Column  // 不指定列名,默认同属性名
	private String productAddr;

	public Integer getCid() {
    
    
		return cid;
	}

	public void setCid(Integer cid) {
    
    
		this.cid = cid;
	}

	public String getCtype() {
    
    
		return ctype;
	}

	public void setCtype(String ctype) {
    
    
		this.ctype = ctype;
	}

	public String getProductAddr() {
    
    
		return productAddr;
	}

	public void setProductAddr(String productAddr) {
    
    
		this.productAddr = productAddr;
	}
	
}

需要写一个接口,继承 jpa 中实现的各种操作数据库的方法,这个接口不需要内容。

public interface ComputerRepository extends JpaRepository<Computer, Integer> {
    
    

	// 不用写内容,继承的 JpaRepository 中已写好很多方法
}

配置数据源 application.properties

## 配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/boot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=mysql

# 有则更新,无则创建数据表结构
spring.jpa.hibernate.ddl-auto=update
# 显示 sql
spring.jpa.show-sql=true

最后写一个 controller 来调用操作数据库的方法

@RestController
public class ComputerController {
    
    

	@Autowired
	ComputerRepository repo;
	
	@GetMapping("/computer/{id}")
	public Computer getComputerById(@PathVariable("id") Integer cid) {
    
    
		return repo.getOne(cid);
	}
	
	@GetMapping("/computer")
	public Computer insertComputer(Computer com) {
    
    
		Computer computer = repo.save(com);
		return computer;
	}
}

启动项目,可看到数据库自动创建,发送相应请求时,能执行相应的数据库操作
在这里插入图片描述在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Alias_fa/article/details/90083977
今日推荐