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转换类使用

六、数据库事务处理

猜你喜欢

转载自blog.csdn.net/lidongliangzhicai/article/details/91819380