spring boot jpadata 使用

 JPA(Java Persistence API)是一种Java持久化解决方案,负责把数据保存到数据库,实际上它就是一种标准、规范,而不是具体的实现。JPA属于重量级的,因为它需要运行在JAVA EE容器中,而Spring Data JPA提供了轻量级的实现,在任何servlet容器中都可以运行。
        Spring Data JPA相对于JAVA EE中的JPA,配置更简单,以轻量级的方式实现了部分在 EJB 容器环境下才具有的功能,将 EntityManager 的创建与销毁、事务管理等代码抽取出来,并由其统一管理,并且极大的简化了数据库访问层的代码。

//pom配置

    <!--引入MySQL的依赖关系-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
//application.properties 配置
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<User,Long>, JpaSpecificationExecutor<User>, Serializable {

List<User> 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(){

ArrayList<User> userList = new ArrayList<User>();
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<User> list = userjpa.findAll();
for(User u:list){

System.out.println(u.toString());

}
System.out.println(list.size());

}
//查询
@Test
public void findByName(){

List<User> list = userjpa.findAllByUserName("张一");
for(User u:list){

System.out.println(u.toString());

}
System.out.println(list.size());

}

//更新
@Test
public void updateName(){


List<User> 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<User> list = userjpa.findAllByUserName("张");
for(User u:list){


userjpa.delete(u);
System.out.println(u.toString());

}
System.out.println(list.size());

}
}
以上是自学总结。有借鉴前辈帖子望见谅。不足之处请多多指教 
posted @ 2019-08-28 12:40  易行舟  阅读(287)  评论(0编辑  收藏  举报