2018-03-08
springboot-集成mybatis
springboot 评论:0 浏览:116

转载请注明出处:https://oldnoop.tech/c/158.html

添加mybatis起步依赖

可以在创建工程时选择mybatis模块

mybatis-spring-boot-starter

修改application.properties

配置mybatis
mybatis.mapper-locations=classpath*:com/oldnoop/**/mapper/*.xml
mybatis.typeAliasesPackage=com.oldnoop.**.entity


修改启动类,添加注解@MapperScan

@MapperScan(basePackages = "com.oldnoop.**.mapper",sqlSessionFactoryRef="sqlSessionFactory")

 

添加分页插件

使用pagehelper,该插件已经支持springboot

 添加起步依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.1.2</version>
</dependency>

配置插件

修改application.properties


#配置分页插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

插件单元测试代码示例

@Test
public void testSelectPage() {
    //测试查询分页
    PageHelper.startPage(1,2);
    List list = memberMapper.selectByExample(null);
    System.out.println(list);
}

配置事务

springboot推荐编码式配置,

修改启动类,添加注解@EnableTransactionManagement

然后就可以在service层上加上@Transational注解来配置事务

 

 



  • 转载请注明出处:https://oldnoop.tech/c/158.html

Copyright © 2018 oldnoop.tech. All Rights Reserved

鄂ICP备2023022735号-1