1 Star 0 Fork 1

Deadpool / auto-mapper

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

AutoMapper

AutoMapper是一款适用于Mybatis的SQL生成插件,提供了JPA风格的SQL语句生成能力,用户仅需依赖一个编译期jar包,就能在编译期根据Mapper接口中的方法定义生成相应的XML文件和SQL语句。就像流行的Lombok一样,一切发生在编译期间,因此不会对软件性能造成任何影响,并且用户可以直接在编译完成的target目录或jar中看到生成的SQL语句。

效果展示

下方动图演示了在idea上插件在编译时动态生成SQL的效果,您可以通过Build -> ReBuild Project达到一样的效果。

idea.gif

无论您使用的是什么开发环境,只要编译器是按照标准实现的,都能生成成功。另外在打包发布时,这个过程是无感的,因为在打包编译的过程中AutoMapper会自动生效。

快速开始

首先需要在项目的编译阶段依赖AutoMapper,如果您正在使用maven管理项目,那么添加如下依赖即可:

<dependency>
    <groupId>fun.fengwk.auto-mapper</groupId>
    <artifactId>auto-mapper-processor</artifactId>
    <scope>provided</scope>
    <version>0.0.24</version>
</dependency>

常见的Mybatis使用方式是编写一个*Mapper.java文件在其中定义接口方法,然后编写一个*Mapper.xml文件实现接口方法的SQL语句。例如example模块中(tips:example模块中展示了如何在spring-boot项目里使用AutoMapper)就有ExampleMapper.java文件和ExampleMapper.xml文件,但只要在ExampleMapper类上标记@AutoMapper注解,AutoMapper就可以在编译期间帮助您生成相应符合规范的Mybatis SQL片段语句到相同包路径下的xml文件中(如果已经手动编写了相应方法定义的SQL,那么框架会跳过该方法的自动生成工作)。

@AutoMapper
public interface ExampleMapper {
    
    ExampleDO findById(long id);
    
}

例如定义了如上的ExampleMapper类,那么框架会自动生成如下的Mybatis SQL片段并插入与ExampleMapper类相同包路径下的名称为ExampleMapper.xml文件中,如果不存在ExampleMapper.xml文件那么AutoMapper将会自动生成一个。

<!--auto mapper generate-->
<select id="findById" parameterType="long" resultType="fun.fengwk.automapper.example.model.ExampleDO">
    select id, name, sort
    from example
    where id=#{id}
</select>

语法约定

为了实现SQL生成,必须遵循一定规则来定义Mapper接口的方法,如果您曾使用过JPA,那么这些规则会非常容易理解,因为这些规则几乎完全遵循了JPA的约定。

下标站时刻展示了当前支持的所有模式(如果您了解状态机可以直接查看doc/MapperMethodStateMachine.drawio):

描述 模式 约束
新增 insert[Selective] 单个JavaBean入参
insertAll[Selective] 可迭代的JavaBean入参,如果在该场景下使用了Selective,则需要在jdbc参数中追加allowMultiQueries=true,并且UseGeneratedKeys会失效(确切的来说只会返回第一个SQL的自增键,这是由jdbc的特性决定的,详见JdbcGeneratedKeysTest)
删除 deleteAll 无入参
deleteBy... 多入参数时必须使用@Param注解绑定By后参数与入参关系
修改 updateBy...[Selectivce] 多入参数时必须使用@Param注解绑定By后参数与入参关系
查询 findAll[OrderBy...] 无入参
findBy...[OrderBy...] 多入参数时必须使用@Param注解绑定By后参数与入参关系
计数 countAll 无入参
countBy... 多入参数时必须使用@Param注解绑定By后参数与入参关系
分页 pageAll[OrderBy...] 入参必须拥有limit,可选offset,必须使用@Param注解绑定By后参数与入参关系
pageBy...[OrderBy...] 入参必须拥有limit,可选offset,必须使用@Param注解绑定By后参数与入参关系

下表格展示了当前支持的所有关键字:

