SpringBoot unofficial tutorials | Article 4: Integrating JPA and springbootjpa in SpringBoot

Source: Internet
Author: User

SpringBoot unofficial tutorials | Article 4: Integrating JPA and springbootjpa in SpringBoot

Reprinted please indicate the source:
Http://blog.csdn.net/forezp/article/details/70545038
This article is from Fang zhipeng's blog

JPA stands for Java Persistence API. JPA describes the ing relationship between object-relational tables through JDK 5.0 annotations or XML, and persists object objects in the database at runtime.

One of the goals of JPA is to develop an API that can be implemented by many vendors, and developers can code it to implement this API, rather than using the API unique to private providers.

JPA requires providers to implement its functions, and Hibernate is a strong one in JPA providers. It should be said that no one can output its right. In terms of functions, JPA is a subset of Hibernate functions.

Add dependency

Add spring-boot-starter-jdbc dependency:

 
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa
            </artifactId>
        </dependency>

Add the mysql connection class and connection pool class:

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency> 
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 # update after the first profile creation
     show-sql: true

Note: If you use jpa to create a table in the database, replace jpa. hibernate and ddl-auto with create. After the table is created, replace it with update. Otherwise, the table is deleted and created every time you restart the project.

Create object class

@ Entity indicates a ing Entity class, @ Id indicates id, and @ GeneratedValue field is automatically generated.

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

... omit getter setter
}
Dao Layer

In the data access layer, you can write an interface inherited from JpaRepository to complete data access, which contains several single-table queries, which is very convenient. It is worth noting that the Account object name is not the specific table name, And Interger is the primary key type, generally Integer or Long

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

In this example, I briefly wrote the service layer. In actual development, it cannot be omitted. 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();

    }


}

The Code has passed the postman request test.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.