Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot integrates JPA

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how SpringBoot integrates JPA. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

JPA full name Java Persistence API.JPA describes object-relational table mapping through JDK 5.0 annotations or XML, and persists 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 instead of using a proprietary vendor-specific API.

JPA needs Provider to achieve its function, Hibernate is a very strong one of JPA Provider, it should be said that no one is second to none. Functionally, JPA is a subset of Hibernate functionality.

Add related dependencies

Add spring-boot-starter-jdbc dependencies:

Org.springframework.boot spring-boot-starter-data-jpa

Add mysql connection classes and connection pooling classes:

Mysql mysql-connector-java runtime configure the data source, in the application.properties file configuration: 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 # the first profile create is followed by update show-sql: true

Note that if you create a table in the database through jpa, change jpa.hibernate,ddl-auto to create, and change it to update after the table is created, otherwise the table will be deleted and created each time the project is restarted.

Create an entity class

The @ Entity indicates that it is a mapped entity class, and @ Id indicates that the id and @ GeneratedValue fields are automatically generated.

@ Entitypublic class Account {@ Id @ GeneratedValue private int id; private String name; private double money;... Omit getter setter} Dao layer

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

Public interface AccountDao extends JpaRepository {} Web layer

In this chestnut, I briefly wrote the service layer, which can not be omitted in actual development. Write a new controller and write a few restful api to test data access.

@ RestController@RequestMapping ("/ account") public class AccountController {@ Autowired AccountDao accountDao; @ RequestMapping (value = "/ list", method = RequestMethod.GET) public List 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 ();}} this is the end of the article on "how SpringBoot integrates JPA". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report