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.
通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。
极其方便的使用MyBatis单表的增删改查。
支持单表操作,不支持通用的多表联合查询。
CSDN博客:http://blog.csdn.net/isea533/article/details/73555400
GitHub项目:https://github.com/mybatis-book/book
@Transient
注解感谢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 包。
在你打算使用通用 Mapper 前,一定要看看下面的文档,许多人在初次使用时遇到的问题,99% 都在文档中有说明!!
实际上,只要你看看上面的第 6 个文档,你完全可以自己开发出来。
或者你可以通过赞助作者 10~50 元来让作者根据你的需求开发一个通用方法。
赞助后保留截图,将截图和需求内容发邮件到 abel533@gmail.com 和作者联系。
你还可以通过开源中国众包购买服务开发 MyBatis 通用 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插件 可以方便的生成这些(带注解的)实体类。
<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
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分页插件
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。