1 Star 0 Fork 763

sxw / Mapper

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

MyBatis通用Mapper3

Build Status Maven central Dependency Status

通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。

极其方便的使用MyBatis单表的增删改查。

支持单表操作,不支持通用的多表联合查询。

新书《MyBatis 从入门到精通》

MyBatis 从入门到精通

购买地址:京东当当亚马逊

CSDN博客:http://blog.csdn.net/isea533/article/details/73555400

GitHub项目:https://github.com/mybatis-book/book

通用 Mapper 支持 Mybatis-3.2.4 及以上版本

不是表中字段的属性必须加 @Transient 注解

Spring DevTools 配置

感谢emf1002提供的解决方案。

在使用 DevTools 时,通用Mapper经常会出现 class x.x.A cannot be cast to x.x.A

同一个类如果使用了不同的类加载器,就会产生这样的错误,所以解决方案就是让通用Mapper和实体类使用相同的类加载器即可。

DevTools 默认会对 IDE 中引入的所有项目使用 restart 类加载器,对于引入的 jar 包使用 base 类加载器,因此只要保证通用Mapper的jar包使用 restart 类加载器即可。

src/main/resources 中创建 META-INF 目录,在此目录下添加 spring-devtools.properties 配置,内容如下:

restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar

使用这个配置后,就会使用 restart 类加载加载 include 进去的 jar 包。

项目文档

https://mapperhelper.github.io

在你打算使用通用 Mapper 前,一定要看看下面的文档,许多人在初次使用时遇到的问题,99% 都在文档中有说明!!

  1. 如何集成通用Mapper
  2. 如何使用通用Mapper
  3. 3.3.0版本新增功能用法文档
  4. 根据需要自定义接口
  5. Mapper3通用接口大全
  6. 扩展通用接口
  7. 使用Mapper专用的MyBatis生成器插件
  8. 在Spring4中使用通用Mapper
  9. Mapper3常见问题和用法

如何让作者为你开发通用方法?

实际上,只要你看看上面的第 6 个文档,你完全可以自己开发出来。

或者你可以通过赞助作者 10~50 元来让作者根据你的需求开发一个通用方法。

赞助后保留截图,将截图和需求内容发邮件到 abel533@gmail.com 和作者联系。

你还可以通过开源中国众包购买服务开发 MyBatis 通用 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> {
}

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

实体类注解

从上面效果来看也能感觉出这是一种类似hibernate的用法,因此也需要实体和表对应起来,因此使用了JPA注解。更详细的内容可以看下面的项目文档

Country代码:

public class Country {
    @Id
    private Integer id;
    @Column
    private String countryname;
    private String countrycode;
    //省略setter和getter方法
}

使用Mapper专用的MyBatis Generator插件 可以方便的生成这些(带注解的)实体类。

使用 Maven

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>最新版本</version>
</dependency>

如果你使用 Spring Boot 可以直接引入:

<!--mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>最新版本</version>
</dependency>

具体用法可以参考:MyBatis-Spring-Boot

引入 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/

更新日志

##作者信息

MyBatis 工具网站:http://mybatis.tk

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

作者邮箱: abel533@gmail.com

如需加群,请通过 http://mybatis.tk 首页按钮加群。

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

The MIT License (MIT) Copyright (c) 2014-2017 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单表的增删改查 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

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

搜索帮助