关键字 方法名 SQL
And findByLastnameAndFirstname ... where x.lastname = ? and x.firstname = ?
Or findByLastnameOrFirstname ... where x.lastname = ? or x.firstname = ?
Is,Equals findById,findByIdIs,findByIdEquals ... where x.id = ?
LessThan findByAgeLessThan ... where x.age < ?
LessThanEquals findByAgeLessThanEquals ... where x.age <= ?
GreaterThan findByAgeGreaterThan ... where x.age > ?
GreaterThanEquals findByAgeGreaterThanEquals ... where x.age >= ?
After findByIdAfter ... where x.id > ?
Before findByIdBefore ... where x.id < ?
IsNull findByNameIsNull ... where x.name is null
IsNotNull,NotNull findByNameIsNotNull,findByNameNotNull ... where x.name is not null
Like findByNameLike ... where x.name like ?
NotLike findByNameNotLike ... where x.name not like ?
StartingWith findByNameStartingWith ... where x.name not like '?%'
EndingWith findByNameEndingWith ... where x.name not like '%?'
Containing findByNameContaining ... where x.name not like '%?%'
OrderBy findByIdOrderByIdDesc ... where x.id = 1 order by x.id desc
Not findByNameNot ... where x.name != ?
In findByIdIn(Collection) ... where x.id in (...)
NotIn findByIdNotIn(Collection) ... where x.id not in (...)

注解支持

支持自定义字段名:可以在入参或字段上添加@FieldName注解支持自定义数据库字段名称。

支持useGeneratedKeys:可以在字段上添加@UseGeneratedKeys注解支持Mybatis的useGeneratedKeys功能。

使用@ExcludeField可以忽略insert或update方法中的指定字段。

使用@IncludeField可以只指定insert或update方法中的指定字段。

使用@Selective可以指明where查询字段是否为可选的。

全局配置

尽管我们可以在@AutoMapper注解中修改当前类的配置,但如果需要进行全局配置,可以在resource根目录下定义auto-mapper.config文件作为全局配置,优先级为:用户明确指定的注解配置 > 全局配置 > 默认配置。

fun.fengwk.automapper.annotation.AutoMapper.dbType=MYSQL
fun.fengwk.automapper.annotation.AutoMapper.mapperSuffix=Mapper
fun.fengwk.automapper.annotation.AutoMapper.tableNamingStyle=LOWER_UNDER_SCORE_CASE
fun.fengwk.automapper.annotation.AutoMapper.fieldNamingStyle=LOWER_UNDER_SCORE_CASE
fun.fengwk.automapper.annotation.AutoMapper.tableNamePrefix=test_

编译信息

AutoMapper在编译期会打印这以下几种常见信息:

  • 映射完成写入目标xml文件。
  • 用户已经编写过要映射的方法的sql片段,AutoMapper将跳过这一方法的处理。
  • 用户没有编写过要映射的方法并且自动映射失败,此时会报告错误并中断编译,此时用户需要确认是否需要使用AutoMapper映射此方法,如果需要则需修正方法语法,否则可以自行在xml文件中定义相应方法对应的sql片段。

MySQL特性

当在@AutoMapper注解或全局配置中指定了dbType为MYSQL时可以使用一些特有的语法:

  • 使用insertIgnore代替insert可使用insert ignore into语法。
  • 使用replace代替insert可使用replace into语法。
  • 使用findLockInShareMode代替find可使用select ... lock in share mode语法。
  • 使用findForUpdate代替find可使用select ... for update语法。
  • 对于like语句,将使用concat拼接防止SQL注入。

应用示例

下面将会展示一些具体的示例(这些示例均来自于example模块,可以结合具体代码理解)帮助您了解如何使用规则定义接口方法。

示例一

方法

int insert(ExampleDO exampleDO);

SQL片段

