创建一个简单的mybatis-plus(以下简称mp)

本文介绍了如何创建一个SpringBoot项目,添加必要的依赖如Mybatis-Plus和Lombok,配置数据库连接,以及如何使用BaseMapper接口简化开发。同时提到了启动类的@MapperScan注解用于扫描Mapper接口,以便于Spring识别和使用。

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

1. 创建springboot工程,一路就行(pom文件的版本自己改)

2. 引入相关依赖springboot和mp依赖(默认安装lombok插件)

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>
        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- lombok用来简化实体类 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

3. 修改配置文件application.properties(后续用yml)

(springboot2.2版本后要加上cj和时区)

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
#自己的密码
spring.datasource.password=xxx
# mybatis 日志配置,方便显示输出
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4. 创建实践类

5. 创建对应的mapper接口,但是不需要再有对应的xml文件。

直接继承BaseMapper接口。(mp的巨大特点)

记得加上@Repository等,方便被spring识别,后续可被@Autowired使用。

 6. 标记启动类。添加

@MapperScan("com.xxx.mapper")到启动类上(因为后续要查接口的类,但是接口没有对应实现类,就会找不到接口,使用MapperScan方便查询)

后续添加到配置类上。(bean必须在configuration注解下使用)

7. 测试。

先导入对应的接口对象(自动创建),后续测试方法调用对象方法。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值