Spring Data JPA defines the global interface basedao__ global interface

Source: Internet
Author: User
Tags stub

OK, now I'm going to build a project, I want to define a Basedao, and then directly inherit the DAO from other modules directly. This Basedao,basedao has a common approach and a handy way for spring data JPA

So create a new Basedao, this basedao should be inherited jparepository,jpaspecificationexecutor because we want to use spring data JPA, and then you define your own common methods, such as MyMethod

@NoRepositoryBean public
Interface Basedao<t,id extends serializable> extends Jparepository<t, Id> jpaspecificationexecutor<t> {

    void MyMethod ();

}

The next step is to achieve

public class Basedaoimpl<t,id extends serializable> extends Simplejparepository<t, id> implements Basedao <t,id>{

    private final entitymanager em; 

    Public Basedaoimpl (class<t> domainclass, Entitymanager em) {
        super (DomainClass, EM);
        This.em = em; 
    }


    @Override public
    void MyMethod () {



    }

}

Inheriting from Simplejparepository not only allows us to get the entitymanager of the Operation database, but also to implement the Jparepository,jpaspecificationexecutor method, So we can just write our own way to achieve

Next, the definition of factory, his role is to throw our Basedao implementation class to Factorybean

The public class Basedaofactory<s, the ID extends serializable> extends jparepositoryfactory {public

    basedaofactory ( Entitymanager Entitymanager) {
        super (Entitymanager);
    }

    @SuppressWarnings ({"Rawtypes", "unchecked", "hiding"})
    @Override
    protected <t, ID extends Serializable > simplejparepository<?,? > gettargetrepository (Repositorymetadata metadata,
            Entitymanager Entitymanager) {
        //TODO auto-generated method stub return
        new Basedaoimpl (Metadata.getdomaintype (), Entitymanager);
    }

    @Override
    protected class<?> getrepositorybaseclass (Repositorymetadata metadata) {
        //TODO auto-generated method Stub return
        Basedao.class
    }

}

Then define a Factorybean, and after receiving factory, throw factory into the spring data JPA

public class Basedaofactorybean<r extends Jparepository<s, Id>, S, ID extends serializable> extends
        Jparepositoryfactorybean<r, S, id> {

    @SuppressWarnings ("Rawtypes")
    @Override
    protected Repositoryfactorysupport createrepositoryfactory (Entitymanager entitymanager) {
        //TODO auto-generated method Stub return
        new Basedaofactory (Entitymanager);
    }

}

Spring data JPA is picked up, configuring your own defined Factorybean in the configuration file, so you can use your Basedao directly.

<jpa:repositories base-package= "Com.liuxg.**.dao"
        repository-impl-postfix= "Impl" 
        Query-lookup-strategy = "Create-if-not-found"
        factory-class= "Com.liuxg.base.dao.BaseDaoFactoryBean
        " entity-manager-factory-ref= "Entitymanagerfactory"
        transaction-manager-ref= "TransactionManager" >
    </jpa:repositories>

And next time we're going to use it, that's it.

Public interface Taskdao extends basedao<task,long>{

}

At the service level, you can use the spring data JPA method and some of the common methods that you define, if you still want to define some methods in your own module, the reference
http://blog.csdn.net/yingxiake/article/details/51017699
And that's what we're going to write.

Public interface Taskdao extends Basedao<task,long>,taskdaocustom<task, long>{

}

Then, after all, there are some things, framework or help us not ah, too complex business, want to write their own database, want to write their own SQL native statements, that how Ah, pro, that can only use the Entitymanager, using the JPA API directly to do their own, Next time, see how you can work with Entitymanager.

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.