JPA(Java Persistence API)是一种Java持久化解决方案,负责把数据保存到数据库,实际上它就是一种标准、规范,而不是具体的实现。JPA属于重量级的,因为它需要运行在JAVA EE容器中,而Spring Data JPA提供了轻量级的实现,在任何servlet容器中都可以运行。
Spring Data JPA相对于JAVA EE中的JPA,配置更简单,以轻量级的方式实现了部分在 EJB 容器环境下才具有的功能,将 EntityManager 的创建与销毁、事务管理等代码抽取出来,并由其统一管理,并且极大的简化了数据库访问层的代码。//pom配置
mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web //application.properties 配置 org.springframework.boot spring-boot-starter-test test
spring.freemarker.cache=false spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database=mysql #是否显示sql语句 spring.jpa.show-sql=true #自动建表 spring.jpa.hibernate.ddl-auto=update
实现接口用于对实体操作
package com.zjx.springbootjpadata.jpainterface; import com.zjx.springbootjpadata.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import java.io.Serializable; import java.util.List; /** * JpaRepository 简单查询接口 * JpaSpecificationExecutor 复杂查询接口 * Serializable 序列化接口 */ public interface UserJpa extends JpaRepository, JpaSpecificationExecutor , Serializable { List findAllByUserName(String username); } 测试用例
package com.zjx.springbootjpadata; import com.zjx.springbootjpadata.jpainterface.UserJpa; import com.zjx.springbootjpadata.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 使用SpringDataJPA完成数据的CRUD */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootjpadataApplicationTests { @Autowired private UserJpa userjpa; //新增 @Test public void save(){ User user = new User("张一","16","地球村"); userjpa.save(user); } //批量新增 @Test public void saveAll(){ ArrayListuserList = new ArrayList (); User user1 = new User("张一","10","地球村1号"); User user2 = new User("张二","11","地球村2号"); User user3 = new User("张三","12","地球村3号"); User user4 = new User("张四","13","地球村4号"); User user5 = new User("张五","14","地球村5号"); User user6 = new User("张六","15","地球村6号"); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); userjpa.saveAll(userList); } //查询所有 @Test public void findAll(){ List list = userjpa.findAll(); for(User u:list){ System.out.println(u.toString()); } System.out.println(list.size()); } //查询 @Test public void findByName(){ List list = userjpa.findAllByUserName("张一"); for(User u:list){ System.out.println(u.toString()); } System.out.println(list.size()); } //更新 @Test public void updateName(){ List list = userjpa.findAllByUserName("张一"); for(User u:list){ u.setUserName("张"); userjpa.save(u); System.out.println(u.toString()); } System.out.println(list.size()); } //删除 @Test public void deleName(){ List list = userjpa.findAllByUserName("张"); for(User u:list){ userjpa.delete(u); System.out.println(u.toString()); } System.out.println(list.size()); } }
以上是自学总结。有借鉴前辈帖子望见谅。不足之处请多多指教