06.`spring boot`集成`mybatis` 或 `mybatis-plus`

1. 说明
  1. spring boot只能集成两者中的一个,mybatis-plus是对mybatis的进一步封装,省去了书写mapper.xml
  2. mybatis-plus的封装只支持单表操作,对于多表依然可以通过xml的方式来实现
2. 集成mybatis
  1. 依赖

    <dependencies>
        <!-- mybatis-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
    </dependencies>
    
    1. 导入starter
    2. 使用还要配置jdbc驱动配置druid连接池
  2. 配置文件

    1. application.yml主配置文件注册

      spring:
        profiles:
          # 注册配置文件
          active: dev,druid,mybatis
      
    2. application-mybatis.yml

      mybatis:
        # mapper.xml文件位置
        mapper-locations: classpath:mapper/**/*.xml
        # 别名配置
        type-aliases-package: com.sheng.mybatis.domain.entity
        configuration:
          # 日志配置
          log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
      
  3. 程序启动类上使用@MappenScan("")

    @SpringBootApplication
    // 自动注册mapper接口
    @MapperScan("com.sheng.mybatis.mapper")
    public class SpringBootMybatisApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootMybatisApplication.class, args);
        }
    }
    
3. 集成mybatis-plus
  1. 依赖

    <dependencies>
        <!-- mybatis-plus-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>
    
    1. 导入starter
    2. 使用还要配置jdbc驱动配置druid连接池
    3. 可以导入mybatis的依赖,但是不能配置mybatis
  2. 配置文件

    1. application.yml主配置文件注册

      spring:
        profiles:
          # 注册配置文件
          active: dev,druid,mybatis-plus
      
    2. application-mybatis-plus.yml

      mybatis-plus:
        # 可配可不配,默认为 classpath*:/mapper/**/*.xml
        mapper-locations: classpath:mapper/**/*.xml
        configuration:
        # 配置日志
          log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
      
  3. 程序启动类上使用@MappenScan("")

    @SpringBootApplication
    // 自动注册mapper接口
    @MapperScan("com.sheng.mybatis.plus.mapper")
    public class SpringBootMybatisPlusApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
        }
    }
    
