SpringBoot2.0 base case (09): JPA persistence framework integration, simplify working with databases

A, JAP framework Introduction

JPA (Java Persistence API) means the Java Persistence API, Java Sun is made official after JDK5.0 persistence specification. Mainly in order to simplify the development and integration of ORM persistence layer technology, the end of Hibernate, TopLink, JDO and other ORM framework for their own business situation. JPA is developed on the basis of absorbing the existing ORM framework, easy to use, strong scalability.

Second, integration with SpringBoot2.0

1, dependent on the core

<!-- JPA框架 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2, the configuration file

spring:
  application:
    name: node09-boot-jpa
  datasource:
    url: jdbc:mysql://localhost:3306/data_jpa?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Several ddl-auto configuration instructions
1) create
are deleted every time you load the hibernate generated on a table, and then re-generate a new table based on the bean class, easily lead to loss of data, used when first created (recommended).
2) create-drop
per hibernate loading table generated according bean class, but sessionFactory a closed table is automatically deleted.
3) update
when you first load hibernate bean class is automatically established according to the structure of a table, the table structure is automatically updated according to the bean class is loaded hibernate later, even if the table structure has changed, but there are still rows in the table does not delete the previous row .
4) validate
each time you load hibernate, verify that create the database table structure, and the only table in the database are compared, does not create a new table, but will insert a new value.

3, the entity class object

The object is to generate a table structure.

@Table(name = "t_user")
@Entity
public class User {
    @Id
    @GeneratedValue
    private Integer id;
    @Column
    private String name;
    @Column
    private Integer age;
    // 省略 GET SET
}

Usage 4, JPA framework

Interface operation defined objects, inheritance JpaRepository core interface.

import com.boot.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {

    // 但条件查询
    User findByAge(Integer age);
    // 多条件查询
    User findByNameAndAge(String name, Integer age);
    // 自定义查询
    @Query("from User u where u.name=:name")
    User findSql(@Param("name") String name);
}

5, a service layer encapsulating logic

import com.boot.jpa.entity.User;
import com.boot.jpa.repository.UserRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
    @Resource
    private UserRepository userRepository ;
    // 保存
    public void addUser (User user){
        userRepository.save(user) ;
    }
    // 根据年龄查询
    public User findByAge (Integer age){
        return userRepository.findByAge(age) ;
    }
    // 多条件查询
    public User findByNameAndAge (String name, Integer age){
        return userRepository.findByNameAndAge(name,age) ;
    }
    // 自定义SQL查询
    public User findSql (String name){
        return userRepository.findSql(name) ;
    }
    // 根据ID修改
    public void update (User user){
        userRepository.save(user) ;
    }
    //根据id删除一条数据
    public void deleteStudentById(Integer id){
        userRepository.deleteById(id);
    }
}

Third, the test block

import com.boot.jpa.JpaApplication;
import com.boot.jpa.entity.User;
import com.boot.jpa.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = JpaApplication.class)
public class UserJpaTest {
    @Resource
    private UserService userService ;
    @Test
    public void addUser (){
        User user = new User() ;
        user.setName("知了一笑");
        user.setAge(22);
        userService.addUser(user);
        User user1 = new User() ;
        user1.setName("cicada");
        user1.setAge(23);
        userService.addUser(user1);
    }
    @Test
    public void findByAge (){
        Integer age = 22 ;
        // User{id=3, name='知了一笑', age=22}
        System.out.println(userService.findByAge(age));
    }
    @Test
    public void findByNameAndAge (){
        System.out.println(userService.findByNameAndAge("cicada",23));
    }
    @Test
    public void findSql (){
        // User{id=4, name='cicada', age=23}
        System.out.println(userService.findSql("cicada"));
    }
    @Test
    public void update (){
        User user = new User() ;
        // 如果这个主键不存在,会以主键自增的方式新增入库
        user.setId(3);
        user.setName("哈哈一笑");
        user.setAge(25);
        userService.update(user) ;
    }
    @Test
    public void deleteStudentById (){
        userService.deleteStudentById(5) ;
    }
}

Fourth, the source address

GitHub地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base


Guess you like

Origin www.cnblogs.com/cicada-smile/p/11033037.html