# mybatis-jpa **Repository Path**: Ding_Jian_w/mybatis-jpa ## Basic Information - **Project Name**: mybatis-jpa - **Description**: Mybatis插件,提供Mybatis处理JPA的能力。Q群:246912326 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 24 - **Created**: 2019-10-18 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mybatis-jpa [![Mybatis](https://img.shields.io/badge/mybatis-3.4.x-brightgreen.svg)](https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis) [![JDK 1.7](https://img.shields.io/badge/JDK-1.7-green.svg)]() [![maven central](https://img.shields.io/badge/version-2.1.3-brightgreen.svg)](http://search.maven.org/#artifactdetails%7Ccom.github.cnsvili%7Cmybatis-jpa%7C2.1.3%7C) [![APACHE 2 License](https://img.shields.io/badge/license-Apache2-blue.svg?style=flat)](LICENSE) [:book: English Documentation](README-EN.md) | :book: 中文文档 Mybatis插件,提供Mybatis处理JPA的能力。 ## maven ```xml com.littlenb mybatis-jpa 2.1.3 ``` ## 插件清单 + ResultTypePlugin [![plugin](https://img.shields.io/badge/plugin-resolved-green.svg)]() + DefinitionStatementScanner [![plugin](https://img.shields.io/badge/plugin-resolved-green.svg)]() ### ResultTypePlugin 对于常规的结果映射,不需要再构建ResultMap,ResultTypePlugin增加了Mybatis对结果映射(JavaBean/POJO)中JPA注解的处理。 映射规则: + 名称匹配默认与mybatis全局配置一致 可以在mybatis全局配置文件中,开启驼峰(Java Field)与下划线(SQL Column)的匹配规则. ```xml ``` + 使用@Column注解中name属性指定SQL Column + 使用@Transient注解标记非持久化字段(不需要结果集映射的字段) 类型处理: + Boolean-->BooleanTypeHandler + Enum默认为EnumTypeHandler 使用@Enumerated(EnumType.ORDINAL) 指定为 EnumOrdinalTypeHandler + Enum实现ICodeEnum接口实现自定义枚举值 使用@CodeEnum(CodeType.INT) 指定为 IntCodeEnumTypeHandler 或@CodeEnum(CodeType.STRING) 指定为 StringCodeEnumTypeHandler @CodeEnum 优先级 高于 @Enumerated 结果集嵌套: + 支持OneToOne + 支持OneToMany e.g. mybatis.xml ```xml ``` JavaBean ```JAVA @Entity public class UserArchive {// @Id private Long id;// /** 默认驼峰与下划线转换 */ private String userName;// /** 枚举类型 */ @Enumerated(EnumType.ORDINAL) private SexEnum sex;// /** 枚举类型,自定义值 */ @CodeEnum(CodeType.INT) private PoliticalEnum political;// /** 属性名与列名不一致 */ @Column(name = "gmt_create") private Date createTime;// }// ``` mapper.xml ```xml ``` ### DefinitionStatementScanner 注册MappedStatement,基于注解,仅支持Insert和Update。 #### InsertDefinition: + selective: 默认值false(处理null属性) #### updateDefinition: + selective: 默认值false(处理null属性) + where: SQL condition e.g. Spring 容器初始化完成后执行 ```java @Service public class DefinitionStatementInit { @Autowired private SqlSessionFactory sqlSessionFactory; @PostConstruct public void init() { Configuration configuration = sqlSessionFactory.getConfiguration(); StatementBuildable statementFactory = new DefinitionStatementBuilder(configuration); DefinitionStatementScanner.Builder builder = new DefinitionStatementScanner.Builder(); DefinitionStatementScanner definitionStatementScanner = builder.configuration(configuration).basePackages(new String[]{"com.mybatis.jpa.mapper"}) .statementBuilder(statementFactory).build(); definitionStatementScanner.scan(); } } ``` Mapper ```Java @Mapper @Repository public interface UserUpdateMapper { @InsertDefinition(selective = true) int insert(User user); @UpdateDefinition(selective = true, where = " id = #{id}") int updateById(User user); } ``` Best Advice ```java /** * Definition a Generic Interface as BaseMapper */ public interface IBaseMapper { @InsertDefinition int insert(T t); @InsertDefinition(selective = true) int insertSelective(T t); @UpdateDefinition int updateById(T t); @UpdateDefinition(selective = true) int updateSelectiveById(T t); } /** * extends BaseMapper */ @Mapper @Repository public interface InheritUserMapper extends IBaseMapper { } ``` 更多示例请查看test目录代码。 ## 联系方式 QQ交流群:246912326