MybatisPlus快速构建数据映射层、服务层

引入依赖

<dependency>
  <groupId>com.baomidougroupId>
  <artifactId>mybatis-plus-boot-starterartifactId>
dependency>

基础服务类实现通用CRUD,此处最关键,是服务层基础,对BaseMapper的实现和包装。
可以按照实际进行代码编辑,此处代码尽可能少,尽量做数据存储。
BaseMapper后面有讲解,BaseMapper由MyBatisPlus提供。
IServiceSupport接口是自行实现的扩展接口,可以是自定义的通用方法,除此之外可以是对父类ServiceImpl里面方法的包装。

/**
 * 对 {@link ServiceImpl} 实现操作,并调用 {@link ServiceImpl} 里面的方法进一步封装
 * 对于继承该类 {@link ServiceSupport} 的类,只需关注自身需要扩展的服务即可
 * @author GanFanFan
 */
@Slf4j
public class ServiceSupport<M extends BaseMapper<T>, T extends Model<T>>
        extends ServiceImpl<M, T> implements IServiceSupport<T> {
    @Override
    public boolean save(T entity) {
        return super.save(entity);
    }

    @Override
    public boolean saveBatch(List<T> entities) {
        if (!CollectionUtils.isEmpty(entities)) {
            return super.saveBatch(entities);
        }
        throw new IllegalArgumentException("批量保存的数据不能为空。");
    }

    @Override
    public boolean deleteById(Long id) {
        return super.removeById(id);
    }

    @Override
    public boolean delete(T entity) {
        QueryWrapper<T> wrapper = new QueryWrapper<>(entity);
        return super.remove(wrapper);
    }

    @Override
    public boolean updateBatch(List<T> entities) {
        if (!CollectionUtils.isEmpty(entities)) {
            return super.updateBatchById(entities);
        }
        throw new IllegalArgumentException("批量更新的数据不能为空。");
    }

    @Override
    public T get(T entity) {
        QueryWrapper<T> wrapper = new QueryWrapper<>(entity);
        return super.getOne(wrapper);
    }

    @Override
    public T getById(Integer id) {
        return super.getById(id);
    }

    @Override
    public List<T> selectForList(T entity) {
        QueryWrapper<T> wrapper = new QueryWrapper<>(entity);
        return super.list(wrapper);
    }

    @Override
    public IPage<T> selectForPage(Page<T> page, T entity) {
        QueryWrapper<T> wrapper = new QueryWrapper<>(entity);
        return super.page(page, wrapper);
    }

    @Override
    public int count(T entity) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        return super.count(wrapper);
    }
}

基础抽象实体类,继承了Model,同时此处简单标记表主键:

/**
 * @author GanFanFan
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public abstract class BaseEntity<T extends Model<T>> extends Model<T> {

    @TableId
    @ApiModelProperty(value = "主键")
    private Integer id;
}

实体类Course

/**
 * 存储课程
 * @author Ganfanfan
 */
@Getter
@Setter
@TableName(value = "course_info")
public class Course extends BaseEntity<Course> {

    @TableField(value = "course_code")
    @ApiModelProperty(value = "课程代码")
    private Integer courseCode;

    @TableField(value = "zh_nickname")
    @ApiModelProperty(value = "课程中文名称")
    private String zhNickname;

    @TableField(value = "en_nickname")
    @ApiModelProperty(value = "课程英文名称")
    private String enNickname;
}

数据映射层Mapper构建,BaseMapper由MyBatisPlus定义提供实现
若需要额外的Mapper映射层功能,可在此编辑,定义接口写SQL即可
定义该接口后,尽量再去创建XML文件

/**
 * @author GanFanFan
 * @date 2021/1/19 14:01
 */
@Repository
public interface CourseMapper extends BaseMapper<Course> {

}

服务层Service构建,先自定义服务层接口,再定义服务层具体实现
自定义服务层接口定义:

/**
 * @author GanFanFan
 */
public interface ICourseService extends IServiceSupport<Course> {
    Course findCourseByCourseCode(Integer courseCode);
    List<Course> findCoursesByZhNickname(String nickname);
}

服务层具体实现:

/**
 * @author GanFanFan
 * @date 2021/1/19 14:03
 */
@Service
public class CourseServiceImpl
        extends ServiceSupport<CourseMapper, Course>
        implements ICourseService {
    @Resource
    CourseMapper courseMapper;
    @Override
    public Course findCourseByCourseCode(Integer courseCode) {

        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("course_code", courseCode);
        return getOne(wrapper);
    }

    @Override
    public List<Course> findCoursesByZhNickname(String nickname) {

        return null;
    }
}

使用方法,使用@Resource注入,尽量使用上面第一种:

@Resource
ICourseService CourseService;

@Resource
CourseServiceImpl CourseService;

low-code,多多关注业务逻辑与实现。

你可能感兴趣的:(mybatisplus,java,mybatis)