Springboot整合mybatis-plus

本文介绍了如何在Springboot项目中整合Mybatis-Plus,以实现更便捷的数据操作。通过引入Mybatis-Plus依赖和配置Druid连接池,可以快速进行基本的数据库CRUD操作。在实体类、Dao层和测试类的简单编写后,即可利用Mybatis-Plus提供的方法进行数据查询。

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

Springboot整合mybatis-plus

mybatis-plus就是在mybatis上的只增不减,集成了jpa的那种,很多简单的crud只需要调用已经写好的方法就是了

mybatis-plus官网
具体实现

  1. 引入依赖(这里我还引入了阿里巴巴的连接池)
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- 阿里巴巴的连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>
  1. 写入配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/sxy_news?serverTimezone=Asia/Shanghai
    username: root
    password: 123123
  1. 在springboot的启动类上添加包扫描(或者是在每个mapper文件上加上@Mapper注解也行)
    在这里插入图片描述
  2. 实体类
    在这里插入图片描述
  3. Dao的编写
    在这里插入图片描述
  4. 测试类的编写
@SpringBootTest
class MybatisplusApplicationTests {
    @Autowired
    SdtbunewDao sdtbunewDao;

    @Test
    void contextLoads() {
        List list = sdtbunewDao.selectList(null);
        list.forEach(System.out::println);
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值