zoukankan      html  css  js  c++  java
  • Spring Boot 中使用 TKMybatis 和 PageHelper

    整合 Druid 数据源

    引入依赖

    pom.xml 中引入 druid-spring-boot-startermysql-connector-java 依赖

    <!-- druid-spring-boot-starter -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
    
    <!-- 数据库连接依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
        <scope>runtime</scope>
    </dependency>
    

    相关配置

    application.yml 中添加以下配置

    spring:
      datasource:
        druid:
          url: jdbc:mysql://127.0.0.1:3306/{db_name}?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
          username: {username}
          password: {password}
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.jdbc.Driver # MySQL 8.x: com.mysql.cj.jdbc.Driver
    

    整合 tk.mybatis

    tk.mybatis 整合了 MyBatis 框架,在其基础上提供了很多工具,封装了常用的增删改查 SQL 语句,可以让我们的开发效率更高。

    引入依赖

    pom.xml 中引入 mapper-spring-boot-starter 依赖

    <!-- mapper-spring-boot-starter -->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.0.2</version>
    </dependency>
    

    相关配置

    application.yml 中添加 mybatis 相关配置,并且设置日志监听路径

    mybatis:
        type-aliases-package: # 实体类的存放路径,如:com.antoniopeng.hello.spring.boot.entity
        mapper-locations: classpath:mapper/*.xml # mapper.xml 文件存放路径,这里存放在配置文件目录 resources 下
        
    logging:
      level:
        com.antoniopeng.hello.springboot.mybatis: debug # 配置监听日志
    

    整合 PageHelper 分页插件

    引入依赖

    pom.xml 中引入 pagehelper-spring-boot-starter 依赖

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

    相关配置

    Application 入口类中使用 tk.mybatis.spring.annotation 包下的 @MapperScan 注解指定 Mapper 接口的扫描路径

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import tk.mybatis.spring.annotation.MapperScan;
    
    @MapperScan(value = "com.antoniopeng.springboot.mybatis.mapper")
    @SpringBootApplication
    public class HelloSpringBootMybatisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HelloSpringBootMybatisApplication.class, args);
        }
    
    }
    

    分页查询示例

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    @Transactional
    @Rollback
    public class MyBatisTests {
    
        @Autowired
        UserService userService;
    
        /**
        * 测试分页插件
        */
        @Test
        public void testPageHelper() {
        
    	Example example = new Example(User.class);
    	// 查询条件
            example.createCriteria().andEqualTo("userId", "1")
            // 分页参数
            PageHelper.startPage(1, 10, "create_time desc");
            // 获取分页列表数据
            List<User> userList = userService.selectByExample(example);
            PageInfo pageInfo = new PageInfo(userList);
            // 获取列表总数
            int userCount = (int) pageInfo.getTotal();
        }
    }
    
  • 相关阅读:
    HTML浏览器标题栏如何设置
    阿里团队常用的布局——双飞翼布局
    浏览器兼容性问题——IE不支持却很实用的CSS属性Outline和Child
    css选择器:基本选择器
    编程十年
    解决WX-IOS打开公众号网页出现的底部导航条
    Angular6路由复用与延迟加载的冲突解决——看看有备无患
    Ionic2 + Angular4 + JSSDK开发中的若干问题汇总
    [Linq Expression]练习自己写绑定
    [Linq To Sql]解决join时的Collation冲突
  • 原文地址:https://www.cnblogs.com/antoniopeng/p/12687262.html
Copyright © 2011-2022 走看看