tk.mybatis源码

BaseMapper.java

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
    //TODO
    //FIXME 特别注意,该接口不能被扫描到,否则会出错
}

1 、Mapper.java

package tk.mybatis.mapper.common;

/**
 * 通用Mapper接口,其他接口继承该接口即可
 * 

*

这是一个例子,自己扩展时可以参考

*

*

项目地址 : https://github.com/abel533/Mapper

* * @param 不能为空 * @author liuzh */
public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { }

1.1 BaseMapper.java

package tk.mybatis.mapper.common;

import tk.mybatis.mapper.common.base.BaseDeleteMapper;
import tk.mybatis.mapper.common.base.BaseInsertMapper;
import tk.mybatis.mapper.common.base.BaseSelectMapper;
import tk.mybatis.mapper.common.base.BaseUpdateMapper;

/**
 * 通用Mapper接口,其他接口继承该接口即可
 * 

*

这是一个例子,自己扩展时可以参考

*

*

项目地址 : https://github.com/abel533/Mapper

* * @param 不能为空 * @author liuzh */
public interface BaseMapper extends BaseSelectMapper, BaseInsertMapper, BaseUpdateMapper, BaseDeleteMapper { }

1.1.1 BaseSelectMapper.java


package tk.mybatis.mapper.common.base;

import tk.mybatis.mapper.common.base.select.*;

/**
 * 通用Mapper接口,基础查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface BaseSelectMapper<T> extends
        SelectOneMapper<T>,
        SelectMapper<T>,
        SelectAllMapper<T>,
        SelectCountMapper<T>,
        SelectByPrimaryKeyMapper<T>,
        ExistsWithPrimaryKeyMapper<T> {

}
1.1.1.1 SelectOneMapper.java
package tk.mybatis.mapper.common.base.select;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

/**
 * 通用Mapper接口,查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface SelectOneMapper {

    /**
     * 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
     *
     * @param record
     * @return
     */
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    T selectOne(T record);

}
1.1.1.2 SelectMapper.java
package tk.mybatis.mapper.common.base.select;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

import java.util.List;

/**
 * 通用Mapper接口,查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface SelectMapper {

    /**
     * 根据实体中的属性值进行查询,查询条件使用等号
     *
     * @param record
     * @return
     */
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    List select(T record);

}
1.1.1.3 SelectAllMapper.java
package tk.mybatis.mapper.common.base.select;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

import java.util.List;

/**
 * @author liuzh
 */
public interface SelectAllMapper {

    /**
     * 查询全部结果
     *
     * @return
     */
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    List selectAll();
}
1.1.1.4 SelectCountMapper.java
package tk.mybatis.mapper.common.base.select;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

/**
 * 通用Mapper接口,查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface SelectCountMapper {

    /**
     * 根据实体中的属性查询总数,查询条件使用等号
     *
     * @param record
     * @return
     */
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    int selectCount(T record);

}
1.1.1.5 SelectByPrimaryKeyMapper.java
package tk.mybatis.mapper.common.base.select;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

/**
 * 通用Mapper接口,其他接口继承该接口即可
 * 

*

这是一个例子,自己扩展时可以参考

*

*

项目地址 : https://github.com/abel533/Mapper

* * @param 不能为空 * @author liuzh */
public interface SelectByPrimaryKeyMapper { /** * 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号 * * @param key * @return */ @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL") T selectByPrimaryKey(Object key); }
1.1.1.6 ExistsWithPrimaryKeyMapper.java
package tk.mybatis.mapper.common.base.select;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

/**
 * 通用Mapper接口,查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface ExistsWithPrimaryKeyMapper {

    /**
     * 根据主键字段查询总数,方法参数必须包含完整的主键属性,查询条件使用等号
     *
     * @param key
     * @return
     */
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    boolean existsWithPrimaryKey(Object key);

}

1.1.2 BaseInsertMapper.java

package tk.mybatis.mapper.common.base;

import tk.mybatis.mapper.common.base.insert.InsertMapper;
import tk.mybatis.mapper.common.base.insert.InsertSelectiveMapper;

/**
 * 通用Mapper接口,基础查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface BaseInsertMapper<T> extends
        InsertMapper<T>,
        InsertSelectiveMapper<T> {

}
1.1.2.1 InsertMapper.java
package tk.mybatis.mapper.common.base.insert;

import org.apache.ibatis.annotations.InsertProvider;
import tk.mybatis.mapper.provider.base.BaseInsertProvider;

/**
 * 通用Mapper接口,插入
 *
 * @param  不能为空
 * @author liuzh
 */
public interface InsertMapper {

    /**
     * 保存一个实体,null的属性也会保存,不会使用数据库默认值
     *
     * @param record
     * @return
     */
    @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
    int insert(T record);

}
1.1.2.2 InsertSelectiveMapper.java
package tk.mybatis.mapper.common.base.insert;

import org.apache.ibatis.annotations.InsertProvider;
import tk.mybatis.mapper.provider.base.BaseInsertProvider;

/**
 * 通用Mapper接口,插入
 *
 * @param  不能为空
 * @author liuzh
 */
