In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "SpringBoot2 how to integrate JPA persistence layer framework, simplify database operations", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "SpringBoot2 how to integrate JPA persistence layer framework, simplify database operations" this article.
A brief introduction to the JAP framework
JPA (Java Persistence API) means Java persistence API, which is the Java persistence specification put forward by Sun official after JDK5.0. The main purpose is to simplify the persistence layer development and integrate ORM technology, and put an end to the situation of separate ORM frameworks such as Hibernate, TopLink, JDO, etc. JPA is developed on the basis of absorbing the existing ORM framework, which is easy to use and flexible.
Integration with SpringBoot2 1, core dependence on org.springframework.boot spring-boot-starter-data-jpa2, 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 configuration instructions for ddl-auto
1) create
Each time hibernate is loaded, the last generated table is deleted, and then the new table is generated again according to the bean class, which can easily lead to data loss (recommended for first creation).
2) create-drop
Each time hibernate is loaded, the table is generated according to the bean class, but as soon as sessionFactory is closed, the table is automatically deleted.
3) update
The structure of the table is automatically established according to the bean class when the hibernate is loaded for the first time, and the table structure is automatically updated according to the bean class when the hibernate is loaded later. Even if the table structure changes, the rows in the table still exist and the previous rows will not be deleted.
4) validate
Each time the hibernate is loaded, the validation creates the database table structure, which is only compared to the tables in the database, and no new tables are created, but new values are inserted.
3. Entity class object
Is the table structure generated from this object.
@ Table (name = "t_user") @ Entitypublic class User {@ Id @ GeneratedValue private Integer id; @ Column private String name; @ Column private Integer age; / / omitting GET SET} 4, the usage of JPA framework
Defines the interface for the operation of the object, inheriting the 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;@Repositorypublic interface UserRepository extends JpaRepository {/ / conditional query User findByAge (Integer age); / / multiple conditional query User findByNameAndAge (String name, Integer age) / / Custom query @ Query ("from User u where u.name=:name") User findSql (@ Param ("name") String name);} 5. Encapsulate a service layer logic import com.boot.jpa.entity.User;import com.boot.jpa.repository.UserRepository;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Servicepublic class UserService {@ Resource private UserRepository userRepository / / Save public void addUser (User user) {userRepository.save (user);} / / query public User findByAge (Integer age) {return userRepository.findByAge (age) by age;} / / query public User findByNameAndAge (String name, Integer age) {return userRepository.findByNameAndAge (name,age) by multiple conditions } / / Custom SQL query public User findSql (String name) {return userRepository.findSql (name);} / / modify public void update (User user) {userRepository.save (user) according to ID;} / / delete a piece of data public void deleteStudentById (Integer id) {userRepository.deleteById (id) according to id;}} third, test code 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=' smiled', 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 (); / / if the primary key does not exist, user.setId (3) will be added to the library by adding the primary key (3); user.setName ("ha ha smile"); user.setAge (25); userService.update (user) } @ Test public void deleteStudentById () {userService.deleteStudentById (5);}} these are all the contents of the article "how SpringBoot2 integrates the JPA persistence layer framework and simplifies database operations". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.