springboot集成mybatisPlus

文章介绍了如何在Springboot项目中集成MybatisPlus,包括Maven依赖配置、YML数据源设置、Mapper接口与实体类创建,以及分页插件的使用。并通过测试类展示了增删查改及分页查询的功能。
摘要由CSDN通过智能技术生成

step1

maven配置:

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
  </dependency>

step2

创建配置

yml

spring:
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://localhost:3306/jxau?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:     #开启mybatisPlus的日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

配置mybatisPlus的分页查询

@EnableTransactionManagement
@Configuration
//配置mapper类的扫描接口
@MapperScan("com.example.dao")
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制
        //paginationInterceptor.setLimit(2);
        return paginationInterceptor;
    }
}

创建自己的实体类和mapper接口

在这里插入图片描述

Mapper接口(原先的dao包)

@Repository
//这里只要继承了mybatisPlus的baseMapper接口就可以直接使用crud功能
public interface UserLoginDao extends BaseMapper<UserInfo> {

}

实体类

//引入lombok的注解,自动生成get和set方法,方便开发
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("userinfo")
public class UserInfo {
    private String username;
    @TableId(type=IdType.AUTO)
    private int useId;
    private String password;
    private char sex;
    private String email;
    private String description;
}

step3

编写测试类进行测试

@SpringBootTest
class SpringbootVueTestApplicationTests {

    @Autowired
    private UserLoginDao userLoginDao;

    @Test
    public void testInsert(){
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("cgh");
        userInfo.setEmail("9365893985@qq.com");
        userInfo.setSex('1');
        userInfo.setPassword("123456");
        userInfo.setDescription("这个人很懒,什么都没有!");
        userLoginDao.insert(userInfo);
    }

    @Test
    public void testQueryById(){
        UserInfo info = userLoginDao.selectById(1);
        System.out.println(info);
    }

    @Test
    public void testdelete(){
        int deleteById = userLoginDao.deleteById(7);
        System.out.println(deleteById);
    }

/**
     * 测试mybatisPlus的条件查询,相当于在sql语句后面加上where条件
     */
    @Test
    public void testWrapper(){
        QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<>();
        infoWrapper.eq("username","pxl");
        List<UserInfo> infos = userLoginDao.selectList(infoWrapper);
        infos.forEach(System.out::println);
    }

/**
     * 测试mybatisPlus的分页,内置和limit查询一样
     */
  @Test
    public void testPageTest(){
        IPage<UserInfo> page = new Page<>(1,5);
        IPage<UserInfo> iPage = userLoginDao.selectPage(page, null);
        long current = iPage.getCurrent();
        long pages = iPage.getPages();
        long total = iPage.getTotal();
        iPage.getRecords().forEach(System.out::println);
        System.out.println("total:"+total+"    current:"+current+"      pages:"+pages);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值