public interface InsertSelectiveMapper {

    /**
     * 保存一个实体,null的属性不会保存,会使用数据库默认值
     *
     * @param record
     * @return
     */
    @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
    int insertSelective(T record);

}

1.1.3 BaseUpdateMapper.java

package tk.mybatis.mapper.common.base;

import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeyMapper;
import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeySelectiveMapper;

/**
 * 通用Mapper接口,基础查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface BaseUpdateMapper<T> extends
        UpdateByPrimaryKeyMapper<T>,
        UpdateByPrimaryKeySelectiveMapper<T> {

}
1.1.3.1 UpdateByPrimaryKeyMapper.java
package tk.mybatis.mapper.common.base.update;

import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.UpdateProvider;
import tk.mybatis.mapper.provider.base.BaseUpdateProvider;

/**
 * 通用Mapper接口,更新
 *
 * @param  不能为空
 * @author liuzh
 */
public interface UpdateByPrimaryKeyMapper {

    /**
     * 根据主键更新实体全部字段,null值会被更新
     *
     * @param record
     * @return
     */
    @UpdateProvider(type = BaseUpdateProvider.class, method = "dynamicSQL")
    @Options(useCache = false, useGeneratedKeys = false)
    int updateByPrimaryKey(T record);

}
1.1.3.2 UpdateByPrimaryKeySelectiveMapper.java
package tk.mybatis.mapper.common.base.update;

import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.UpdateProvider;
import tk.mybatis.mapper.provider.base.BaseUpdateProvider;

/**
 * 通用Mapper接口,更新
 *
 * @param  不能为空
 * @author liuzh
 */
public interface UpdateByPrimaryKeySelectiveMapper {

    /**
     * 根据主键更新属性不为null的值
     *
     * @param record
     * @return
     */
    @UpdateProvider(type = BaseUpdateProvider.class, method = "dynamicSQL")
    @Options(useCache = false, useGeneratedKeys = false)
    int updateByPrimaryKeySelective(T record);

}

1.1.4 BaseDeleteMapper.java

package tk.mybatis.mapper.common.base;

import tk.mybatis.mapper.common.base.delete.DeleteByPrimaryKeyMapper;
import tk.mybatis.mapper.common.base.delete.DeleteMapper;

/**
 * 通用Mapper接口,基础删除
 *
 * @param  不能为空
 * @author liuzh
 */
public interface BaseDeleteMapper<T> extends
        DeleteMapper<T>,
        DeleteByPrimaryKeyMapper<T> {

}
1.1.4.1 DeleteMapper.java
package tk.mybatis.mapper.common.base.delete;

import org.apache.ibatis.annotations.DeleteProvider;
import tk.mybatis.mapper.provider.base.BaseDeleteProvider;

/**
 * 通用Mapper接口,删除
 *
 * @param  不能为空
 * @author liuzh
 */
public interface DeleteMapper {

    /**
     * 根据实体属性作为条件进行删除,查询条件使用等号
     *
     * @param record
     * @return
     */
    @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL")
    int delete(T record);

}
1.1.4.2 DeleteByPrimaryKeyMapper.java
package tk.mybatis.mapper.common.base.delete;

import org.apache.ibatis.annotations.DeleteProvider;
import tk.mybatis.mapper.provider.base.BaseDeleteProvider;

/**
 * 通用Mapper接口,删除
 *
 * @param  不能为空
 * @author liuzh
 */
public interface DeleteByPrimaryKeyMapper {

    /**
     * 根据主键字段进行删除,方法参数必须包含完整的主键属性
     *
     * @param key
     * @return
     */
    @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL")
    int deleteByPrimaryKey(Object key);

}

1.2 SelectCountByExampleMapper.java

package tk.mybatis.mapper.common.example;

import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.provider.ExampleProvider;

/**
 * 通用Mapper接口,Example查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface SelectCountByExampleMapper {

    /**
     * 根据Example条件进行查询总数
     *
     * @param example
     * @return
     */
    @SelectProvider(type = ExampleProvider.class, method = "dynamicSQL")
    int selectCountByExample(Object example);

}

1.3 DeleteByExampleMapper.java

package tk.mybatis.mapper.common.example;

import org.apache.ibatis.annotations.DeleteProvider;
import tk.mybatis.mapper.provider.ExampleProvider;

/**
 * 通用Mapper接口,Example查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface DeleteByExampleMapper {

    /**
     * 根据Example条件删除数据
     *
     * @param example
     * @return
     */
    @DeleteProvider(type = ExampleProvider.class, method = "dynamicSQL")
    int deleteByExample(Object example);

}

1.4 UpdateByExampleMapper.java

package tk.mybatis.mapper.common.example;

import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.UpdateProvider;
import tk.mybatis.mapper.provider.ExampleProvider;

/**
 * 通用Mapper接口,Example查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface UpdateByExampleMapper {

    /**
     * 根据Example条件更新实体`record`包含的全部属性,null值会被更新
     *
     * @param record
     * @param example
     * @return
     */
    @UpdateProvider(type = ExampleProvider.class, method = "dynamicSQL")
    @Options(useCache = false, useGeneratedKeys = false)
    int updateByExample(@Param("record") T record, @Param("example") Object example);

}

