Spring Boot integrating Spring Data JPA introduction 01

Spring Data JPA: Introduction:
Spring Data is the framework that spring provides operation database, and Spring Data JPA is just a module based on JPA standard operation database under Spring Data framework.
Spring Data JPA: operate on data based on JPA standard. Simplify the code of persistence layer, only need to write interface.
Spring boot integrates Spring Data JPA
1. Add dependency:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
         <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

2. Add application.yml configuration information:

#mysql database driver URL username password
#Database connection pool
#Forward engineering
#Console output sql
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

New database

3. Add entity class

@Entity
@Table(name = "t_users")
@Data
public class Users {

    /**
     * @Id:Indicates that id is the primary key
     * @GeneratedValue: Generation strategy
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private Integer age;
    @Column(name = "address")
    private String address;
}

4. Add persistent layer interface

/**
 * Parameter 1: entity class to be mapped currently
 * Parameter 2: id primary key type in the entity class to be mapped currently
 */
public interface UsersRepository  extends JpaRepository<Users,Integer> {
}

5. Add the coordinates of the test startup class in the pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

6. Create a new startup class

@SpringBootApplication
public class SpringBootDataJpaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDataJpaApplication.class, args);
    }
}

Write test code
Under the test package, create a new com.gblfy.repository package (consistent with the java package structure above):
The new test class UsersRepositoryTest inherits the test class generated by Springboot. The advantage of this is that it does not need to add test annotation to each test class, but only inherits the common test class (the same principle as the interface implementation class)

@RunWith(SpringRunner.class)
@SpringBootTest
public class UsersRepositoryTest {

    @Autowired
    private UsersRepository usersRepository;

    @Test
    public void saveUser(){
        Users u = new Users();
        u.setName("zhangdan");
        u.setAge(25);
        u.setAddress("beijing");
        usersRepository.save(u);
    }
}

10. Operation effect diagram:

View database:
1. Generate T ﹣ users table
2. Whether to insert the data into the database successfully

Tags: Spring Database MySQL Java

Posted on Fri, 08 Nov 2019 08:39:05 -0800 by theeper