Code Monkey home page Code Monkey logo

tk-mybatis's Introduction

MyBatis通用Mapper3

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

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

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

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

特别强调

项目文档

在你打算使用通用 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 元来让作者根据你的需求开发一个通用方法。

赞助后保留截图,将截图和需求内容发邮件到 [email protected] 和作者联系。

你还可以通过开源**众包购买服务开发 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>3.4.0</version>
</dependency>

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

<!--mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>1.1.0</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

作者邮箱: [email protected]

Mybatis工具群(推荐): Mybatis工具群(2)

Mybatis工具群(2000 人已满): Mybatis工具

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

tk-mybatis's People

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.