zoukankan      html  css  js  c++  java
  • TkMyBatis 高级查询

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/13197586.html

    Maven Dependency

    ...        
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>
    
        <!--mybatis-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
    
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>
    
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>
    
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    ...

    Method

    selectByExample

    public void selectByExample() {
        log.info("select by example: where username = ? and password = ?");
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", "update100");
        criteria.andEqualTo("password", "update100");
        List<User> resultList = userMapper.selectByExample(example);
        log.info("result list: {}", resultList);
    }

    selectByExampleAndLike

    public void selectByExampleAndLike() {
        log.info("select by example with andLike: where username like = ?");
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("username", "%update%");
        List<User> resultList = userMapper.selectByExample(example);
        log.info("result list: {}", resultList);
    }

    selectByExampleOrderBy

    public void selectByExampleOrderBy() {
        log.info("select by example with order by: where username like = ? order by id desc");
        Example example = new Example(User.class);
        example.setOrderByClause("username desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("username", "%user%");
        List<User> resultList = userMapper.selectByExample(example);
        log.info("result list: {}", resultList);
    }

    selectByExampleIn

    public void selectByExampleIn() {
        log.info("select by example with in: where id in (100, 101)");
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(100, 101));
        List<User> resultList = userMapper.selectByExample(example);
        log.info("result list: {}", resultList);
    }

    selectByPageInfo

    public void selectByPageInfo(int pageNo, int pageSize) {
        log.info("select by PageInfo: limit ?, ?");
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        long totalPage = pageInfo.getPages();
        long totalSize = pageInfo.getTotal();
        log.info("total page: {}, total size: {}, result list: {}", totalPage, totalSize, userList);
    }

    Test

    UserServiceTest.java

     1 package org.fool.redis.test;
     2 
     3 import org.fool.redis.Application;
     4 import org.fool.redis.service.UserService;
     5 import org.junit.jupiter.api.Test;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.boot.test.context.SpringBootTest;
     8 
     9 @SpringBootTest(classes = Application.class)
    10 public class UserServiceTest {
    11     @Autowired
    12     private UserService userService;
    13 
    14     @Test
    15     public void testSelectByExample() {
    16         userService.selectByExample();
    17     }
    18 
    19     @Test
    20     public void testSelectByExampleAndLike() {
    21         userService.selectByExampleAndLike();
    22     }
    23 
    24     @Test
    25     public void testSelectByExampleOrderBy() {
    26         userService.selectByExampleOrderBy();
    27     }
    28 
    29     @Test
    30     public void testSelectByExampleIn() {
    31         userService.selectByExampleIn();
    32     }
    33 
    34     @Test
    35     public void testSelectByPageInfo() {
    36         userService.selectByPageInfo(2, 10);
    37     }
    38 }
  • 相关阅读:
    《Linux4.0设备驱动开发详解》笔记--第十四章:Linux网络设备驱动
    《Linux4.0设备驱动开发详解》笔记--第十三章:Linux块设备驱动
    《Linux4.0设备驱动开发详解》笔记--第十二章:Linux设备驱动的软件架构思想
    《Linux4.0设备驱动开发详解》笔记--第十一章:内存与I/O访问
    《Linux4.0设备驱动开发详解》笔记--第十章:Linux设备驱动中的中断与时钟
    《Linux4.0设备驱动开发详解》笔记--第九章:Linux设备驱动中的异步通知与同步I/O
    《Linux4.0设备驱动开发详解》笔记--第八章:linux设备驱动的阻塞与非阻塞
    《机器学习》(西瓜书)笔记(5-2)--神经网络
    《机器学习》(西瓜书)笔记(5-1)--神经网络
    《机器学习》(西瓜书)笔记(4)--决策树
  • 原文地址:https://www.cnblogs.com/agilestyle/p/13197586.html
Copyright © 2011-2022 走看看