springboot整合mybatis-plus 3.0

1.搭建springboot工程

略.

2.引入相应依赖


            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.1
        
        
            org.projectlombok
            lombok
            1.16.10
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

3.properties文件配置

server.port=8088

spring.datasource.name=demo
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123

#sql语句打印
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4.mybatis 分页配置

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

5.创建mapper以及实体类

@Mapper
public interface UserMapper extends BaseMapper {


}


@Data
public class User {

    @TableId(value = "id",type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date createTime;
}

建表语句

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) not null auto_increment COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(30) NULL DEFAULT NULL COMMENT '邮箱',
	create_time DATETIME(0) NULL DEFAULT NULL COMMENT '创建时间',
	PRIMARY KEY (id)
);

 

6.分页带条件查询

备注:增删改比较简单,不做演示,附上测试代码

 

@SpringBootTest
public class MybatisPlusApplicationTests {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void queryUserForPage() {
        IPage userPage = new Page<>(1, 10);//参数一是当前页,参数二是每页个数
        userPage = userMapper.selectPage(userPage, null);
        List list = userPage.getRecords();
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void insert() {
        for (int i = 0; i <= 35; i++) {
            User queryInfo = new User();
            queryInfo.setName("wang" + (i + 1));
            queryInfo.setAge(Integer.parseInt(i + "") + 1);
            queryInfo.setEmail("[email protected] ---> " + (i + 1));
            userMapper.insert(queryInfo);
        }
    }


}

controller代码

@RestController
public class MyBatisController {

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/list")
    public Object list(@RequestParam("pageNo") Integer pageNo,
                       @RequestParam("pageSize") Integer pageSize){
        // 参数一是当前页,参数二是每页个数
        IPage userPage = new Page<>(pageNo, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.and(wrapper -> wrapper.eq("name","wang1").
//                or().eq("name", "wang2").
//                or().eq("name", "wang3"));

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,5);
        queryWrapper.gt("create_time",new Date());
        queryWrapper.lt("create_time",calendar.getTime());
        userPage = userMapper.selectPage(userPage, queryWrapper);
        List list = userPage.getRecords();

        return list;
    }

    @RequestMapping("/insert")
    public Object insert(){
        int num = 0;
        for (int i = 0; i <= 35; i++) {
            Calendar calendar = Calendar.getInstance();
            User queryInfo = new User();
            queryInfo.setName("wang" + (i + 1));
            queryInfo.setAge(Integer.parseInt(i + "") + 1);
            queryInfo.setEmail("[email protected] ---> " + (i + 1));
            calendar.add(Calendar.DAY_OF_MONTH,(i + 1));
            queryInfo.setCreateTime(calendar.getTime());
            num += userMapper.insert(queryInfo);
        }
        return num;
    }


}

使用restclient测试

1.带时间条件分页查询

springboot整合mybatis-plus 3.0_第1张图片

2. and or eq 条件查询

springboot整合mybatis-plus 3.0_第2张图片

 

 

你可能感兴趣的:(java)