Mybatis-Plus入门实践
阅读原文时间:2022年05月11日阅读:1

简介

Mybatis-Plus 简称 MP ,是 Mybatis 的增强工具,提供了一批开箱即用的功能、特性、接口、注解,简化了应用程序访问数据库的相关操作,完善了Mybatis作为ORM仅能做到半自动的不足,提高了开发人员的开发效率。

MP是社区产品,当前源代码在Github上面进行维护,基于Apache2.0开源协议,可放心在商业项目上使用。

常用注解

  • TableName:表名注解,标识实体类对应的数据库表
  • TableId:主键注解,标识此属性对应数据库中的主键字段
  • TableField:字段注解(非主键)

CRUD接口

MP的宗旨就是简化编码,所以一般而言,都是尽量直接使用 service 层接口。若确需手写 sql , 在 mapper 层中编写注解,当然也可以编写xml,更加推荐注解方式,因为xml方式下类型推导和引用会失效。

以下内容摘抄自官网

save

// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);

saveOrUpdate

// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

remove

// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);

update

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);

get

// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

list

// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表
List<Map<String, Object>> listMaps();
// 查询列表
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查询全部记录
List<Object> listObjs();
// 查询全部记录
<V> List<V> listObjs(Function<? super Object, V> mapper);
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

page

// 无条件分页查询
IPage<T> page(IPage<T> page);
// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);

count

// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);

chain

// 链式查询 普通
QueryChainWrapper<T> query();
// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery();

// 示例:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();

insert

// 插入一条记录
int insert(T entity);

delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

update

// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);

select

// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

实践

需要添加的maven依赖:

  • mysql-connector-java:Mysql 官方的jdbc驱动器
  • spring-boot-starter-jdbc:数据源自动配置;提供一个JdbcTemplate 简化使用;事物管理。
  • druid-spring-boot-starter:数据库连接池管理
  • mybatis-plus-boot-starter:MP的依赖,引入MP之后无需再次引入mybatis。
  • springfox-boot-starter:swagger3.0,接口自测工具

jdbc是Java提供的一种标准规范,具体的实现由各个数据库厂商去实现。对开发者来说屏蔽了不同数据库之间的区别,可以使用相同的方式(Java API)去操作不同的数据库。两个设备之间要进行通信需要驱动,不同数据库厂商对jdbc的实现类就是去连接数据库的驱动,如mysql-connector-java连接mysql数据库的驱动。

数据源配置

spring:
  application:
    name: mp-service
  datasource:
    #url: jdbc:mysql://192.168.1.152:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    #username: root
    #password: 123456
    #driver-class-name: com.mysql.cj.jdbc.Driver
    druid:
      #或 spring.datasource.url
      url: jdbc:mysql://192.168.1.152:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
      #或 spring.datasource.username
      username: dev-luyubo
      #或 spring.datasource.password
      password: KjJhAjXJ
      #或 spring.datasource.driver-class-name
      driver-class-name: com.mysql.cj.jdbc.Driver

日志配置

#配置mp日志打印到输出。此配置若和logging.level.org.apache.ibatis.logging: debug 同时存在,后者将不生效
#mybatis-plus:
#configuration:
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

logging:
&nbsp; level:
&nbsp; &nbsp; com.ramble: debug
&nbsp; &nbsp; #配置mp日志打印到项目配置的日志实现框架,默认为logback
&nbsp; &nbsp; org.apache.ibatis.logging: debug





SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` &nbsp;(
&nbsp; `id` bigint NOT NULL COMMENT '主键ID',
&nbsp; `name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '姓名',
&nbsp; `age` int NULL DEFAULT NULL COMMENT '年龄',
&nbsp; `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱',
&nbsp; `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP,
&nbsp; PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'Jone', 18, 'test1@baomidou.com', NULL);
INSERT INTO `user` VALUES (2, 'Jack', 20, 'test2@baomidou.com', NULL);
INSERT INTO `user` VALUES (3, 'Tom', 28, 'test3@baomidou.com', NULL);
INSERT INTO `user` VALUES (4, 'Sandy', 21, 'test4@baomidou.com', NULL);
INSERT INTO `user` VALUES (5, 'Billie', 24, 'test5@baomidou.com', NULL);
SET FOREIGN_KEY_CHECKS = 1;




@TableName("user")
@Data
@Accessors(chain = true)
public class User implements Serializable {
&nbsp; &nbsp; @TableId
&nbsp; &nbsp; private Long id;
&nbsp; &nbsp; @TableField("name")
&nbsp; &nbsp; private String name;
&nbsp; &nbsp; private Integer age;
&nbsp; &nbsp; private String email;
}

若使用 Repository 注解,需手动设置扫描路径,建议使用 Mapper 注解

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

