Speedment Introductory Tutorials

Source: Internet
Author: User

Speedment is based on JAVA8 ORM Framework, compared to hibernate and mybatis you only have very little code to achieve the operation of the database, and automatically help you optimize the SQL based on the query, developers do not need to write SQL code

Create the MAVEN project first, then add dependencies in Pom.xml:

  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId> mysql-connector-java</artifactid>
      <version>5.1.42</version>
      <scope>runtime< /scope>
    </dependency>
    <dependency>
      <groupId>com.speedment</groupId>
      <artifactId>runtime</artifactId>
      <version>${speedment.version}</version>
      <type>pom</type>
    </dependency>
  </dependencies>

After importing the dependency pack, under the Pom.xml directory, run the MAVEN command, MVN Speedment:tool eject the initialization tool, and generate the template Speedment.json file under your project directory

According to the Graphics tool prompts, enter the connection information, select DB, and then click Generate, will generate the base class, to your current project directory

Initialize load, Spring boot project as example:

@Configuration public
class Setup {

    @Bean public
    dms2application createapplication () {return
        new Dms2applicationbuilder ()
                . Withpassword ("root")
                . Withlogging (Logtype.stream_optimizer)
                . Build ();
    
    @Bean public
    Jackson2objectmapperbuilder Jacksonbuilder () {return
        new Jackson2objectmapperbuilder (). Indentoutput (True);
    }

The code above, is my initial configuration of the DMS2 database, the reader initialization, according to the generated code, the name of their own database, the format is almost

Speedment.json will only retain your basic information, will not keep your password and other important information, so you need to initialize the database password, of course, in the initialization you can configure more information, such as thread pool, log information, etc., please consult the documentation

Once the initialization is complete, you can start using speedment to start the operation on the database.

Accountmanager Accountmanager;
    Private Accountservice (Dms2application app) {Accountmanager = App.getorthrow (Accountmanager.class); /** * New Deutsch User * @return * * * The Public Account Add () () () () = new Accoun
        Timpl (). Setusername ("Deutsch");
    return accountmanager.persist (account); /** * Find Deutsch User and modify the name deutsch-h * @return * * Public account edit () {optional& Lt
        account> Optaccount = Accountmanager.stream (). Filter (Account.USER_NAME.equal ("Deutsch")). FindFirst ();
        Optaccount.map (account-> {return accountmanager.update (Account.setusername ("deutsch-h"));
        });
    return null;  /** * Find deutsch-h User and delete/public void Delete () {optional<account> Optaccount =
        Accountmanager.stream (). Filter (Account.USER_NAME.equal ("Deutsch-h")). FindFirst (); Optaccount.ifpresent (a-> {accOuntmanager.remove (a);
    }); }

The above is speedment simple additions and deletions to check, are through the java.util.stream to operate, very convenient

Of course, Speedment also support more powerful queries, we only do a simple introduction, such as a multiple-conditional complex query, you can define the predicate<t>, and then passed into the. Filter to flexibly handle

	predicate<account> queryparams = Account.STATUS.notEqual (DataStatusEnum.DELETE.value);
        
        if (Stringutil.isnotempty (UserName)) {
            queryparams = Queryparams.and (Account.USER_NAME.equal (UserName));
        
        if (Stringutil.isnotempty (LoginName)) {
            queryparams = Queryparams.and (Account.LOGIN_NAME.equal (LoginName));

Dynamic query conditions, the construction of paging
	list<account> data = Accountmanager
		. Stream ()
		. Filter (Queryparams)
		. Skip (Accountreqpaging.getstartindex ())
		. Limit (PageSize)
		. Collect (Collectors.tolist ());

The above is simply used, more offset, offset and Limit, Count, Group by, gaving, Distinct are easy to implement

For more Java combat knowledge, please scan two-dimensional code, focus on getting more technical knowledge

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.