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

The method of persistent Storage of Spring Data JPA data to Database

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about the method of persistent storage of Spring Data JPA data to the database. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

1 Core concept

The central interface for Spring Data repository abstraction is Repository. It takes the entity class to be managed and the ID type of the entity class as type parameters. This interface is mainly used as a tagging interface to capture types at work and to help you find interfaces that extend the interface. The CrudRepository interface provides complex CRUD functions for managed entity classes.

You can take a look at the extension interface of Repository and the implementation class IDEA to position the cursor in the Repository.java file, and the ctrl+h shortcut key can be seen.

The inheritance relationship of the class diagram is as follows:

CrudRepository interface

This interface defines a set of methods for basic operation of CRUD, which is easy to use.

What's more powerful about the CrudRepository interface is that it can derive methods. What do you mean, for example?

You is a table of users, and this implementation is really powerful if you want to count the numbers by a certain field.

Public interface MemberMapper extends CustomRepository {/ * * derived method counts the number by user name * / Long countByMemberName (String username); / * * derived method counts the number by user department number * / Long countByDeptId (Integer deptId);} PagingAndSortingRepository API

On top of CrudRepository, there is an abstraction of PagingAndSortingRepository, which adds additional methods to facilitate paging query access to entity classes

2 query method

Standard CRUD functional libraries usually have queries for the underlying data stores. Using Spring Data, declaring these queries requires four steps:

1] declare an interface that extends Repository or one of its subinterfaces, and specify the entity classes and ID types it should handle

Interface MemberRepository extends Repository {... }

2] declare the query method in the interface

Interface MemberRepository extends Repository {List findByMembername (String username);}

3] set Spring to create proxy instances for these interfaces, which can be implemented by using configuration classes or xml configuration files

By configuring the class, the example is as follows:

Import org.springframework.data.jpa.repository.config.EnableJpaRepositories;@EnableJpaRepositoriespublic class MyConfig {}

Through the xml configuration file, the example is as follows:

The JPA namespace is used in this example. If you use repository abstraction for any other storage, you need to modify it to the corresponding namespace declaration for your storage module. In other words, you should replace jpa with other storage types, such as mongodb.

Also, note that the JavaConfig configuration class does not explicitly configure the scanned base package, because the package of the annotation class is used by default. To customize the packages to be scanned, use the basePackage of @ Enable$ {store} Repositories-annotation of the data store specific library. One of the properties. What do you mean, for example:

If your store type is mongodb, use @ EnableMongoRepositories

@ Configuration@EnableMongoRepositories (* arrayOf ("com.kkarma.repository", "com.???.???")) class PersistenceConfig: AbstractMongoConfiguration () {}

If your store type is redis, use @ EnableRedisRepositories

If your store type is jpa, use @ EnableJpaRepositories

Simply configure a single package in the following format:

@ EnableJpaRepositories ("com.spr.repository")

Simple configuration supports multiple package in the following format:

@ EnableJpaRepositories ({"com.cshtong.sample.repository", "com.cshtong.tower.repository"})

4] inject the Repository instance and use it

@ Servicepublic class MemberServiceImpl implements MemberService {private final MemberMapper memberMapper; public MemberServiceImpl (MemberMapper memberMapper) {this.memberMapper = memberMapper;} @ Override public ApiResponse insertMember (Member member) {Member user = memberMapper.save (member); return ApiResponse.success ("added success", user);}} these are all the contents of the article "how to persist Spring Data JPA data to the database". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.

Share To

Development

Wechat

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

12
Report