本作品采用 知识共享署名 4.0 国际许可协议 进行许可。
本文作者:低调小熊猫
文章链接: https://aodeng.cc/archives/springbootba
转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032
<!--通用mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> <!-- MYSQL包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources>
spring: datasource: url: password: username: # 如果想看到mybatis日志需要做如下配置 logging: level: com.hope: DEBUG ########## Mybatis 自身配置 ########## mybatis: mapper-locations: classpath:/mapper/*.xml type-aliases-package: com.hope.model # 驼峰命名规范 如:数据库字段是 order_id 那么 实体字段就要写成 orderId #mybatis.configuration.map-underscore-to-camel-case=true ########## 通用Mapper ########## # 主键自增回写方法,默认值MYSQL,详细说明请看文档 mapper: identity: MYSQL # 设置 insert 和 update 中,是否判断字符串类型!='' not-empty: true # 枚举按简单类型处理 enum-as-simple-type: true ########## 分页插件 ########## pagehelper: helper-dialect: mysql params: count=countSql reasonable: false support-methods-arguments: true
import org.apache.ibatis.annotations.Mapper; import tk.mybatis.mapper.common.BaseMapper; @Mapper public interface SysRoleMapper extends BaseMapper<SysRole>{ }
RunWith(SpringRunner.class) @SpringBootTest public class SysRoleServiceImplTest { private static final Logger log = LoggerFactory.getLogger(SysRoleServiceImplTest.class); @Autowired private SysRoleServiceImpl sysRoleService; @Test public void test1(){ List<SysRole> sysRoleList=sysRoleService.SelectAll(); log.info("[普通写法] - [{}]", sysRoleList); //分页 PageInfo<Object> pageInfo= PageHelper.startPage(1,1).doSelectPageInfo(() -> sysRoleService.SelectAll()); log.info("[分页]-[{}]",pageInfo); } }
private static final Logger log = LoggerFactory.getLogger(SysRoleServiceImplTest.class); log.info("[分页]-[{}]",pageInfo);
PageInfo<Object> pageInfo= PageHelper.startPage(1,1).doSelectPageInfo(() -> sysRoleService.SelectAll());