MP的BaseMapper没有提供批量的DML操作。
但是在IService
接口中却定义了这些常用操作
定义一个接口
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.stereotype.Service;
import top.yumbo.springcloud.car.entity.Car;// 自定义的实体
@Service
public interface TCarIService extends IService<Car> {
}
定义接口实现类
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.yumbo.springcloud.car.dao.TCarDao;
import top.yumbo.springcloud.car.entity.Car;
@Service
public class TCarIServiceImpl extends ServiceImpl<TCarDao, Car> implements TCarIService{
}
同时需要有Dao
层的那个接口
dao层接口的定义
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import top.yumbo.springcloud.car.entity.Car;
@Mapper
@Component
public interface TCarDao extends BaseMapper<Car> {
}
实体Car的配置参考例子,使用了lombok所以代码如下
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@TableName(value = "t_car")
public class Car implements Serializable {
@TableId
private long carId;
private String carType;
private String carBrand;
private String carColor;
private double carPrice;
private String carDescription;
private String carInfo;
private String carArea;
private String carImage;
private String carRegtime;
private String carMileage;
private String carStatus;
}
yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mp?useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
mybatis-plus:
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名
更详细的项目搭建参考我之前发布的博客:mybatis-plus入门