GithubHelp home page GithubHelp logo

jpa-findbyexample's Introduction

FindByExample - Using Spring Data JPA

A good example for how to do a findByExample in JPA. The BaseExample extend the JPA Specification interface.

e.g:

    public static void main(String[] args) {
        ContactExample example =
                ContactExample
                        .where()//default is and
                        .id.eq(1l)
                        .or()
                        .birthday.lt(new Date())
                        .name.like("%", "name");
        //this example is "where id =1 or( birthday < #birthday# and name like '%#name#')"
        syntax(example);
    }
    // search by interface JpaSpecificationExecutor 
    public static void syntax(ContactExample example) {
        //init by springcontext
        ContactRepository repository = null;
        
        Contact contact = repository.findOne(example);

        List<Contact> list = repository.findAll(example);

        Page<Contact> page = repository.findAll(example, new PageRequest(0, 10));

        Page<Contact> pageAndSort = repository.findAll(example, new PageRequest(0, 10, new Sort("id")));

    }

Usage:

Create Entity Bean and JpaRepository(extends JpaSpecificationExecutor):

    @Entity
    public class Contact {
        @Id
        @GeneratedValue
        private Long id;
        @Column
        private String name;
        @Column
        private Date birthday;
        //Getter and Setter
    }
    public interface ContactRepository
            extends
            JpaSpecificationExecutor<Contact> {
    }

Create your own Example:

    public class ContactExample extends BaseExample<ContactExample, Contact> {
        public final Attr<Long> id = new Attr<Long>("id");
        public final Attr<String> name = new Attr<String>("name");
        public final Attr<Date> birthday = new Attr<Date>("birthday");
        //default builder  
        public static ContactExample where() {
            ContactExample example = new ContactExample();
            example.operatorType = OperatorType.and;
            return example;
        }
    }

API:

invoke by method chaining

    
    public static void main(String[] args) {
        ContactExample

        .where()//default is and

            .id.eq(1l)
            .id.notEq(1l)

            .id.gt(1l)
            .id.gtEq(1l)

            .id.lt(1l)
            .id.ltEq(1l)

            .id.in(1l)
            .id.in(1l,2l)
            .id.in(new Long[]{})
            .id.in(new ArrayList())

            .id.notIn(1l)

            .id.between(1l,2l)

            .id.isNull()
            .id.isNotNull()

        .and()
            .name.like("%name%")
            .name.like("%",name)
            .name.like(name,"%")
                //...
        .or()
                //...
            ;
    }

invoke by pojo

create pojo query
    public class ContactQuery{
        private Long id;
        private Long idGt;
        private Long idIn;
        private Long idBetween;
        //Getter and Setter
    }
fill example by pojo
    ContactQuery query = new ContactQuery();
    query.setId(1l);
    query.setIdGt(100l);
    query.setIdIn(new Long[]{1l,2l,3l});
    query.setIdBetween(new Long[]{1l,3l});

    ContactExample
        .where()
        .fill(query)
fill by your own method
    ContactQuery query = new ContactQuery();
    query.setId(1l);
    query.setIdGt(100l);
    query.setIdIn(new Long[]{1l,2l,3l});
    query.setIdBetween(new Long[]{1l,3l});

    ContactExample
        .where()
        .fill(query, 
            new FillCondition() {
                @Override
                public void fill(BaseExample baseExample, Object query) {
                    //your code here
                }
            });

Example to JPQL:

  • jpql : from Contact where id <200 and id > 100
    ContactExample.where()
        .id.lt(200l)
        .id.gt(100l)
  • jpql : from Contact where id <200 and id > 100 and( id = 150 or id = 160 )
    ContactExample.where()
            .id.lt(200l)
            .id.gt(100l)
        .or()
            .id.eq(150l)
            .id.eq(160l)

TODO list:

  • group by
  • SubQuery

jpa-findbyexample's People

Contributors

xiaod0510 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.