springboot 集成mybatis
1:依賴mybaits分頁插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2:啟動類中@MapperScan 掃描接口包
@MapperScan("com.jxd.Boot.mybatis.dao") // 啟用mybatis
@SpringBootApplication //相當於上邊幾個的集合
//@EnableScheduling//開啟定時任務註解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3:mybatis 生成工具生成po mapper .xml
.xml 放到resource下
例如:
4:開啟sql輸出日誌:application.properties中加入:
#打印mybatis日誌
logging.level.com.jxd.Boot.atis.dao=debug
5:測試:
/**
* @Description (測試springboot集成mybatis 分頁)
* @param no
* @param size
* @return
*/
@RequestMapping("/testmybatis")
public Object likeName(@RequestParam(defaultValue = "1") Integer no,
@RequestParam(defaultValue = "10") Integer size) {
MstudentExample ex = new MstudentExample();
PageHelper.startPage(no, size);
Page<Mstudent> list = (Page<Mstudent>) mstudentMapper.selectByExample(ex);
Pager pager = RenderPager.mybatisconverPager(list);
return pager;
}
springboot 集成mybatis