1 Star 0 Fork 763

于世鹏 / Mapper

forked from abel533 / Mapper 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

#Mybatis通用Mapper

##极其方便的使用Mybatis单表的增删改查

##优点?

本项目支持两种类型的通用Mapper,这两种Mapper都可以极大的方便开发人员。

为了让您更方便的了解这两种通用Mapper,这里分别贴一段代码来看实际效果。

##通用Mapper

这是本项目提供的第一种通用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> {
}

这里不说更具体的内容,如果您有兴趣,可以查看下面的项目文档

##EntityMapper

这是第二种通用Mapper,EntityMapper可以像Hibernate的session一样操纵全部的实体类,由于可以操纵全部实体,因此不能使用二级缓存。EntityMapper支持通用的Example查询和MGB生成的Example查询。

示例代码:

//获取CommonMapper,继而包装成EntityMapper
CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
EntityMapper entityMapper = new EntityMapper(commonMapper);
//通用Example查询
Example example = new Example(Country.class);
//id > 100 && id <= 150
example.createCriteria().andGreaterThan("id", 100).andLessThanOrEqualTo("id", 150);
//查询总数
int count = entityMapper.countByExample(example);
Assert.assertEquals(50, count);

example = new Example(Country.class);
//countryname like 'A%'
example.createCriteria().andLike("countryname", "A%");
//查询总数
count = entityMapper.countByExample(example);
Assert.assertEquals(12, count);

##实体类注解

从上面效果来看也能感觉出这是一种类似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坐标以及下载地址

###开发版2.3.1-SNAPSHOT - 2015-04-07

  • 完善所有和PrimaryKey有关的通用查询

###最新版本2.3.0 - 2015-04-05

  • Mapper接口和EntityMapper都增加了selectOne方法,该查询返回值最多只能有一个,存在多个时抛出异常

  • Mapper接口和EntityMapper中,返回List的查询方法都支持JPA的@Orderby注解。其中Example查询中的orderby会覆盖注解的@Orderby设置。

  • 通过实体类获取表名的时候,不对表名进行强制的大小写转换。如果数据库大小写敏感,请通过@Table注解和数据库保持一致。

如果你使用Maven,只需要添加如下依赖:

<dependency>
    <groupId>com.github.abel533</groupId>
    <artifactId>mapper</artifactId>
    <version>2.3.0</version>
</dependency>

如果你想引入Jar包,你可以从下面的地址下载:

https://oss.sonatype.org/content/repositories/releases/com/github/abel533/mapper/

http://repo1.maven.org/maven2/com/github/abel533/mapper/

由于通用Mapper依赖JPA,所以还需要下载persistence-api-1.0.jar:

http://repo1.maven.org/maven2/javax/persistence/persistence-api/1.0/

##项目文档

###通用Mapper

  1. 如何集成通用Mapper

  2. 如何使用通用Mapper

  3. 如何开发自己的通用Mapper

  4. 在Spring4中使用通用Mapper

  5. 如何使用Mapper专用的MyBatis Generator插件

###EntityMapper(单一Mapper操作全部实体)

  1. 如何集成EntityMapper

  2. 如何使用EntityMapper

  3. 如何使用Mapper专用的MyBatis Generator插件

###如何使用SqlMapper

  1. 如何使用SqlMapper

##作者信息

作者博客:http://blog.csdn.net/isea533

作者邮箱: abel533@gmail.com

Mybatis工具群: 211286137 (Mybatis相关工具插件等等)

推荐使用Mybatis分页插件:PageHelper分页插件

The MIT License (MIT) Copyright (c) 2014 abel533@gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

极其方便的使用Mybatis单表的增删改查 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/spyu2000/Mapper.git
git@gitee.com:spyu2000/Mapper.git
spyu2000
Mapper
Mapper
master

搜索帮助