springboot整合mybatis-plus 3.0

本文介绍如何在SpringBoot项目中整合MyBatis Plus,包括环境搭建、依赖引入、属性配置、分页及条件查询等核心功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.搭建springboot工程

略.

2.引入相应依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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<User> {


}


@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<User> userPage = new Page<>(1, 10);//参数一是当前页,参数二是每页个数
        userPage = userMapper.selectPage(userPage, null);
        List<User> 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("1021558365@qq.com ---> " + (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<User> userPage = new Page<>(pageNo, pageSize);
        QueryWrapper<User> 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<User> 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("1021558365@qq.com ---> " + (i + 1));
            calendar.add(Calendar.DAY_OF_MONTH,(i + 1));
            queryInfo.setCreateTime(calendar.getTime());
            num += userMapper.insert(queryInfo);
        }
        return num;
    }


}

使用restclient测试

1.带时间条件分页查询

2. and or eq 条件查询

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁漂打工仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值