• 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 }
  • 相关阅读:
    关于网络调试助手
    阿里云之设备连接方法学习
    阿里云学习
    Jquery ThickBox的使用
    推荐几款制作网页滚动动画的 JavaScript 库
    Javascript动态操作CSS总结
    css3动画属性系列之transform细讲scale缩放
    JS函数重载解决方案
    从Java开发者的视角解释JavaScript
    理解JavaScript中的事件路由冒泡过程及委托代理机制
  • 原文地址:https://www.cnblogs.com/agilestyle/p/13197586.html
Copyright © 2020-2023  润新知