<!--auto mapper generate-->
<insert id="insert" keyProperty="id" parameterType="fun.fengwk.automapper.example.model.ExampleDO" useGeneratedKeys="true">
    insert into example (name, sort) values
    (#{name}, #{sort})
</insert>

说明

其中name和sort是通过读取ExampleDO中字段获取的,并且Java字段名称会通过@AutoMapper中定义中fieldNamingStyle转换为相应的数据库字段格式,如果没有办法通过常规的规则转换,也可以通过@FieldName注解直接指定数据库字段名称,如果使用了自增主键,使用@UseGeneratedKeys注解可以对相应字段开启Mybatis的useGeneratedKeys属性并且在insert语句中忽略该字段的插入。

示例二

方法

int deleteById(long id);

SQL片段

<!--auto mapper generate-->
<delete id="deleteById" parameterType="long">
    delete from example
    where id=#{id}
</delete>

说明

AutoMapper根据By后条件生成where语句,因为只有一个参数不必添加@Param注解来说明参数名称。

示例三

方法

int updateById(ExampleDO exampleDO);

SQL片段

<!--auto mapper generate-->
<update id="updateById" parameterType="fun.fengwk.automapper.example.model.ExampleDO">
    update example set id=#{id}, name=#{name}, sort=#{sort}
    where id=#{id}
</update>

说明

AutoMapper通过读取入参ExampleDO类的字段生成set语句,并且根据By后条件生成where语句。

示例四

方法

ExampleDO findById(long id);

SQL片段

<!--auto mapper generate-->
<select id="findById" parameterType="long" resultType="fun.fengwk.automapper.example.model.ExampleDO">
    select id, name, sort
    from example
    where id=#{id}
</select>

说明

AutoMapper通过读取返回值ExampleDO类中的字段生成select语句,并且根据By后条件生成where语句,因为只有一个参数不必添加@Param注解来说明参数名称。

示例五

方法

List<ExampleDO> findByNameStartingWith(String name);

SQL片段

<!--auto mapper generate-->
<select id="findByNameStartingWith" parameterType="java.lang.String" resultType="fun.fengwk.automapper.example.model.ExampleDO">
    select id, name, sort
    from example
    where name like concat(#{name}, '%')
</select>

说明

AutoMapper通过读取返回值泛型ExampleDO类中的字段生成select语句,根据By后条件生成where语句,因为只有一个参数不必添加@Param注解来说明参数名称。

示例六

方法

List<ExampleDO> findByNameStartingWithAndSortGreaterThanEqualsOrderBySortDesc(@Param("name") String name, @Param("sort") int sort);

SQL片段

<!--auto mapper generate-->
<select id="findByNameStartingWithAndSortGreaterThanEqualsOrderBySortDesc" resultType="fun.fengwk.automapper.example.model.ExampleDO">
    select id, name, sort
    from example
    where name like concat(#{name}, '%')
    and sort&gt;=#{sort}
    order by sort desc
</select>

说明

AutoMapper通过读取返回值泛型ExampleDO类中的字段生成select语句,根据By后条件生成where语句,因为只有多个参数必须添加@Param注解来说明参数名称。

示例七

方法

List<E> pageAll(@Param("offset") int offset, @Param("limit") int limit);

SQL片段

<!--auto mapper generate-->
<select id="pageAll" resultType="fun.fengwk.automapper.example.model.ExampleDO">
    select id, name, sort
    from example
    limit #{offset},#{limit}
</select>

说明

AutoMapper解析父类泛型E得到ExampleDO,继而读取ExampleDO字段生成select语句,由于是page方法,必须添加offset和limit参数。

示例八

方法

@ExcludeField("name")
int insert(ExampleDO exampleDO);

SQL片段

<!--auto mapper generate-->
<insert id="insert" keyProperty="id" parameterType="fun.fengwk.automapper.example.model.ExampleDO" useGeneratedKeys="true">
    insert into example (sort) values
    (#{sort})
</insert>

说明

使用@ExcludeField注解可以去除insert和update语句中不需要的字段。

示例九

方法

@IncludeField("name")
int insert(ExampleDO exampleDO);

SQL片段

<!--auto mapper generate-->
<insert id="insert" keyProperty="id" parameterType="fun.fengwk.automapper.example.model.ExampleDO" useGeneratedKeys="true">
    insert into example (name) values
    (#{name})
</insert>

说明

使用@IncludeField注解可以在insert和update语句中仅引入需要的字段。

示例十

方法

List<ExampleDO> findByIdInAndIsDeleted(@Param("id") Collection<Long> ids, @Param("isDeleted") int isDeleted);

SQL片段

<!--auto mapper generate-->
<select id="findByIdInAndIsDeleted" resultType="fun.fengwk.automapper.example.model.ExampleDO">
    select id, name, sort, f1, f2, is_deleted as isDeleted
    from example
    where id in
    <foreach close=")" collection="id" item="item" open="(" separator=",">
        #{item}
    </foreach>
    and is_deleted=#{isDeleted}
</select>

说明

注意在当前版本中@Param的value值要与对象字段和接口定义表达式中的值(这里是id)保持一致。

原理

AutoMapper基于JSR 269 Annotation Processing API实现,Annotation Processing API是Javac程序的一个SPI扩展点,通过编译期读取原文件信息自动生成相应代码片段,类似原理实现的框架有Lombok、Google auto......

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

AutoMapper是一款适用于Mybatis的SQL生成框架,提供了JPA风格的SQL语句生成能力,用户仅需依赖一个编译期jar包就能在编译期根据Mapper接口中的方法定义生成相应的XML文件和SQL语句。就像流行的Lombok一样,一切发生在编译期间,因此不会对软件性能造成任何影响,并且用户可以直接在编译完成的target目录或jar中看到生成的SQL语句。 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/zh__rain/auto-mapper.git
git@gitee.com:zh__rain/auto-mapper.git
zh__rain
auto-mapper
auto-mapper
main

搜索帮助