UserService:

public interface UserService &nbsp;extends IService<User> {
&nbsp; &nbsp; List<User> findList(UserConditionDto condition);
&nbsp; &nbsp; Page<User> search(UserConditionDto condition);
}

UserServiceImpl:

@Slf4j
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
&nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp;* 获取列表数据
&nbsp; &nbsp; &nbsp;*
&nbsp; &nbsp; &nbsp;* @param condition
&nbsp; &nbsp; &nbsp;* @return
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; @Override
&nbsp; &nbsp; public List<User> findList(UserConditionDto condition) {
&nbsp; &nbsp; &nbsp; &nbsp; return this.list(this.buildQueryWrapper(condition));
&nbsp; &nbsp; }
&nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp;* 搜索数据
&nbsp; &nbsp; &nbsp;*
&nbsp; &nbsp; &nbsp;* @param condition
&nbsp; &nbsp; &nbsp;* @return
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; @Override
&nbsp; &nbsp; public Page<User> search(UserConditionDto condition) {
&nbsp; &nbsp; &nbsp; &nbsp; QueryWrapper<User> query = this.buildQueryWrapper(condition);
&nbsp; &nbsp; &nbsp; &nbsp; Page<User> page = new Page<>(condition.getPageNum(), condition.getPageSize());
&nbsp; &nbsp; &nbsp; &nbsp; return this.page(page, query);
&nbsp; &nbsp; }
&nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp;* 构造 queryWrapper
&nbsp; &nbsp; &nbsp;*
&nbsp; &nbsp; &nbsp;* @param condition
&nbsp; &nbsp; &nbsp;* @return
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; private QueryWrapper<User> buildQueryWrapper(UserConditionDto condition) {
&nbsp; &nbsp; &nbsp; &nbsp; Assert.notNull(condition, CommonErrorMsgEnum.PARAM_IS_NULL.toString());
&nbsp; &nbsp; &nbsp; &nbsp; QueryWrapper<User> query = new QueryWrapper<>();
&nbsp; &nbsp; &nbsp; &nbsp; //name 模糊匹配
&nbsp; &nbsp; &nbsp; &nbsp; if (StringUtils.hasText(condition.getName())) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.like("name", condition.getName());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; if (StringUtils.hasText(condition.getEmail())) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.like("email", condition.getEmail());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; //id 精确查找
&nbsp; &nbsp; &nbsp; &nbsp; if (null != condition.getId() && condition.getId() > 0) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.eq("id", condition.getId());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; //时间范围
&nbsp; &nbsp; &nbsp; &nbsp; if (null != condition.getStartTime()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (null != condition.getEndTime()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.in("create_time", condition.getStartTime(), condition.getEndTime());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime now = LocalDateTime.now();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.in("create_time", condition.getStartTime(), now);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (null != condition.getEndTime()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.ge("create_time", condition.getEndTime());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; //默认时间倒序
&nbsp; &nbsp; &nbsp; &nbsp; query.orderByDesc("create_time");
&nbsp; &nbsp; &nbsp; &nbsp; return query;
&nbsp; &nbsp; }
}

    /**
     * 构造查询包装器,lambda 链式查询,不用手写字段名称,通过数据库实体映射
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<TagInfo> buildQueryWrapper(TagInfoParam param) {
        LambdaQueryWrapper<TagInfo> query = new LambdaQueryWrapper<>();
        if (null == param) {
            return query;
        }
        query.like(StringUtils.isNotEmpty(param.getTagCode()), TagInfo::getTagCode, param.getTagCode())
                .like(StringUtils.isNotEmpty(param.getTagName()), TagInfo::getTagName, param.getTagName())
                .like(StringUtils.isNotEmpty(param.getTagSimpleName()), TagInfo::getTagSimpleName, param.getTagSimpleName())
                .eq(null != param.getTagId(), TagInfo::getId, param.getTagId()).eq(TagInfo::getIsDeleted, 0);
        return query;
    }

基础语法

  • script

  • where

  • if

  • foreach

  • choose-when-otherwise(if-else ;switch-case-default)

    package com.ramble.mybatisplus.repository;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.ramble.mybatisplus.model.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import java.util.List;
    import java.util.Set;
    @Mapper
    public interface UserMapper extends BaseMapper {
        @Select({
                ""
        })
        User findUserById(@Param("user") User user);
        @Select({
                ""
        })
        List findUserByIds(@Param("ids") Set ids);
        @Select({
                ""
        })
        List findUser(@Param("user") User user, @Param("currentUserName") String currentUserName);
    }

代码

https://gitee.com/naylor_personal/ramble-spring-cloud/tree/master/mybatisplus

引用

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章