Springboot multiple data sources using the paging plug-in Pagehelper

Source: Internet
Author: User

Previously, only the paging plug-ins under the single data source were used, and almost no configuration was necessary. A static method can be done.

Pagehelper.startpage (Pagenum, pageSize);

Later using a multi-data source (different database), the Pagehelper paging plugin needs to set a default database, MySQL, Oracle, or other.
However, it is not possible to switch in real-time, resulting in requests for the first type of database requested to continue the request, while requests from other databases are different because the SQL statements differ from each other in the count and paging statements in different databases.

The solution to this idea is
The ① configuration configures multiple data sources first, and then configures multiple sqlsessionfactory to use different data sources . sqlsessionfactory also specifies some DAO layer interfaces (or mapper), at which point different DAO layers can access different data sources
② Configuring a paging plug-in in each sqlsessionfactory

Multi-data source configuration in the first step many of the posts have a record, which is not repeated here, I configured the reference is the link
http://blog.csdn.net/neosmith/article/details/61202084
He provides a multi-data source manual configuration, an automatic configuration scheme. Because we want to configure the paging plug-in separately under the multi-data source, we choose the manual configuration scheme.


Focus on the second step:
One, multi-data source under the configuration of the paging plug-in
If you use the top configuration scheme, then you should now have multiple Sqlsessionfactory's Bean. Let's focus on The configuration of a sqlsessionfactory bean:
1 @Configuration2@MapperScan (basepackages = {"Com.firstRest.dao.localLeftjointest"}, Sqlsessionfactoryref = "SqlSessionFactory1")3  Public classMybatisdbaconfig {4 5 @Autowired6@Qualifier ("Localleftjointestdatasource")7     PrivateDataSource DS1;8 9 @BeanTen      PublicSqlsessionfactory SqlSessionFactory1 ()throwsException { OneSqlsessionfactorybean Factorybean =NewSqlsessionfactorybean (); AFactorybean.setdatasource (DS1);//connect the Leftjointest library using localleftjointest data sources -  -         //Paging Plugin theInterceptor Interceptor =Newpageinterceptor (); -Properties Properties =NewProperties (); -Properties.setproperty ("Helperdialect", "MySQL"); -Properties.setproperty ("Offsetaspagenum", "true"); +Properties.setproperty ("Rowboundswithcount", "true"); -Properties.setproperty ("reasonable", "true"); +Properties.setproperty ("Supportmethodsarguments", "true"); AProperties.setproperty ("params", "Pagenum=pagenumkey;pagesize=pagesizekey;")); at Interceptor.setproperties (properties); -Factorybean.setplugins (Newinterceptor[] {interceptor}); -  -         returnFactorybean.getobject (); -  -     } in  - @Bean to      PublicSqlsessiontemplate sqlSessionTemplate1 ()throwsException { +Sqlsessiontemplate template =NewSqlsessiontemplate (SqlSessionFactory1 ());//Use the factory configured above -         returntemplate; the     } *}

The extra code is where we need to add, so let's see what we've done:
① creates a new pageinterceptor because MyBatis allows interception calls at a point during the execution of a mapped statement, and Pagehelper is the paging operation.
② Create a new property and add some property values that are important in the values:
  
(1) Helperdialect database dialect: Whatever the database is, just write it. mysql, SQL Server, Oracle, DB2 and more, refer to https://github.com/pagehelper/ Mybatis-pagehelper/blob/master/wikis/en/howtouse.md
(2) Whether the supportmethodsarguments supports the parameter method of paging: Write true on the line, the reason behind the write
(3) params supports those parameters: Pagenum=pagenumkey;pagesize=pagesizekey. The paging page number, paging size, respectively.

Note that some of the posts are written in the ① step to create a new Pagehelper object, set the properties of Pagehelper, and then add to the plugins inside, and now does not support this way!      now we're going to use pageinterceptor!. (2) (3) is set is the plug-in is configured with the scheme, if so, in the DAO layer (or Mapper) interface also appears pagenumkey and Pagesizekey parameters, this method will be paged. Why use this method, we later said that the first to write this is right.
 single sqlsessionfactory configured, other   helperdialect to a different data source database type. 


Ii. How to write the DAO layer interface
top we set up Span style= "font-size:14px" >supportmethodsarguments and params, how do we make the paging plugin take effect when we call the DAO layer method? The
code is as follows:

 1   @Repository  2  public   Interface   Jlzxdao { 3  @Select (value     = "SELECT * FROM ${table_name} ORDER by id" )  4  @ResultType (Hashmap. Class  )  5  list 6   @Param ("Pagenumkey") int pagenum,7 @Param ("Pa Gesizekey ") int   pageSize );  8 } 
Focus on the red parameters, I did not use these two parameters in SQL, but the two parameters written in. This way the Pagenumkey and Pagesizekey parameters are present, and the method is paged.

In this way, when the DAO layer is called, more than two paging parameters can be automatically paged out in the past.

Third, why use this kind of parameter method uses pagination
In a single data source, I used a static method, that is, the first line of the code to mark, so that the next query statement paging.
But when using this static method, the following statement is used at the service layer (or controller layer) to determine where the source of the data came from, and this is what I did before the service layer:
1@Service ("Baseservice")2  Public classBasepagingserviceImplementspagingservice{3 @Autowired4     PrivateJlzxdao Jlzxdao;5 @Autowired6     PrivateLeftjointestdao Leftjointestdao;7 8 @Override9      PublicPageinfointCurrentPage,intpageSize) {Ten         if(Dbname.equals ("Leftjointest")){ One pagehelper.startpage (CurrentPage, pageSize); A             return NewPageinfo<>(Leftjointestdao.getall (TableName)); -         } -         Else if(Dbname.equals ("JLZX")){ the pagehelper.startpage (CurrentPage, pageSize); -             return NewPageinfo<>(Jlzxdao.getall (TableName)); -         } -         return NULL; +  -     } +}

This method will error: found in the system of multiple paging plug-ins, please check the system configuration!

  So change the parameters to write directly to the use of the DAO layer method.




My Project link is as follows:
Https://github.com/Jaccccccccccccccccccccccccccccck/firstREST

If there is a mistake, also look correct!

  

  

Springboot a multi-data source using a paging plug-in Pagehelper

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.