Springboot introduction of Mybaits and paging plugins

Source: Internet
Author: User

First, Pom

        <Dependency>            <groupId>Org.mybatis.spring.boot</groupId>            <Artifactid>Mybatis-spring-boot-starter</Artifactid>            <version>1.1.1</version>        </Dependency>        <!--Paging Plugin -        <Dependency>            <groupId>Com.github.pagehelper</groupId>            <Artifactid>Pagehelper</Artifactid>            <version>3.7.5</version>        </Dependency>

Second, the configuration

Configure the data source, specify the location of the configuration file, and the location of the mapper file

 PackageCn.net.leadu.config;Importorg.springframework.context.annotation.Configuration;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.tomcat.jdbc.pool.DataSource;ImportOrg.mybatis.spring.SqlSessionFactoryBean;ImportOrg.springframework.beans.factory.annotation.Qualifier;Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.core.io.support.PathMatchingResourcePatternResolver; @Configuration Public classmybatisconfiguration {//load the datasource configuration file@Bean @ConfigurationProperties (prefix= "Spring.datasource") @Qualifier ("Datasourcemybatis")     PublicDataSource Datasourcemybatis () {return NewDataSource (); }      //Load Sqlsessionfactory@Bean (name = "Sqlsessionfactory")     PublicSqlsessionfactory Sqlsessionfactorybean ()throwsException {Sqlsessionfactorybean Sqlsessionfactorybean=NewSqlsessionfactorybean ();        Sqlsessionfactorybean.setdatasource (Datasourcemybatis ()); Pathmatchingresourcepatternresolver Resolver=NewPathmatchingresourcepatternresolver (); Sqlsessionfactorybean.setconfiglocation (Resolver.getresource ("Classpath:mybatis-config.xml")); //Configure MyBatis Scan to find all *mybatis.xml mapping filesSqlsessionfactorybean.setmapperlocations (Resolver.getresources ("Classpath:mybatis/*.xml")); returnSqlsessionfactorybean.getobject (); }}

Note the configuration of the data source in the configuration file

Note (prefix= "Spring.datasource") is consistent with the configuration file

Spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driverClassName=  Com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://Localhost:3306/test?useunicode=true &characterencoding=utf8spring.datasource.username=rootspring.datasource.password=xxx

Mybatis-config.xml Configuring the paging plug-in, displaying SQL statements

<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Configuration Public"-//mapper.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><configuration> <s ettings> <!--print query statement--<setting name= "Logimpl" value= "stdout_logging"/> &LT;/SETTINGS&G T <plugins> <!--Com.github.pagehelper is Pagehelper class package name--<plugin interceptor= "Com.github. Pagehelper.              Pagehelper "> <!--4.0.0 later versions can not set this parameter--<property name=" dialect "value=" MySQL "/>              <!--this parameter defaults to false--< when!--is set to True, the Rowbounds first parameter offset is used as the Pagenum page number--              <!--and startpage in Pagenum effect--<property name= "Offsetaspagenum" value= "true"/> <!--this parameter defaults to False-<!--is set to True when you use Rowbounds paging to count queries--<property name= "ro              Wboundswithcount "value=" true "/> <!--set to True, if pagesize=0 or rowbounds.limit = 0 will query out all results-- <! --(equivalent to not performing a paged query, but the return result is still page type)--<property name= "Pagesizezero" value= "true"/> <!-              -3.3.0 available-Paging parameter rationalization, default false disable-<!--when rationalized is enabled, if pagenum<1 queries the first page, if pagenum>pages queries the last page- <!--disable rationalization, if pagenum<1 or pagenum>pages returns empty data--<property name= "reasonable" value= "False"/> <!--3.5.0 version available-to support StartPage (Object params) method-<!--adds a ' params ' parameter to match Parameter mapping, which can be used to take values from a map or ServletRequest-<!--configurable Pagenum,pagesize,count,pagesizezero,reasonable,orderby, no mapping Use default value-<!--do not understand the meaning of the premise, do not casually copy the configuration-<property name= "params" value= "Pagenum=star T;pagesize=limit; " /> <!--supports passing paging parameters via mapper interface Parameters--<property name= "supportmethodsarguments" value= "tr UE "/> <!--always returns the PageInfo type, check checks whether the return type is Pageinfo,none returns page-to-<property name = "REturnpageinfo "value=" Check "/> </plugin> </plugins></configuration> 

The location of the scanned package

 PackageCn.net.leadu.config;ImportOrg.mybatis.spring.mapper.MapperScannerConfigurer;ImportOrg.springframework.boot.autoconfigure.AutoConfigureAfter;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration; @Configuration//Note that because Mapperscannerconfigurer executes earlier, it must have the following annotations@AutoConfigureAfter (mybatisconfiguration.class) Public classMybatisconfigurationscanner {@Bean Publicmapperscannerconfigurer Mapperscannerconfigurer () {mapperscannerconfigurer mapperscannerconfigurer=NewMapperscannerconfigurer (); Mapperscannerconfigurer.setsqlsessionfactorybeanname ("Sqlsessionfactory"); Mapperscannerconfigurer.setbasepackage ("Cn.net.leadu.mapper"); returnMapperscannerconfigurer; }}

Springboot introduction of Mybaits and paging plugins

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.