SpringBoot(八):整合mybatis,通用mapper,分页插件,lambda,Logger,junit用法

栏目: Java · 发布时间: 6年前

内容简介:本作品采用本文作者:低调小熊猫文章链接:

本作品采用 知识共享署名 4.0 国际许可协议 进行许可。

本文作者:低调小熊猫

文章链接: https://aodeng.cc/archives/springbootba

转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032

整合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());

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ruby for Rails

Ruby for Rails

David Black / Manning Publications / 2006-05-11 / USD 44.95

What's Inside * How Ruby and Rails work, separately and together * Extensive Ruby language tutorial * Ruby techniques for Rails applications * Explore the Rails framework source code A new level of pr......一起来看看 《Ruby for Rails》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具