1 Star 0 Fork 23

docker / mybatis-jpa

forked from littlenb / mybatis-jpa 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 5.50 KB
一键复制 编辑 原始数据 按行查看 历史
swayli 提交于 2019-07-06 21:06 . v2.2.0

mybatis-jpa

Mybatis JDK 1.7 maven central APACHE 2 License

:book: English Documentation | :book: 中文文档

Mybatis插件,提供Mybatis处理JPA的能力。

maven

        <dependency>
            <groupId>com.littlenb</groupId>
            <artifactId>mybatis-jpa</artifactId>
            <version>2.2.0</version>
        </dependency>

插件清单

  • ResultTypePlugin plugin

  • AnnotationStatementScanner plugin

ResultTypePlugin

对于常规的结果映射,不需要再构建ResultMap,ResultTypePlugin增加了Mybatis对结果映射(JavaBean/POJO)中JPA注解的处理。

映射规则:

  • 名称匹配默认与mybatis全局配置一致

    可以在mybatis全局配置文件中,开启驼峰(Java Field)与下划线(SQL Column)的匹配规则.

<settings>
    <!-- default : false -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
  • 使用@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

<configuration>
    <plugins>
		<plugin interceptor="com.littlenb.mybatisjpa.rs.ResultTypePlugin">
		</plugin>
	</plugins>
</configuration>

JavaBean

@Entity
public class UserArchive {// <resultMap id="xxx" type="userArchive">

    @Id
    private Long id;// <id property="id" column="id" />
                           
    /** 默认驼峰与下划线转换 */
    private String userName;// <result property="username" column="user_name"/>

    /** 枚举类型 */
    @Enumerated(EnumType.ORDINAL)
    private SexEnum sex;// <result property="sex" column="sex" typeHandler=EnumOrdinalTypeHandler/>
    
    /** 枚举类型,自定义值 */
    @CodeEnum(CodeType.INT)
    private PoliticalEnum political;// <result property="political" column="political" typeHandler=IntCodeEnumTypeHandler/>
    
    /** 属性名与列名不一致 */
    @Column(name = "gmt_create")
    private Date createTime;// <result property="createTime" column="gmt_create"/>
}// </resultMap>

mapper.xml

<!-- in xml,declare the resultType -->
<select id="selectById" resultType="userArchive">
	SELECT * FROM t_sys_user_archive WHERE id = #{id}
</select>

AnnotationStatementScanner

注册MappedStatement,基于注解,仅支持Insert和Update。

InsertDefinition:

  • selective: 默认值false(处理null属性)

updateDefinition:

  • selective: 默认值false(处理null属性)

  • where: SQL condition

e.g.

Spring 容器初始化完成后执行

@Component
public class AnnotationStatementInit {

  @Autowired
  private SqlSessionFactory sqlSessionFactory;

  @PostConstruct
  public void init() {
    Configuration configuration = sqlSessionFactory.getConfiguration();
    KeyGenerator keyGenerator = new IdentityKeyGenerator(new MyIdGenerator());
    configuration.addKeyGenerator(Constant.DEFAULT_KEY_GENERATOR, keyGenerator);
    AnnotationStatementScanner annotationStatementScanner = new AnnotationStatementScanner.Builder()
        .configuration(configuration)
        .basePackages(new String[]{"com.littlenb.mybatisjpa.demo.mapper"})
        .annotationStatementRegistry(AnnotationStatementRegistry.getDefaultRegistry()).build();
    annotationStatementScanner.scan();
  }
}

Mapper

@Mapper
@Repository
public interface UserUpdateMapper {

    @InsertDefinition(strategy = SelectorStrategy.IGNORE_NULL)
    int insert(User user);

    @UpdateDefinition(strategy = SelectorStrategy.IGNORE_NULL, where = " id = #{id}")
    int updateById(User user);
}

Best Advice

/**
* Definition a Generic Interface as BaseMapper 
*/
public interface IBaseMapper<T> {

    @InsertDefinition
    int insert(T t);
  
    @InsertDefinition(strategy = SelectorStrategy.IGNORE_NULL)
    int insertIgnoreNull(T t);
  
    @InsertDefinition(strategy = SelectorStrategy.CERTAIN)
    int insertCertain(Certainty<T> certainty);
  
    @UpdateDefinition
    int updateById(T t);
  
    @UpdateDefinition(strategy = SelectorStrategy.IGNORE_NULL)
    int updateByIdIgnoreNull(T t);
  
    @UpdateDefinition(strategy = SelectorStrategy.CERTAIN, where = " id = #{entity.id}")
    int updateByIdCertain(Certainty<T> certainty);

}

/**
* extends BaseMapper
*/
@Mapper
@Repository
public interface UserMapper extends IBaseMapper<User> {

}

更多示例请查看test

联系方式

QQ交流群:246912326

Java
1
https://gitee.com/sakakokiya1/mybatis-jpa.git
git@gitee.com:sakakokiya1/mybatis-jpa.git
sakakokiya1
mybatis-jpa
mybatis-jpa
master

搜索帮助