This action will force synchronization from abel533/Mapper, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
#MyBatis通用Mapper3
##极其方便的使用MyBatis单表的增删改查
##支持单表操作,不支持通用的多表联合查询
##优点?
通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。
##MyBatis工具网站:http://mybatis.tk
##特别强调:不是表中字段的属性必须加@Transient
注解
##项目文档
###在你打算使用通用Mapper前,一定要看看下面的文档,许多人在初次使用时遇到的问题,99%都在文档中有说明!!
##通用Mapper - 简单用法示例
全部针对单表操作,每个实体类都需要继承通用Mapper接口来获得通用方法。
示例代码:
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
//查询全部
List<Country> countryList = mapper.select(new Country());
//总数
Assert.assertEquals(183, countryList.size());
//通用Example查询
Example example = new Example(Country.class);
example.createCriteria().andGreaterThan("id", 100);
countryList = mapper.selectByExample(example);
Assert.assertEquals(83, countryList.size());
//MyBatis-Generator生成的Example查询
CountryExample example2 = new CountryExample();
example2.createCriteria().andIdGreaterThan(100);
countryList = mapper.selectByExample(example2);
Assert.assertEquals(83, countryList.size());
CountryMapper代码如下:
public interface CountryMapper extends Mapper<Country> {
}
这里不说更具体的内容,如果您有兴趣,可以查看下面的项目文档
##实体类注解
从上面效果来看也能感觉出这是一种类似hibernate的用法,因此也需要实体和表对应起来,因此使用了JPA注解。更详细的内容可以看下面的项目文档。
Country代码:
public class Country {
@Id
private Integer id;
@Column
private String countryname;
private String countrycode;
//省略setter和getter方法
}
使用Mapper专用的MyBatis Generator插件 可以方便的生成这些(带注解的)实体类。
##通用Mapper支持Mybatis-3.2.4及以上版本
##使用Maven
###重要提示,3.1.0及以后版本的groupId修改为tk.mybatis,artifactId为mapper
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.7</version>
</dependency>
##引入Jar包,下载地址:
https://oss.sonatype.org/content/repositories/releases/tk/mybatis/mapper
http://repo1.maven.org/maven2/tk/mybatis/mapper
由于通用Mapper依赖JPA,所以还需要下载persistence-api-1.0.jar:
http://repo1.maven.org/maven2/javax/persistence/persistence-api/1.0/
##更新日志
##最新版本3.3.8 - 2016-03-23
Example
的andIn
和andNotIn
方法参数改为Collection
#109lazy
方法导致无法兼容3.2.4~3.2.5版本的问题,仍然兼容3.2.4+###3.3.7 - 2016-03-12
Example
增加orderBy
方法,使用属性进行排序,例如:example.orderBy("id").desc().orderBy("countryname").orderBy("countrycode").asc();
resultMap
中不使用javaType
,这种情况如果出错,可以通过@ColumnType
注解设置jdbcType
#103transient
类型的字段#106###3.3.6 - 2016-02-20
###3.3.5 - 2016-02-16
Example
增加对动态表名支持,通过setTableName
设置表名update
方法中,参数为实体类和Example
,这个方法只能通过Example
来设置动态表名,不支持通过实体设置动态表名select count
查询,当表只有一个主键的时候,使用select count(pk)
,其他时候使用select count(*)
###3.3.4 - 2016-01-05
Example
构造方法增加notNull
参数,默认false
,允许值为null
,值为null
的时候不加入到条件中。seqFormat
格式化参数增加第四个可配置值TableName
,该属性的具体含义请参考如何集成通用Mapper中的参数介绍###3.3.3 - 2015-12-30
###3.3.2 - 2015-12-12
tk.mybatis.spring.mapper.MapperScannerConfigurer
中的属性mapperHelper
增加setter和getter方法,方便通过代码进行配置###3.3.1 - 2015-12-09
enableMethodAnnotation
参数,可以控制是否支持方法上的JPA注解,默认false
。
设置enableMethodAnnotation = true
的时候注意,如getRealName
或setYourName
都会产生realName
属性或yourName
属性,如果该方法对应的属性不是表中的字段,就需要给方法增加@Transient
注解。
同样如果你的实体是继承Map
类型的,你不需要在实体中写private String userName
这样的属性,你只需要写setUserName
或getUserName
这样的方法就可以。Field
获取,然后是setter
方法,最后是getter
方法,注解重复的情况下,只获取按顺序得到的第一个public class Country extends Entity<Integer, String>
这样的泛型类型,在生成#{propertyName}
的时候都带上了javaType
属性。
产生的结果就是#{propertyName, javaType=java.lang.Integer}
这样子的,这会导致当你调用方法时,必须保证类型一致。
也就是假设主键是Integer id
,调用selectByPrimaryKey(Object id)
的时候,参数id
必须使用100
这样的数字,不能使用"100"
字符串(以前版本可以)。
如果不带javaType
,那么如果id
是个泛型,MyBatis查找的时候就会因为找不到正确的类型而抛出异常。tk.mybatis.mapper.provider
包下所有的通用接口的实现方法改为了String
形式。
自己扩展单表操作的方法是非常容易的事情,建议有一定通用Mapper使用基础的自行扩展,扩展可以参考如何扩展通用接口
SqlHelper
工具类,其中包含了大量可用的现成的SQL方法@Column
注解增加对insertable
和updatable
属性的支持###3.3.0 - 2015-11-01
增加对动态表名的支持,需要实体类继承IDynamicTableName
接口,用法见详细说明
Example
增加自定义查询条件,提供了4个方法,具体方法和用法见详细说明
新增@ColumnType
注解,可以单独设置列的jdbcType
和typeHandler
Example
的in
和not in
中的List<Object>
参数改为List<?>
,允许任意类型
select查询方法返回类型不在使用resultType
,改为resultMap
,因此可以支持typeHandler
的读取
Style
自动转方式新增camelhumpAndUppercase
驼峰转下划线大写形式,camelhumpAndLowercase
驼峰转下划线小写形式
MapperTemplate中的getSelectReturnType
方法改为getEntityClass
,getBEFORE
改为isBEFORE
文档中增加@GeneratedValue(strategy = GenerationType.IDENTITY)
的一种重要用法说明
修复selectAll不支持@OrderBy
注解的bug
解决一个驼峰转换bug,例如helloWorld
会转换为hello_world
(原先是hello_World
)
###3.2.2 - 2015-09-19
markerInterface
属性配置通用接口(注意该属性的原有作用不变),想要让该接口自动注册,该接口就需要继承tk.mybatis.mapper.common.Marker
接口,Mapper<T>
默认继承该接口,所以如果自己的接口是继承Mapper<T>
的,不需要再继承。###3.2.1 - 2015-09-02
###3.2.0 - 2015-09-01
MapperInterceptor
拦截器,以后不能在通过拦截器配置tk.mybatis.spring.mapper.MapperScannerConfigurer
tk.mybatis.spring.mapper.MapperFactoryBean
MapperScannerConfigurer
中使用properties
属性注入配置org.mybatis.xxx
改为了tk.mybatis.xxx
,名字相近,更方便修改配置<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
style
属性配置,用来配置对象名/字段和表名/字段之间的转换方式,可选值:
normal
:使用实体类名/属性名作为表名/字段名camelhump
:这是默认值,驼峰转换为下划线形式uppercase
:转换为大写lowercase
:转换为小写@NameStyle
,该注解优先于全局配置style
example.selectProperties
映射错误的bug#48
###3.1.3 - 2015-08-25
MapperOnceInterceptor
拦截器,下个版本会完善MapperHelper
的配置方式Example
增加了example.selectProperties("id", "countryname", ...)
方法,可以指定查询列,注意这里参数写的是属性名,Example
会自动映射到列名Example
增加andEqualTo(实体对象)
方法,可以将一个实体放进去,会自动根据属性和值拼出column=value的条件 Bob - 0haizhu0@gmail.com 提供
<cache/>
和@CacheNamespace
的时候不统一,只有一个能生效,这导致xml中配置二级缓存对通用Mapper注解形式的方法无效,该问题已解决@Select
等注解定义的这种仍然无效,这种情况只能在xml中定义###3.1.2 - 2015-07-14
@Column(name="
desc")
的时候,就不需要自动添加别名###3.1.1 - 2015-07-01
ConditionMapper
中selectByCondition
和updateByCondition
方法错误###3.1.0 - 2015-06-10
com.github.abel533
改为tk.mybatis.mapper
tk.mybatis
,artifactId为mapper
###3.0.0 - 2015-06-04
EntityMapper
和SqlMapper
移出,现在是独立项目EntityMapper
Mapper<T>
全部接口方法拆分为独立接口,方便选择集成MySqlMapper<T>
包含批量插入和单个插入,批量插入可以回写全部idRowBoundsMapper<T>
包含两个分页查询,可以配合PageHelper实现物理分页##作者信息
作者博客:http://blog.csdn.net/isea533
作者邮箱: abel533@gmail.com
推荐使用Mybatis分页插件:PageHelper分页插件
Sign in to post a comment
Repository Comments ( 0 )