1.5 UpdateByExampleSelectiveMapper.java

package tk.mybatis.mapper.common.example;

import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.UpdateProvider;
import tk.mybatis.mapper.provider.ExampleProvider;

/**
 * 通用Mapper接口,Example查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface UpdateByExampleSelectiveMapper {

    /**
     * 根据Example条件更新实体`record`包含的不是null的属性值
     *
     * @param record
     * @param example
     * @return
     */
    @UpdateProvider(type = ExampleProvider.class, method = "dynamicSQL")
    @Options(useCache = false, useGeneratedKeys = false)
    int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);

}

1.3 RowBoundsMapper.java

package tk.mybatis.mapper.common;

import tk.mybatis.mapper.common.rowbounds.SelectByExampleRowBoundsMapper;
import tk.mybatis.mapper.common.rowbounds.SelectRowBoundsMapper;

/**
 * 通用Mapper接口,带RowBounds参数的查询
 * 

* 配合分页插件PageHelper可以实现物理分页 *

* PageHelper - http://git.oschina.net/free/Mybatis_PageHelper * * @param 不能为空 * @author liuzh */ public interface RowBoundsMapper<T> extends SelectByExampleRowBoundsMapper<T>, SelectRowBoundsMapper<T> { }

1.3.1 SelectByExampleRowBoundsMapper.java

package tk.mybatis.mapper.common.rowbounds;

import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.provider.ExampleProvider;

import java.util.List;

/**
 * 通用Mapper接口,查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface SelectByExampleRowBoundsMapper {

    /**
     * 根据example条件和RowBounds进行分页查询
     *
     * @param example
     * @param rowBounds
     * @return
     */
    @SelectProvider(type = ExampleProvider.class, method = "dynamicSQL")
    List selectByExampleAndRowBounds(Object example, RowBounds rowBounds);

}

1.3.2 SelectRowBoundsMapper.java

package tk.mybatis.mapper.common.rowbounds;

import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

import java.util.List;

/**
 * 通用Mapper接口,查询
 *
 * @param  不能为空
 * @author liuzh
 */
public interface SelectRowBoundsMapper {

    /**
     * 根据实体属性和RowBounds进行分页查询
     *
     * @param record
     * @param rowBounds
     * @return
     */
    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
    List selectByRowBounds(T record, RowBounds rowBounds);

}

1.4 Marker .java

package tk.mybatis.mapper.common;

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

}

2 MySqlMapper.java

package tk.mybatis.mapper.common;

import tk.mybatis.mapper.common.special.InsertListMapper;
import tk.mybatis.mapper.common.special.InsertUseGeneratedKeysMapper;

/**
 * 通用Mapper接口,MySql独有的通用方法
 *
 * @param  不能为空
 * @author liuzh
 */
public interface MySqlMapper<T> extends
        InsertListMapper<T>,
        InsertUseGeneratedKeysMapper<T> {

}

2.1 InsertListMapper.java

package tk.mybatis.mapper.common.special;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import tk.mybatis.mapper.provider.SpecialProvider;

import java.util.List;

/**
 * 通用Mapper接口,特殊方法,批量插入,支持批量插入的数据库都可以使用,例如mysql,h2等
 *
 * @param  不能为空
 * @author liuzh
 */
public interface InsertListMapper {

    /**
     * 批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含`id`属性并且必须为自增列
     *
     * @param recordList
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
    int insertList(List recordList);

    /**
     * ======如果主键不是id怎么用?==========
     * 假设主键的属性名是uid,那么新建一个Mapper接口如下
     * 
        public interface InsertUidListMapper {
            @Options(useGeneratedKeys = true, keyProperty = "uid")
            @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
            int insertList(List recordList);
        }
     * 只要修改keyProperty = "uid"就可以
     *
     * 然后让你自己的Mapper继承InsertUidListMapper即可
     *
     * 
*/
}

2.2 InsertUseGeneratedKeysMapper.java

package tk.mybatis.mapper.common.special;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import tk.mybatis.mapper.provider.SpecialProvider;

/**
 * 通用Mapper接口,特殊方法,批量插入,支持批量插入的数据库都可以使用,例如mysql,h2等
 *
 * @param  不能为空
 * @author liuzh
 */
public interface InsertUseGeneratedKeysMapper {

    /**
     * 插入数据,限制为实体包含`id`属性并且必须为自增列,实体配置的主键策略无效
     *
     * @param record
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
    int insertUseGeneratedKeys(T record);

    /**
     * ======如果主键不是id怎么用?==========
     * 假设主键的属性名是uid,那么新建一个Mapper接口如下
     * 
        public interface InsertUidMapper {
            @Options(useGeneratedKeys = true, keyProperty = "id")
            @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
            int insertUseGeneratedKeys(T record);
        }
     * 只要修改keyProperty = "uid"就可以
     *
     * 然后让你自己的Mapper继承InsertUidListMapper即可
     *
     * 
*/
}

你可能感兴趣的:(mybatis)