SpringBoot2入门记录

IntelliJ IDEA 快捷键   

Ctrl + G或者 Cmd + G 快捷键多次,可以将下一个匹配的相同文本选中

 

 

MyBatis-Plus

分页要添加分页配置

package cn.he4966.flex.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPulsConfig {

    /*
     * @description: mybatis-plus插件配置 <br>
     * @param
     * @return com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        //数据库类型
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        //添加到插件列表
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }
}

 

 

测试代码

 

package cn.he4966.flex;


import cn.he4966.test.domain.User;
import cn.he4966.test.mapper.UserMapper;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;


@SpringBootTest
class FlexApplicationTests {
    //
    @Test
    void contextLoads() {
    }


    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- 开始打印输出 ------"));
        List<User> userList = userMapper.selectList(null);

//        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印 ------"));
    }

    @Test
    public void testId小于3() {
        System.out.println(("----- 开始打印输出id<3 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.lt("id", 3);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出id<3 ------"));
    }


    @Test
    public void testId大于3() {
        System.out.println(("----- 开始打印输出id>3 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.gt("id", 3);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出id>3 ------"));
    }

    @Test
    public void testId大于等于3() {
        System.out.println(("----- 开始打印输出id>=3 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.ge("id", 3);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出id>=3 ------"));
    }

    @Test
    public void Id小于等于3() {
        System.out.println(("----- 开始打印输出id<=3 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.le("id", 3);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出id<=3 ------"));
    }


    @Test
    public void email模糊搜索() {
        System.out.println(("----- 开始打印输出email模糊搜索 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.like("email", "est");
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出email模糊搜索 ------"));
    }

    @Test
    public void email模糊搜索加age大于等于() {
        System.out.println(("----- 开始打印输出email模糊搜索加age大于等于 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.like("email", "est");
        wrapper.ge("age", 21);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出email模糊搜索加age大于等于 ------"));
    }


    @Test
    public void email模糊搜索加age大于() {
        System.out.println(("----- 开始打印输出email模糊搜索加age大于 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.like("email", "est");
        wrapper.gt("age", 21);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);
        System.out.println(("----- 结束打印输出email模糊搜索加age大于 ------"));
    }


    @Test
    public void 翻页() {
        System.out.println(("----- 开始打印输出翻页 ------"));
        //        注意记得要添加分页配置
        Page<User> page = new Page<>(1, 2);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("id", 1);
        Page<User> userList = userMapper.selectPage(page, wrapper);

        System.out.println(JSON.toJSONString(userList));
        System.out.println("————————————————————————");
        System.out.println(userList.getTotal());
        System.out.println("————————————————————————");
        List<User> list = userList.getRecords();
        String jsonString = JSON.toJSONString(list);

        System.out.println(jsonString);
        System.out.println(("----- 结束打印输出翻页 ------"));
    }

    @Test
    public void 统计数量() {
        System.out.println(("----- 开始打印输出统计数量 ------"));
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.like("email", "est");
        wrapper.gt("age", 21);
        Long count = userMapper.selectCount(wrapper);

        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.out::println);

        System.out.println(count);
        System.out.println(("----- 结束打印输出统计数量 ------"));
    }

}

 

 

生成

 

 

1679
主攻后端PHP
4660
二次开发Code
1679
长连接Swoole
4640
高并发网站Redis