pom.xml坐標
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis-plus begin --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>${mybatisplus-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatisplus.version}</version> </dependency> <!-- mybatis-plus end --> </dependencies>
yml配置文件集成Mybatis-puls
mybatis-plus:
#mapper-locations: classpath:/mapper/*Mapper.xml
#實體掃描,多個package用逗號或者分號分隔
typeAliasesPackage: #填寫實體類路徑
global-config:
id-type: 1 #0:數據庫ID自增 1:用戶輸入id
db-column-underline: false
refresh-mapper: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: true #配置的緩存的全局開關
lazyLoadingEnabled: true #延時加載的開關
multipleResultSetsEnabled: true #開啟延時加載,否則按需加載屬性
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql語句,調試用
啟動類注解
@SpringBootApplication @MapperScan("dao的包路徑") //配置mapper包掃描 @EnableEurekaClient @EnableFeignClients public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); }
創建config包和MyBatisPlusConfig類
@Configuration public class MyBatisPlusConfig { //分頁插件 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
controller層,模擬對用戶進行分頁查詢
@RequestMapping(value = "search/{page}/{size}", method = RequestMethod.POST) public Result findBypage(@PathVariable Integer page, @PathVariable Integer size, @RequestBody Map<String, Object> map) { // @PathVariable Integer page, @PathVariable Integer size 是條件 // @RequestBody Map<String, Object> map 本應該使用pojo來進行接收遍歷,但是需要使用反射的方式,成本高,效率低, // 而是用map 直接遍歷 //根據條件分頁查詢 Page<User> userPage = userService.findByPage(map, page, size); //封裝分頁返回對象 PageResult<Article> pageResult = new PageResult<>( userPage.getTotal(), userPage.getRecords() ); //返回數據 return new Result("查詢成功", pageResult); }
1 service層 , 對map集合進行處理
public Page<Article> findByPage(Map<String, Object> map, Integer page, Integer size) { //設置查詢條件 EntityWrapper<User> wrapper = new EntityWrapper<>(); Set<String> keySet = map.keySet(); //在多條件查詢中 ,如果字段為空,就不加入條件查詢中,如果有值那就加入條件查詢中 for (String kye : keySet) { /* if (map.get(kye) != null) { wrapper.eq(kye, map.get(kye)); }*/ //實現了動態sql wrapper.eq(map.get(kye) != null, kye, map.get(kye)); } //設置分頁參數 Page<User> userPage = new Page<>(page, size); //執行查詢 //分頁參數, 查詢條件 List<User> users = userDao.selectPage(userPage, wrapper); userPage.setRecords(users); //返回 return userPage; }
2.Service層 ,因為controller接收的是對象,所以不需要像處理map集合一樣處理
//分頁查詢 public Page<User> selectByPage(User user, Integer page, Integer size) { //設置分頁條件 Page<User> pageData = new Page<>(page, size); //執行分頁查詢 List<User> users = userDao.selectPage(pageData, new EntityWrapper<>(user)); //設置結果集到分頁對象中 pageData.setRecords(users); //返回結果 return pageData; }
dao層直接集成BaseMapper即可
public interface UserDao extends BaseMapper<User> {
}