How to customize the DAO method in repository under Spring boot

Source: Internet
Author: User
Tags table definition

Introduction to Environment Configuration

JDK 1.8, Spring Boot 1.5.3.RELEASE, MySQL, Spring Data, JPA

Problem description

Spring data provides a simple and easy-to-use DAO layer abstraction and encapsulation, covering the basic functions of curd, but in many cases, users need to customize the DAO implementation method to achieve more complex and granular database access operations, how to solve this problem?

Target description

Here we take a custom testaa approach as an example to show how to implement a custom DAO method extension.

Definition of a database table

Here we define a very simple mycity table, as an example of the entity class BaseEntity:
database table Definition:

Import Java.util.Date;Import Javax.persistence.Column;Import Javax.persistence.GeneratedValue;Import Javax.persistence.GenerationType;Import Javax.persistence.Id;Import Javax.persistence.MappedSuperclass;Import javax.persistence.Temporal;Import Javax.persistence.TemporalType;Import javax.persistence.Version;@MappedSuperclassPublicAbstractClassBaseEntityImplementsJava.Io.Serializable {PrivateStaticFinalLong Serialversionuid =-2420979951576787924L;@Id @GeneratedValue (strategy=generationtype.identity) @ Column (name =  "id") private Long ID;  @Version private Long Version;  @Temporal (temporaltype.timestamp)  @Column (name = Span class= "hljs-string" > "Create_time", Columndefinition= "timestamp default Current_ TIMESTAMP ") private Date createtime;  @Temporal (temporaltype.timestamp)  @Column (name = Span class= "hljs-string" > "Update_time", Columndefinition= "timestamp default Current_ TIMESTAMP on UPDATE current_timestamp ") private Date updatetime;}     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

Mycity is defined as follows:

Import Javax.persistence.Column;Import javax.persistence.Entity;Import javax.persistence.Table;Import Lombok. Data;@Entity@Table (name="Mycity")@DataPublicclass city extends baseentity {private static final long SerialVersionUID =-< Span class= "Hljs-number" >7510771121759944670l;  @Column (Name= "name") private String name;  @Column (Name= "Country_code") private String CountryCode;  @Column private String District;  @Column private int population;}                
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24

The @data here uses the powerful annotations provided by Lombok to simplify the use of redundant getter/setter methods.

Define Repository

Standard Cityrepository.java, where the default method is fully used:

import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;import com.rose.money.City;@Repositorypublic interface CityRepository extends JpaRepository<City, Long>, CityRepositoryCustom{}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

Here Cityrepository inherits 2 parent classes, including user-defined interface classes, which allow user-defined interfaces to leak out.
The cityrepsoitorycustom here defines the user's custom method:

public interface CityRepositoryCustom {    public void testAA();}
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

Notice: Here the custom suffix is the contract, can not be arbitrarily modified.
Implementation classes for custom methods:

Import java.util.List;Import Javax.persistence.EntityManager;Import Javax.persistence.PersistenceContext;Import org.springframework.beans.factory.annotation.Autowired;PublicClassCityrepositoryimplimplements cityrepositorycustom {  @Autowired  @PersistenceContext private Entitymanager Entitymanager;  @Override public void testaa () {list<object[]> cities = entitymanager.createnativequery (  "SELECT ID, Name, district from Mycity"). Getresultlist (); for (object[] objs:cities) {System.out.print ( "Location 1:" + Objs[0]); System.out.print ( "Location 2:" + objs[1]); System.out.print ( "Location 3:" + objs[2]);}}    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23

The implementation class here reads a few records and prints them out. It implements the custom interface class.

Configuration information

Application.properties:

Spring. Application. Name=custom jpaspring. JPA. database=mysqlspring. DataSource. username=rootspring. DataSource. driver-class-name=Com. mysql. jdbc.datasource.password=123456spring.datasource.URL=JDBC: Mysql://localhost:3306/world?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull&allowmultiqueries=true&usessl=truespring.jpa.hibernate.strategy=org.hibernate.cfg.jpa.show-sql=true  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
Test

Test Case:

import org. junit. Test; Import org. junit. Runner. Runwith; Import org. springframework. Beans. Factory. annotation. Autowired; Import org. springframework. Boot. Test. Context. Springboottest; Import org. springframework. Test. Context. junit4. Springrunner;import com.rose.money.repository.class) @ Springboottestpublic class Customjpaapplicationtests {@Autowired private cityrepository cityrepo; @Test public void Contextloads () {City city = Cityrepo.findone (1l" ; System.out.println ( " City=> "+ City" .testaa () ; }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22

The result of the test is illustrated:

Summarize

Convention is greater than configuration, custom suffix implementation and extension, very simple and practical.

http://www.woaipu.com/shops/zuzhuan/61406
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117777
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117890
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117994
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=118376

How to customize the DAO method in repository under Spring boot

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.