内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

SpringBoot(8):整合mybatis,通用mapper,分页插件,lambda,Lo

2018-10-08 17:10 出处:清屏网 人气: 评论(0

整合demo

配置依赖

<!--通用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>

配置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<SysRole>{
}

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<SysRole> sysRoleList=sysRoleService.SelectAll();
        log.info("[普通写法] - [{}]", sysRoleList);
        //分页
        PageInfo<Object> 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<Object> pageInfo= PageHelper.startPage(1,1).doSelectPageInfo(() -> sysRoleService.SelectAll());

分享给小伙伴们:
本文标签: SpringBootmybatismapper

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号