Springboot MyBatis Integrated Mapper and Pagehelper

Source: Internet
Author: User

1. Introducing Jar to Pom.xml

  

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>

<!--Https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper--
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>

2, Configuration Mapper

Create Application-bean.xml, file contents as follows

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd ">

<!--inject the bean that spring boot cannot scan. -
<bean class= "Tk.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "study.boot.config. Mymapper "/>
<property name= "Properties" >
<value>
Mappers=tk.mybatis.mapper.common.mapper
</value>
</property>
</bean>

</beans>

3. Create a configuration class

  

Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource (value={"Classpath:application-bean.xml"})
public class Configclass {

}

Import java.util.Properties;

Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

Import Com.github.pagehelper.PageHelper;
@Configuration
public class Mybatisconfiguration {

private static final Logger Logger = Loggerfactory.getlogger (Mybatisconfiguration.class);

@Bean
Public Pagehelper Pagehelper () {
Logger.info ("Register MyBatis page plug-in Pagehelper");
Pagehelper pagehelper = new Pagehelper ();
Properties P = new properties ();
P.setproperty ("Offsetaspagenum", "true");
P.setproperty ("Rowboundswithcount", "true");
P.setproperty ("reasonable", "true");
Pagehelper.setproperties (P);
return pagehelper;
}
}

Import Tk.mybatis.mapper.common.Mapper;
Import Tk.mybatis.mapper.common.MySqlMapper;

Public interface mymapper<t> extends Mapper<t>, mysqlmapper<t> {
}

4. Practical Application

MyBatis's mapper Inherits General Mapper

@Mapper
Public interface Citymapper extends mymapper<city>{

}

Direct use

@RestController
@RequestMapping ("/Boot")
public class HelloWorld {

@Autowired
Citymapper Citymapper;

@RequestMapping ("/hello")
Public list<city> Greeting () {
Pagehelper.startpage (1, 5);
return Citymapper.selectall ();
}

}

Springboot MyBatis Integrated Mapper and 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.