Spring系列——使用JPA来操作数据
来源:互联网 发布:linux 更新grub 编辑:程序博客网 时间:2024/05/17 02:11
原文链接:http://www.dubby.cn/detail.html?id=9042
代码地址:https://github.com/dubby1994/spring-demo
JPA全称Java Persistence API,Java持久性API(简称JPA)是类和方法的集合,以数据关系映射持久并存储到数据库,这是由Oracle公司提供方案技术。可以减少开发者自己操作数据库时需要很多臃肿的代码。
1. 我们要做什么
我们会使用JPA来操作数据库,这里使用的时内存数据库,上一篇Spring系列——访问MySQL提到过,如果不配置,默认就是内置的内存数据库H2
。
2.你需要什么
- MySQL 5.6或者更高版本
- 大约15分钟
- 一个最爱的编辑器或者IDE
- JDK 1.8 +
- Maven 3.0+
3.创建项目
3.1 项目结构
maven项目,结构如下:
└── src └── main └── java └── hello
3.2 项目依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>cn.dubby</groupId> <artifactId>accessing-data-jpa</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> </repository> <repository> <id>org.jboss.repository.releases</id> <name>JBoss Maven Release Repository</name> <url>https://repository.jboss.org/nexus/content/repositories/releases</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories></project>
3.3 实体类
JPA是一个实体类和数据库映射的技术,所以这里先写出实体类 src/main/java/hello/Customer.java
package hello;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class Customer { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String firstName; private String lastName; protected Customer() {} public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format( "Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); } public Long getId() { return id; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; }}
Customer
这个实体类有三个属性,id
,firstName
和lastName
。有两个构造函数,没有参数的构造函数时为了给JPA使用,所以这里设置成protected,我们自己的代码中还是使用有参数的那个构造函数。
Customer
使用了@Entity
修饰,说明它是一个JPA的实体。没有使用@Table
修饰,所以这个实体类对应的表名就是Customer
。
Customer
的属性id
使用@Id
修饰,说明这个字段就是id。id
还被@GeneratedValue
修饰,说明这个id时有数据库自动生成的。
firstName
和lastName
这两个字段没有被修饰,说明这两个就是普通的字段对应数据库里两列,列名和字段名一样。
3.4 创建查询
Spring Data JPA专注于使用JPA来操作数据。其中最吸引人的特性就是我们只需要定义接口,Spring会帮我在运行时创建真正的实现。
src/main/java/hello/CustomerRepository.java
package hello;import org.springframework.data.repository.CrudRepository;import java.util.List;public interface CustomerRepository extends CrudRepository<Customer, Long> { List<Customer> findByLastName(String lastName);}
CustomerRepository
继承了CrudRepository
,泛型参数是<Customer, Long>
,分别代表了,实体类的类型和ID的类型。只要继承了CrudRepository
,就拥有了基本的增删改查的功能,类如其名CrudRepository
。
org.springframework.data.repository.CrudRepository
package org.springframework.data.repository;import java.io.Serializable;@NoRepositoryBeanpublic interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity); <S extends T> Iterable<S> save(Iterable<S> entities); T findOne(ID id); boolean exists(ID id); Iterable<T> findAll(); Iterable<T> findAll(Iterable<ID> ids); long count(); void delete(ID id); void delete(T entity); void delete(Iterable<? extends T> entities); void deleteAll();}
当然这些只是最基本的数据操作,Spring Data JPA也允许你写自己的查询语句。上面代码中的findByLastName()
就是自定义的查询语句。
3.5 启动类
src/main/java/hello/Application.java
package hello;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class Application { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class); } @Bean public CommandLineRunner demo(CustomerRepository repository) { return (args) -> { // save a couple of customers repository.save(new Customer("Jack", "Bauer")); repository.save(new Customer("Chloe", "O'Brian")); repository.save(new Customer("Kim", "Bauer")); repository.save(new Customer("David", "Palmer")); repository.save(new Customer("Michelle", "Dessler")); // fetch all customers log.info("Customers found with findAll():"); log.info("-------------------------------"); for (Customer customer : repository.findAll()) { log.info(customer.toString()); } log.info(""); // fetch an individual customer by ID Customer customer = repository.findOne(1L); log.info("Customer found with findOne(1L):"); log.info("--------------------------------"); log.info(customer.toString()); log.info(""); // fetch customers by last name log.info("Customer found with findByLastName('Bauer'):"); log.info("--------------------------------------------"); for (Customer bauer : repository.findByLastName("Bauer")) { log.info(bauer.toString()); } log.info(""); }; }}
3.6 测试
src/main/java/hello/CustomerRepositoryTests.java
package hello;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;import static org.assertj.core.api.Assertions.assertThat;@RunWith(SpringRunner.class)@DataJpaTestpublic class CustomerRepositoryTests { @Autowired private TestEntityManager entityManager; @Autowired private CustomerRepository customers; @Test public void testFindByLastName() { Customer customer = new Customer("first", "last"); entityManager.persist(customer); List<Customer> findByLastName = customers.findByLastName(customer.getLastName()); assertThat(findByLastName).extracting(Customer::getLastName).containsOnly(customer.getLastName()); }}
- Spring系列——使用JPA来操作数据
- 使用JPA进行数据操作
- Spring ORM数据访问——JPA
- 使用spring-data-jpa的JpaRepository来进行类的db操作(配置)
- MSDN Visual系列:使用BDC操作来控制业务数据
- spring使用jpa进行update操作
- Spring Data JPA 系列 —— 单表
- spring boot-jpa整合QueryDSL来简化复杂操作
- 通过Spring使用JPA进行数据访问
- Spring Boot系列(五):spring data jpa的使用
- Spring-boot中使用Spring-data-jpa操作DB
- Spring集成JPA操作
- 淘淘商城系列——使用Spring来管理Redis单机版和集群版
- Spring Data 系列之JPA
- Spring——集成JPA
- 8. 使用JPA保存数据【从零开始学Spring Boot】
- 8. 使用JPA保存数据【从零开始学Spring Boot】
- (8). 使用JPA保存数据【从零开始学Spring Boot】
- JAVA final关键字
- Andorid NoHttp简单使用
- hge 抗锯齿(二)
- java多线程
- 171021—用do...while和if完成素数完全分解。
- Spring系列——使用JPA来操作数据
- Docker、Containerd、RunC...:你应该知道的所有
- hge 抗锯齿(三)
- hge 抗锯齿(四)
- 中国成功企业家是真的白手起家吗
- Centos上Oracle-启动及DBCA配置
- 有向图找最小的环
- nosql数据库mongodb安装
- 达尔文OP2音频播放