码迷,mamicode.com
首页 > 移动开发 > 详细

springboot mybatis 集成mapper与pagehelper

时间:2017-05-08 01:15:00      阅读:1451      评论:0      收藏:0      [点我收藏+]

标签:app   pom.xml   jar   group   city   autowired   reason   extend   static   

1、引入jar到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、配置mapper

  创建application-bean.xml,文件内容如下

<?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">

<!-- 注入spring boot无法扫描到的bean. -->
<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、创建配置类

  

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("注册MyBatis分页插件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、实际运用

  mybatis的mapper继承通用mapper

@Mapper
public interface CityMapper extends MyMapper<City>{

}

直接使用

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

@Autowired
CityMapper cityMapper;

@RequestMapping("/hello")
public List<City> greeting() {
PageHelper.startPage(1, 5);
return cityMapper.selectAll();
}

}

springboot mybatis 集成mapper与pagehelper

标签:app   pom.xml   jar   group   city   autowired   reason   extend   static   

原文地址:http://www.cnblogs.com/dongyy/p/6823010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!