spring boot jpadata use

 JPA (Java Persistence API) is a Java persistence solutions, responsible for saving data to the database, in fact it is a standard specification, rather than specific implementation. JPA belong to heavyweight, because it needs to run JAVA EE container, and Spring Data JPA provides a lightweight implementation, can be run in any servlet container.
        Spring Data JPA relative JAVA EE in the JPA, the configuration is simpler, lightweight way to achieve some in the EJB container environment that has the features that will EntityManager of creation and destruction, transaction management code is extracted by its unified management, and greatly simplifies the code database access layer.

// pom configuration

    <!--引入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
Implementing the interface for operation of the entity
com.zjx.springbootjpadata.jpainterface Package; 

Import com.zjx.springbootjpadata.pojo.User;
Import org.springframework.data.jpa.repository.JpaRepository;
Import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

Import the java.io .Serializable;
Import java.util.List;

/ **
* JpaRepository simple query Interface
* JpaSpecificationExecutor complex query interface
* serializable serial interfaces
* /
public interface UserJpa the extends JpaRepository <the User, Long>, JpaSpecificationExecutor <the User>, the serializable {

List < the User> findAllByUserName (String username);
}
test
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;

// Add
@Test
public void Save () {

the User User the User new new = ( "Zhang", "16", "global village");
userjpa.save (User);
}

// add bulk
@Test
public void saveAll () {

the ArrayList <the User> userList = new new the ArrayList <the User> ();
the User user1 = new new the User ( "Zhang", "global village No. 1", "10");
the User user2 = new new the User ( "Zhang II", "11", "global village No. 2");
the User USER3 = new new the User ( "San", "12", "global village. 3");
the User USER4 = new new the User ( "Zhang four", "13", "global village No. 4 ");
the User = new new USER5 the User (" the five "," 14 "," global village No. 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());

}
}
These are self-summary. Senior posts have reference hope forgive me. Inadequacies Please exhibitions

Guess you like

Origin www.cnblogs.com/yixingzhou/p/11423373.html