首先添加maven依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.1.6</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>1.0.5</version> </dependency>
application.yml配置:
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
typeAliasesPackage: com.itmayiedu.entity
global-config:
id-type: 0
field-strategy: 2
##允许下划线
db-column-underline: true
##允许大写
capital-mode: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用
添加MybaticsConfig:
package com.itmayiedu.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisConfig {
// /**
// * mybatis-plus SQL执行效率插件【生产环境可以关闭】
// */
// @Bean
// public PerformanceInterceptor performanceInterceptor() {
// return new PerformanceInterceptor();
// }
/**
* 分页插件
*
* @return
* @author zhaocheng
* @date 2018年4月14日下午4:13:15
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持,也可以关闭,plus自带分页
return paginationInterceptor;
}
}
编写dao层,RoleMapper:
package com.itmayiedu.mapper; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.itmayiedu.entity.Role; public interface RoleMapper extends BaseMapper<Role> { }
编写service层:
RoleService
package com.itmayiedu.service; import com.baomidou.mybatisplus.service.IService; import com.itmayiedu.entity.Role; public interface RoleService extends IService<Role> { }
RoleServiceImpl
package com.itmayiedu.service.Impl; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.itmayiedu.entity.Role; import com.itmayiedu.mapper.RoleMapper; import com.itmayiedu.service.RoleService; import org.springframework.stereotype.Service; @Service public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService { }
controller:
package com.itmayiedu.controller; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.plugins.pagination.PageHelper; import com.itmayiedu.entity.Role; import com.itmayiedu.entity.User; import com.itmayiedu.service.RoleService; import com.itmayiedu.service.UserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @Slf4j public class TestController { @Autowired private UserService userService; @Autowired private RoleService roleService; @RequestMapping("/testString") public String testString(){ log.info("testString"); return "springboot01 test"; } @Transactional @RequestMapping("/insert") public Integer insertRole(String role_name,String note){ Integer a = userService.insertRole(role_name, note); int b = 100/Integer.valueOf(note); return a; } //使用mybaticsPLUS分页 @RequestMapping("/testSql") public void testSql(String name){ EntityWrapper<Role> ew = new EntityWrapper<Role>(); Page<Role> page = new Page<>(1, 2); ew.andNew("note="+name); Page<Role> userPage = roleService.selectPage(page, ew); for(Role role:userPage.getRecords()){ log.info(role.getRole_name()); } System.out.println(ew.getSqlSegment()); } //使用PageHelper分页 @RequestMapping("/testPage") public void testPage(String name){ // Page page = PageHelper.startPage(size, 2); PageHelper.startPage(1, 2); EntityWrapper<Role> ew = new EntityWrapper<Role>(); ew.andNew("note="+name); List<Role> data = roleService.selectList(ew); for(Role role:data){ log.info(role.getRole_name()); } } }