SpringBoot Unofficial Tutorial | Part 4: SpringBoot Integration JPA

For reprinting, please indicate the source:
http://blog.csdn.net/forezp/article/details/70545038 
This article is from Fang Zhipeng's blog

The full name of JPA is Java Persistence API.JPA describes the mapping relationship of object-relational tables through JDK 5.0 annotations or XML, and persists the runtime entity objects to the database.

One of the goals of JPA is to develop an API that can be implemented by many vendors, and developers can code to implement the API, rather than using private vendor-specific APIs.

JPA needs a Provider to implement its functions. Hibernate is one of the most powerful JPA Provider s. It should be said that no one can match it. Functionally, JPA is a subset of Hibernate's functionality.

Adding dependencies

Add spring-boot-starter-jdbc dependencies:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa
            </artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Add mysql connection class and connection pool class:

    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Configure the data source in the application.properties file:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456

  jpa:
    hibernate:
      ddl-auto: update  # Use update after the first summary table create
    show-sql: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Note that if you build tables in the database through jpa, change jpa.hibernate,ddl-auto to create. After the table is built, change to update, otherwise every reboot will delete the tables and create new ones.

Create entity classes

The @Entity indicates that it is a mapping entity class, the @Id indicates id, and the @GeneratedValue field is automatically generated.

@Entity
public class Account {
    @Id
    @GeneratedValue
    private int id ;
    private String name ;
    private double money;

...  ellipsis getter setter
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Dao level

Data access layer, by writing an interface inherited from JpaRepository, can complete data access, which contains several forms of query method, very convenient. It's worth noting that this Account object name is not a specific table name, and that Interger is the primary key type, usually Integer or Long.

public interface AccountDao  extends JpaRepository<Account,Integer> {
}

  • 1
  • 2
  • 3
  • 4

Web Tier

In this chestnut, I outlined the writing of service layer, which can not be omitted in actual development. Write a new controller and several restful APIs to test data access.

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountDao accountDao;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountDao.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return accountDao.findOne(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        account.setId(id);
        Account account1 = accountDao.saveAndFlush(account);

        return account1.toString();

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        Account account1 = accountDao.save(account);
        return account1.toString();

    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

Through the postman request test, the code has all passed the test.

Keywords: MySQL Hibernate Spring JDBC

Added by Chef-Mars on Sun, 19 May 2019 12:17:31 +0300