mybatis-plus实现增删改查

本文详细介绍使用MyBatis-Plus进行数据库操作的方法,包括如何配置依赖、创建实体类、Mapper接口及ServiceImpl类,实现增删改查等功能。通过具体示例,读者将学会利用QueryWrapper构建SQL查询条件,掌握高效数据库操作技巧。

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

mybatis-plus实现增删改查

QueryWrapper

在开始操作之前先了解一下QueryWrapper

QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类
用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件

常用的条件构造器

eq:等于

like:模糊查询

ne:不等于

gt:大于

lt:小于

更多条件构造器可以参考官网文档

添加依赖
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
        <!--阿里巴巴连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>
        <!--mybatis-plus依赖 -->
        <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
      

创建poji实体类Car

@TableName("car")//和数据库的表相对应
@Data//自动添加get/set方法
public class Car {
//主键自增
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String type;
    private String brand;
    private Double price;

创建Mapper接口继承BaseMapper

@Mapper
public interface AutoMapper extends BaseMapper<Car> {

}

创建ServiceImpl类

@Service
public class AutoServiceImpl implements AutoService {
    @Autowired(required = false)
    private AutoMapper autoMapper;
 }
查询全部

在serviceImpl类中查询所有方法

//查询所有
    @Override
    public List<Car> findAll() {
        return autoMapper.selectList(null);
    }
增加

在ServiceImpl类中添加增加方法

public void addCar(Car car)
{
    autoMapper.insert(car);
}
根据id查询

在ServiceImpl中添加根据id查询的方法

//根据id查询
    @Override
    public List<Car> findCarById(Integer id) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return autoMapper.selectList(queryWrapper);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值