mybatis 版的 spring-data-jpa mybatis-jpa

码农软件 · 软件分类 · ORM/持久层框架 · 2019-09-23 07:58:55

软件介绍

mybatis-jpa

当时看到spring-data-jpa用方法名来定义sql查询感觉非常好,后来用了一段时间,因为spring-data-jpa是基于hibernate,而我的hibernate又是非常不好,于是在网上查询了相关资料,主要借鉴了mybatis通用mapper的实现方式,写了一版mybatis版的spring-data-jpa:

  • 1.x版本预期实现目标:(如没有重大逻辑性错误,1.0正式版本将在2016年11月30日前发布。)  1.0版本主要实现目标为对mysql、sqlserver、oracle等三大数据库的单表操作,并在后续1.x版本中增加其它的数据库单表操作实现

  • 2.x版本展望目标:实现多表的级联查询

如何集成mybatis-jpa:

只需要将org.mybatis.spring.mapper.MapperFactoryBean這个类替换成mybatis-jpa的com.woodws.mybatis.jpa.MyMapperFactoryBean。因为使用了jpa规范所以需要导入javax.persistence相关jar包

```
    <!-- MyBatis配置 -->
    <bean id="sqlSessionFactory" class="com.woodws.mybatis.jpa.JpaMapperFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.woodws"/>
        <property name="mapperLocations" value="${sqlSessionFactory.mapperLocations}" />
    </bean>
```

使用示例

只需要在mapper接口方法中按着方法命名规范命名mybatis-jpa将自动生成相关的sql,mapper.xml中的配置为主,只要当在mapper.xml中没有的sqlId才会自动生成,命名规范可以查询com.woodws.mybatis.jpa.Definition

```
List<User> findOrderByAge();
    List<User> findOrderByAgeDesc();
    List<User> findByNameOrderByAge(String name);
    List<User> findByNameOrAgeGreaterOrderByAge(@Param("name") String name,
                                                @Param("age") Integer age);
    String findNameById(Integer id);
    int updateName(User user);
    List<User> findByNameLike(String name);
```

自定义扩展

当命名规范于自己的习惯不一致的时候可以增加相关的规范,或者想添加命名解析sql查询,如下就是增加FUNCTION方法命名查询(FUNCTION、PROCEDURE在1.0版本中将自带支持)

```
Definition.expand("func","FUNCTION", Function.class);
```

同时希望各位网友能提出宝贵的意见和改进代码,让mybatis-jpa更加完善。

本文地址:https://codercto.com/soft/d/15204.html

Artificial Intelligence

Artificial Intelligence

Stuart Russell、Peter Norvig / Pearson / 2009-12-11 / USD 195.00

The long-anticipated revision of this #1 selling book offers the most comprehensive, state of the art introduction to the theory and practice of artificial intelligence for modern applications. Intell......一起来看看 《Artificial Intelligence》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具