本文作者:低调小熊猫
转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032
整合demo
配置依赖
tk.mybatis
mapper-spring-boot-starter
2.0.2
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
mysql
mysql-connector-java
配置静态资源文件
src/main/resources
src/main/java
**/*.xml
true
配置application.yml文件
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
然后自己创建model,mapper,xml,service
通用mapper的使用
import org.apache.ibatis.annotations.Mapper;
import tk.mybatis.mapper.common.BaseMapper;
@Mapper
public interface SysRoleMapper extends BaseMapper{
}
junitTest
idea快捷键生成junitTest
打开要测试的类,如果选择idea工具栏的Navigate,然后点击Test就行了
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 sysRoleList=sysRoleService.SelectAll();
log.info("[普通写法] - [{}]", sysRoleList);
//分页
PageInfo pageInfo= PageHelper.startPage(1,1).doSelectPageInfo(() -> sysRoleService.SelectAll());
log.info("[分页]-[{}]",pageInfo);
}
}
Logger
打印比较详细
private static final Logger log = LoggerFactory.getLogger(SysRoleServiceImplTest.class);
log.info("[分页]-[{}]",pageInfo);
lambda
PageInfo pageInfo= PageHelper.startPage(1,1).doSelectPageInfo(() -> sysRoleService.SelectAll());