ReBatis

Generic mybatis mapper interface without write SQL yourself

License

License

GroupId

GroupId

com.lihongkun
ArtifactId

ArtifactId

rebatis
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

ReBatis
Generic mybatis mapper interface without write SQL yourself
Source Code Management

Source Code Management

https://github.com/lihongkun/rebatis

Download rebatis

How to add to project

<!-- https://jarcasting.com/artifacts/com.lihongkun/rebatis/ -->
<dependency>
    <groupId>com.lihongkun</groupId>
    <artifactId>rebatis</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.lihongkun/rebatis/
implementation 'com.lihongkun:rebatis:1.0.0'
// https://jarcasting.com/artifacts/com.lihongkun/rebatis/
implementation ("com.lihongkun:rebatis:1.0.0")
'com.lihongkun:rebatis:jar:1.0.0'
<dependency org="com.lihongkun" name="rebatis" rev="1.0.0">
  <artifact name="rebatis" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.lihongkun', module='rebatis', version='1.0.0')
)
libraryDependencies += "com.lihongkun" % "rebatis" % "1.0.0"
[com.lihongkun/rebatis "1.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework : spring-core jar 4.3.9.RELEASE
org.apache.commons : commons-lang3 jar 3.6
org.slf4j : slf4j-api jar 1.7.3

provided (1)

Group / Artifact Type Version
org.mybatis : mybatis jar 3.4.4

test (7)

Group / Artifact Type Version
org.mybatis : mybatis-spring jar 1.3.1
org.springframework : spring-context jar 4.3.9.RELEASE
org.springframework : spring-jdbc jar 4.3.9.RELEASE
junit : junit jar 4.12
com.h2database : h2 jar 1.4.196
com.alibaba : druid jar 1.1.4
ch.qos.logback : logback-classic jar 1.2.3

Project Modules

There are no modules declared in this project.

功能简介

提供通用SQL注册、分页、SQL打印功能,可以单独使用.如果使用通用SQL注册,将同时开启分页功能.

通用SQL注册

基础的SQL不用再手工写,增加工程效率

  • 类似JPA(Hibernate),使用实体注解.
  • 实现通用的Mapper接口及其SQL注册,无代码生成.
  • 对Mybatis使用无任何侵入,Mybatis原有的使用方式可继续使用.

分页功能

Mybatis的分页参数RowBounds存在序列化问题,业界里面的分页插件在使用的时候通常需要自己手动进行转换,且返回值在一些序列化中会丢失.

  • 分页参数不再继承RowBounds,采用自己写的可序列化的Paginator.
  • 重写Configuration注册Mapper相关的类,增加分页的功能

SQL打印功能

  • 提供通用的SQL打印Inteceptor.

开始使用

  • 实体Bean 增加注解
    自定义注解Table对应了数据库的表名
@Table("t_category")
public class Category {

	@Id(KeyGeneratorType.AUTO)
	private Long id;
	
	private String code;
	
	private String name;
	
	private Integer type;
	
	private String cover;
	...
}
  • Mapper 编写
    BaseMapper自定义了通用的数据库方法
public interface CategoryMapper extends BaseMapper<Category, Long>{

	@Select("select id,code,name,type,cover,created_on createdOn,created_by createdBy,updated_on updatedOn,updated_by updatedBy  from t_category where type = #{type}")
	public List<Category> findByType(@Param("type") Integer type);
	
	@Select("select id,code,name,type,cover,created_on createdOn,created_by createdBy,updated_on updatedOn,updated_by updatedBy  from t_category where type = #{type}")
	public Pagination<Category> findByTypePage(@Param("type") Integer type,Paginator paginator);
	
	@Select("select id,code,name,type,cover,created_on createdOn,created_by createdBy,updated_on updatedOn,updated_by updatedBy  from t_category where type = #{type}")
	public Pagination<Category> findAll2(Category category,Paginator paginator);
}
  • SqlSessionFactoryBean 等相关Bean 注册
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages="com.lihongkun.rebatis.sample.mapper")
public class MybatisConfiguration {
    
	@Autowired
	private DataSource dataSource;
	
	@Bean
	@ConditionalOnMissingBean
	public DataSourceTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean
	@ConditionalOnMissingBean
	public SqlSessionFactory sqlSessionFactory() throws Exception {

		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		// 只需要注入 RebatisConfiguration 并选择启用的功能
		sqlSessionFactory.setConfiguration(new RebatisConfiguration(true, true, true));

		return sessionFactory.getObject();
	}

}

测试用例

测试使用了内存数据库h2,源码目录下的RebatisTest和PaginationTest可以直接运行.

其中RebatisTest 演示了BaseMapper的方法,PaginationTest演示了单独使用分页功能的几个方法.

原理

主要扩展了Configuration,在注册Mapper的时候同时对BaseMapper中已定义的接口进行对应的MapperStatement的注册达到了不用自己手写SQL的目的.

联系方式

QQ群 : 477318923

微信公众号 : HelloBackend

Versions

Version
1.0.0
0.0.1