引入mybatis-plus

1.引入的依赖

<!--spring整合mybatis-plus -->
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.4.3</version>
         </dependency>
 <!--spring整合mybatis-plus -->
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.2.0</version>
         </dependency>

2.编辑Mapper接口

public interface UserMapper extends BaseMapper<User>

3.编写service

public interface UserService extends IService<User>

4.编写ServiceImpl

public class UserServiceImpl extends ServiceImpl<UserMapper , User> implements UserService{
}

5.查询方法


QueryWrapper<User> wrapper = new QueryWrapper<>();
        //等于 
        wrapper.eq("user_id",userId);
        //模糊查询
        wrapper.like("username","%张%");

 mysql中模糊查询时可用concat

select userName from user where userName LIKE CONCAT(’%’, ‘张’, ‘%’)

Spring Boot 引入 MyBatis-Plus 的依赖通常是为了简化MyBatis的配置和数据库操作,它是一个基于MyBatis的ORM工具,封装了一些常用的功能,如CRUD、分页等,并且集成了Spring Data的一些特性。 以下是添加MyBatis-PlusSpring Boot项目的步骤: 1. 添加依赖:在你的`pom.xml`文件中,添加MyBatis-PlusSpring Boot JPA相关的依赖,如果不需要JPA,可以只添加MyBatis-Plus的依赖: ```xml <dependencies> <!-- 如果使用Spring Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 或者只使用MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.x.x</version> <!-- 根据最新版本替换x.x.x --> </dependency> <!-- 可选,如果你需要支持更多功能,如全局通用Mapper和全局通用Service --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension-boot-starter</artifactId> <version>3.x.x</version> </dependency> </dependencies> ``` 2. 配置数据源:确保你在application.properties或application.yml文件中配置了数据源信息。 3. 创建实体和Mapper:在项目中创建对应的实体类和Mapper接口,MyBatis-Plus会自动扫描生成对应的实现类。 4. 注解驱动:启用MyBatis的注解驱动,可以在`@EnableAutoConfiguration`上添加`spring.jpa.hibernate.ddl-auto=update`来自动创建表。 5. 初始化:运行应用后,MyBatis-Plus会在启动时自动生成相应的映射文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值