spring-boot 整合Mybatis-1-基本使用

一、添加依赖

        <!--database-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis.spring.boot.version}</version>
		</dependency>

二、指定数据源与mybatis属性配置

spring:
    profiles: test
    datasource:
      url: jdbc:mysql://127.0.0.1:3306/usthe?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456


mybatis:
  # 指向实体类包路径
  type-aliases-package: com.usthe.bootshiro.domain.bo
  # mapper xml 文件地址
  mapper-locations: classpath:mapper/*.xml
  # 检查 mybatis 配置是否存在,一般命名为 mybatis-config.xml
  check-config-location: true
  # 执行模式。默认是 SIMPLE
  executor-type: simple

三、与springboot整合

   MapperScan(推荐)

@SpringBootApplication
@MapperScan("com.usthe.bootshiro.dao")
@EnableCaching
@ServletComponentScan
public class BootshiroApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootshiroApplication.class, args);
	}
}

四、集成mybatis插件-分页插件

            <!-- 分页支持pageHelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>${pagehelper.version}</version>
		</dependency>
    #pagehelper分页插件
    pagehelper:

        helperDialect: mysql
        #为了使用输入页数为负或者超出最大页时候使页数为最小或最大值
        reasonable: true
        supportMethodsArguments: true
        params: count=countSql
1.使用插件
    
  
    
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "获取日志记录", httpMethod = "GET")
    @RequestMapping("/accountLog/{currentPage}/{pageSize}")
    public Message getAccountLogList(@PathVariable Integer currentPage, @PathVariable Integer pageSize ) {
        //1.设置分页插件
        PageHelper.startPage(currentPage, pageSize);
        //2.查询数据 
        List<AuthAccountLog> accountLogs = accountLogService.getAccountLogList();
        //3.封装结果
        PageInfo pageInfo = new PageInfo(accountLogs);
        //4.返回结果
        return new Message().ok(6666, "return accountLogs success").addData("data",pageInfo);
    }
 
     
2.返回结果示例

 {
  "pageNum": 0,
  "pageSize": 5,
  "size": 5,
  "startRow": 1,
  "endRow": 5,
  "total": 14,
  "pages": 3,
  "list": [
    {
      "userId": 1000,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1001,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1002,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1003,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1004,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    }
  ],
  "prePage": 0,
  "nextPage": 1,
  "isFirstPage": false,
  "isLastPage": false,
  "hasPreviousPage": false,
  "hasNextPage": true,
  "navigatePages": 8,
  "navigatepageNums": [
    1,
    2,
    3
  ],
  "navigateFirstPage": 1,
  "navigateLastPage": 3,
  "firstPage": 1,
  "lastPage": 3
}

五、使用别名增删改查(枚举) -typeHandler转换类使用

 

六、数据库事务处理

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

良之才-小良

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值