• 通用Mapper(二)创建具体Mapper接口与Mapper接口介绍


    1、创建具体的Mapper接口

      继承通用的Mapper<T>,必须指定泛型<T>,例如下面的例子:

    /**
     *
     * 具体操作数据库的 Mapper接口,需要继承通用Mapper提供的核心接口 Mapper<T>
     *     泛型类型就是实体类的类型
     *
     */
    public interface EmployeeMapper extends Mapper<Employee> {
      //其他必须手写的接口
    }

       一旦继承了Mapper<T>,继承的Mapper就拥有了Mapper<T>所有的通用方法。

    2、Mapper<T> 里面有什么

        Mapper<T> 的声明

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface Mapper<T> extends
            BaseMapper<T>,
            ExampleMapper<T>,
            RowBoundsMapper<T>,
            Marker {
    
    }

      (1)BaseMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface BaseMapper<T> extends
            BaseSelectMapper<T>,
            BaseInsertMapper<T>,
            BaseUpdateMapper<T>,
            BaseDeleteMapper<T> {
    
    }

        ① BaseSelectMapper<T>:定义基本的查询操作

    /**
     * 通用Mapper接口,基础查询
     *
     * @param <T> 不能为空
     * @author liuzh
     */
    @RegisterMapper
    public interface BaseSelectMapper<T> extends
            SelectOneMapper<T>,
            SelectMapper<T>,
            SelectAllMapper<T>,
            SelectCountMapper<T>,
            SelectByPrimaryKeyMapper<T>,
            ExistsWithPrimaryKeyMapper<T> {
    
    }
    
    @RegisterMapper
    public interface SelectOneMapper<T> {
    
        /**
         * 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
         *
         * @param record
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        T selectOne(T record);
    
    }
    
    @RegisterMapper
    public interface SelectMapper<T> {
    
        /**
         * 根据实体中的属性值进行查询,查询条件使用等号
         *
         * @param record
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        List<T> select(T record);
    
    }
    
    @RegisterMapper
    public interface SelectAllMapper<T> {
    
        /**
         * 查询全部结果
         *
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        List<T> selectAll();
    }
    
    @RegisterMapper
    public interface SelectCountMapper<T> {
    
        /**
         * 根据实体中的属性查询总数,查询条件使用等号
         *
         * @param record
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        int selectCount(T record);
    
    }
    
    @RegisterMapper
    public interface SelectByPrimaryKeyMapper<T> {
    
        /**
         * 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
         *
         * @param key
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        T selectByPrimaryKey(Object key);
    
    }
    
    @RegisterMapper
    public interface ExistsWithPrimaryKeyMapper<T> {
    
        /**
         * 根据主键字段查询总数,方法参数必须包含完整的主键属性,查询条件使用等号
         *
         * @param key
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        boolean existsWithPrimaryKey(Object key);
    
    }

        ② BaseInsertMapper<T>:定义基本的插入操作

    /**
     * 通用Mapper接口,基础查询
     *
     * @param <T> 不能为空
     * @author liuzh
     */
    @RegisterMapper
    public interface BaseInsertMapper<T> extends
            InsertMapper<T>,
            InsertSelectiveMapper<T> {
    
    }
    
    @RegisterMapper
    public interface InsertMapper<T> {
    
        /**
         * 保存一个实体,null的属性也会保存,不会使用数据库默认值
         *
         * @param record
         * @return
         */
        @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
        int insert(T record);
    
    }
    
    @RegisterMapper
    public interface InsertSelectiveMapper<T> {
    
        /**
         * 保存一个实体,null的属性不会保存,会使用数据库默认值
         *
         * @param record
         * @return
         */
        @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
        int insertSelective(T record);
    
    }

        ③ BaseUpdateMapper<T>:定义基本的更新操作

    /**
     * 通用Mapper接口,基础查询
     *
     * @param <T> 不能为空
     * @author liuzh
     */
    @RegisterMapper
    public interface BaseUpdateMapper<T> extends
            UpdateByPrimaryKeyMapper<T>,
            UpdateByPrimaryKeySelectiveMapper<T> {
    
    }
    
    @RegisterMapper
    public interface UpdateByPrimaryKeyMapper<T> {
    
        /**
         * 根据主键更新实体全部字段,null值会被更新
         *
         * @param record
         * @return
         */
        @UpdateProvider(type = BaseUpdateProvider.class, method = "dynamicSQL")
        int updateByPrimaryKey(T record);
    
    }
    
    @RegisterMapper
    public interface UpdateByPrimaryKeySelectiveMapper<T> {
    
        /**
         * 根据主键更新属性不为null的值
         *
         * @param record
         * @return
         */
        @UpdateProvider(type = BaseUpdateProvider.class, method = "dynamicSQL")
        int updateByPrimaryKeySelective(T record);
    
    }

        ④ BaseDeleteMapper<T>:定义基本的删除操作

    /**
     * 通用Mapper接口,基础删除
     *
     * @param <T> 不能为空
     * @author liuzh
     */
    @RegisterMapper
    public interface BaseDeleteMapper<T> extends
            DeleteMapper<T>,
            DeleteByPrimaryKeyMapper<T> {
    
    }
    
    @RegisterMapper
    public interface DeleteMapper<T> {
    
        /**
         * 根据实体属性作为条件进行删除,查询条件使用等号
         *
         * @param record
         * @return
         */
        @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL")
        int delete(T record);
    
    }
    
    @RegisterMapper
    public interface DeleteByPrimaryKeyMapper<T> {
    
        /**
         * 根据主键字段进行删除,方法参数必须包含完整的主键属性
         *
         * @param key
         * @return
         */
        @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL")
        int deleteByPrimaryKey(Object key);
    
    }

      (2)ExampleMapper<T>

    /**
     * 通用Mapper接口,Example查询
     *
     * @param <T> 不能为空
     * @author liuzh
     */
    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface ExampleMapper<T> extends
            SelectByExampleMapper<T>,
            SelectOneByExampleMapper<T>,
            SelectCountByExampleMapper<T>,
            DeleteByExampleMapper<T>,
            UpdateByExampleMapper<T>,
            UpdateByExampleSelectiveMapper<T> {
    
    }

        ① SelectByExampleMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface SelectByExampleMapper<T> {
    
        /**
         * 根据Example条件进行查询
         *
         * @param example
         * @return
         */
        @SelectProvider(type = ExampleProvider.class, method = "dynamicSQL")
        List<T> selectByExample(Object example);
    
    }

        ② SelectOneByExampleMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface SelectOneByExampleMapper<T> {
    
        /**
         * 根据Example条件进行查询
         *
         * @param example
         * @return
         */
        @SelectProvider(type = ExampleProvider.class, method = "dynamicSQL")
        T selectOneByExample(Object example);
    
    }

        ③ SelectCountByExampleMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface SelectCountByExampleMapper<T> {
    
        /**
         * 根据Example条件进行查询总数
         *
         * @param example
         * @return
         */
        @SelectProvider(type = ExampleProvider.class, method = "dynamicSQL")
        int selectCountByExample(Object example);
    
    }

        ④ DeleteByExampleMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface DeleteByExampleMapper<T> {
    
        /**
         * 根据Example条件删除数据
         *
         * @param example
         * @return
         */
        @DeleteProvider(type = ExampleProvider.class, method = "dynamicSQL")
        int deleteByExample(Object example);
    
    }

        ⑤ UpdateByExampleMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface UpdateByExampleMapper<T> {
    
        /**
         * 根据Example条件更新实体`record`包含的全部属性,null值会被更新
         *
         * @param record
         * @param example
         * @return
         */
        @UpdateProvider(type = ExampleProvider.class, method = "dynamicSQL")
        int updateByExample(@Param("record") T record, @Param("example") Object example);
    
    }

        ⑥ UpdateByExampleSelectiveMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface UpdateByExampleSelectiveMapper<T> {
    
        /**
         * 根据Example条件更新实体`record`包含的不是null的属性值
         *
         * @param record
         * @param example
         * @return
         */
        @UpdateProvider(type = ExampleProvider.class, method = "dynamicSQL")
        int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
    
    }

      (3)RowBoundsMapper<T>

    /**
     * 通用Mapper接口,带RowBounds参数的查询
     * <p/>
     * 配合分页插件PageHelper可以实现物理分页
     * <p/>
     * PageHelper - http://git.oschina.net/free/Mybatis_PageHelper
     *
     * @param <T> 不能为空
     * @author liuzh
     */
    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface RowBoundsMapper<T> extends
            SelectByExampleRowBoundsMapper<T>,
            SelectRowBoundsMapper<T> {
    
    }

        ① SelectByExampleRowBoundsMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface SelectByExampleRowBoundsMapper<T> {
    
        /**
         * 根据example条件和RowBounds进行分页查询
         *
         * @param example
         * @param rowBounds
         * @return
         */
        @SelectProvider(type = ExampleProvider.class, method = "dynamicSQL")
        List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);
    
    }

        ② SelectRowBoundsMapper<T>

    @tk.mybatis.mapper.annotation.RegisterMapper
    public interface SelectRowBoundsMapper<T> {
    
        /**
         * 根据实体属性和RowBounds进行分页查询
         *
         * @param record
         * @param rowBounds
         * @return
         */
        @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
        List<T> selectByRowBounds(T record, RowBounds rowBounds);
    
    }

      (4)Marker

    /**
     * 标记接口,继承该接口的接口,在MapperScannerConfigurer#setMarkerInterface时,会自动注册到通用Mapper
     *
     * @author liuzh
     * @since 3.2.2
     */
    public interface Marker {
    
    }

    3、

  • 相关阅读:
    类变量、成员变量、实例变量、局部变量、静态变量、全局变量的解释
    String的属性和方法
    数组的扩容
    以下实例演示了如何通过 List 类的 Arrays.toString () 方法和 List 类的 list.Addall(array1.asList(array2) 方法将两个数组合并为一个数组
    数组获取最大值和最小值
    Java字符串反转
    IO-3
    IO-2
    IO流
    泛型、MAP集合
  • 原文地址:https://www.cnblogs.com/niujifei/p/15266403.html
Copyright © 2020-2023  润新知