美文网首页
06.`spring boot`集成`mybatis` 或 `m

06.`spring boot`集成`mybatis` 或 `m

作者: 风安峻_ | 来源:发表于2020-09-06 14:12 被阅读0次
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
      ge ge("age", 18) age >= 18
      lt 小于 < lt("age", 18) age < 18
      le le("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
      like LIKE '%值%' like("name", "王") name like '%王%'
      notLike NOT 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
      orderByAsc Asc升序排序 orderByAsc("id", "name") order by id ASC,name ASC
      orderByDesc Desc降序排序 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. 一般逻辑删除,不怎么使用

相关文章

网友评论

      本文标题:06.`spring boot`集成`mybatis` 或 `m

      本文链接:https://www.haomeiwen.com/subject/ltdtektx.html