设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 软件追踪 查看内容

Mybatis通用Mapper 2.1.0发布

2015-3-10 21:48| 发布者: joejoe0332| 查看: 943| 评论: 0|原作者: oschina|来自: oschina

摘要: Mybatis通用Mapper极其方便的使用Mybatis单表的增删改查本项目支持两种类型的通用Mapper,这两种Mapper都可以极大的方便开发人员。为了让您更方便的了解这两种通用Mapper,这里分别贴一段代码来看实际效果。通用Mapp ...

Mybatis通用Mapper

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

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

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

通用Mapper

这是本项目提供的第一种通用Mapper,优点是可以缓存,全部针对单表操作,每个实体类都需要继承通用Mapper接口来获得通用方法。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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查询。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//获取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插件 可以方便的生成这些(带注解的)实体类。


最新版本2.1.0

  • 通用Mapper接口增加Example查询方法,包括以下方法:

    int selectCountByExample(Object example);

    int deleteByExample(Object example);

    List selectByExample(Object example);

    int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);

    int updateByExample(@Param("record") T record, @Param("example") Object example);

  • 通用Example增加了一个exists的参数,当true的时候如果使用的字段不存在会抛出异常,false时不抛出异常,但是不使用该字段的条件。


项目文档

通用Mapper

  1. 如何集成通用Mapper

  2. 如何使用通用Mapper

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

  4. 在Spring4中使用通用Mapper

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

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

  1. 如何集成EntityMapper

  2. 如何使用EntityMapper

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


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部