4. mybatis-plus的基本使用
  1. mybatis-plus 官网

  2. select

    1. 源码

      /**
       * 根据 ID 查询
       *
       * @param id 主键ID
       */
      T selectById(Serializable id);
      
      /**
       * 查询(根据ID 批量查询)
       *
       * @param idList 主键ID列表(不能为 null 以及 empty)
       */
      List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
      
      /**
       * 查询(根据 columnMap 条件)
       *
       * @param columnMap 表字段 map 对象
       */
      List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
      
      /**
       * 根据 entity 条件,查询一条记录
       *
       * @param queryWrapper 实体对象封装操作类(可以为 null)
       */
      T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
      /**
       * 根据 Wrapper 条件,查询总记录数
       *
       * @param queryWrapper 实体对象封装操作类(可以为 null)
       */
      Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
      /**
       * 根据 entity 条件,查询全部记录
       *
       * @param queryWrapper 实体对象封装操作类(可以为 null)
       */
      List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
      /**
       * 根据 Wrapper 条件,查询全部记录
       *
       * @param queryWrapper 实体对象封装操作类(可以为 null)
       */
      List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
      /**
       * 根据 Wrapper 条件,查询全部记录
       * <p>注意: 只返回第一个字段的值</p>
       *
       * @param queryWrapper 实体对象封装操作类(可以为 null)
       */
      List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
      /**
       * 根据 entity 条件,查询全部记录(并翻页)
       *
       * @param page         分页查询条件(可以为 RowBounds.DEFAULT)
       * @param queryWrapper 实体对象封装操作类(可以为 null)
       */
      <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
      /**
       * 根据 Wrapper 条件,查询全部记录(并翻页)
       *
       * @param page         分页查询条件
       * @param queryWrapper 实体对象封装操作类
       */
      <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
      
    2. 常用方法栗子

      @Test
      public void test() {
          // selectById()
          Menu menu = menuMapper.selectById(1);
      
          // selectList()
          QueryWrapper<Menu> wrapper = new QueryWrapper<>();
          wrapper.eq(Menu.COL_MENU_NAME, "zero")
                  .or()
                  .eq(Menu.COL_STATUS, 0);
          List<Menu> menus = menuMapper.selectList(wrapper);
          
          // selectCount()
          Integer integer = menuMapper.selectCount(wrapper);
          
          // selectBatchIds()
          menus = menuMapper.selectBatchIds(Arrays.asList(1, 2, 3));
      }
      
    3. QueryWrapper的使用:官网

      常用方法说明栗子栗子对应sql
      eq等于=eq("name", "老王")name = '老王'
      ne不等于!=ne("name", "老王")name <> '老王'
      gt大于 >gt("age", 18)age > 18
      gege("age", 18)age >= 18
      lt小于 <lt("age", 18)age < 18
      lele("age", 18)age <= 18
      between在…之间between("age", 18, 30)age between 18 and 30
      notBetween不在…之间notBetween("age", 18, 30)age not between 18 and 30
      likeLIKE ‘%值%’like("name", "王")name like '%王%'
      notLikeNOT LIKE ‘%值%’notLike("name", "王")name not like '%王%'
      in在某个集合内in("age",{1,2,3})age in (1,2,3)
      notIn不在某个集合内notIn("age",{1,2,3})age not in (1,2,3)
      groupBy分组groupBy("id", "name")group by id,name
      orderByAscAsc升序排序orderByAsc("id", "name")order by id ASC,name ASC
      orderByDescDesc降序排序orderByDesc("id", "name")order by id DESC,name DESC
      or下一个方法不是用and连接eq("id",1).or().eq("name","老王")id = 1 or name = '老王'
      • 不调用or默认使用and连接
  3. update

    1. 源码

      /**
       * 根据 ID 修改
       *
       * @param entity 实体对象
       */
      int updateById(@Param(Constants.ENTITY) T entity);
      
      /**
       * 根据 whereEntity 条件,更新记录
       *
       * @param entity        实体对象 (set 条件值,可以为 null)
       * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
       */
      int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
      
    2. 栗子

      // updateById()
      Address address = new Address();
      address.setAddressId(id);
      address.setIsDefault(1);
      // 修改
      int result = addressMapper.updateById(address);
      
      // update()
      Address address = new Address();
      address.setCustomerId(useId);
      address.setIsDefault(0);
      // 条件对象
      UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
      // 拼接条件
      wrapper.eq(Address.COL_CUSTOMER_ID, useId);
      result = addressMapper.update(address, wrapper);
      
    3. UpdateWrapperQueryWrapper的使用类似

  4. insert

    1. 源码

      /**
       * 插入一条记录
       *
       * @param entity 实体对象
       */
      int insert(T entity);
      
    2. 栗子

      @Test
      public void test() {
          Item item = new Item();
          item.setName("zero");
          item.setPassword("123456");
          item.setAge(18);
          // 插入
          int result = itemMapper.insert(item);
      }
      
  5. delete

    1. 源码

      /**
       * 根据 ID 删除
       *
       * @param id 主键ID
       */
      int deleteById(Serializable id);
      
      /**
       * 根据 columnMap 条件,删除记录
       *
       * @param columnMap 表字段 map 对象
       */
      int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
      
      /**
       * 根据 entity 条件,删除记录
       *
       * @param wrapper 实体对象封装操作类(可以为 null)
       */
      int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
      
      /**
       * 删除(根据ID 批量删除)
       *
       * @param idList 主键ID列表(不能为 null 以及 empty)
       */
      int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
      
    2. 一般逻辑删除,不怎么使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值