1 Star 0 Fork 23

hot / mybatis-jpa

forked from littlenb / mybatis-jpa 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README-EN.md 5.52 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: 中文文档

The plugins for mybatis, in order to provider the ability to handler jpa.

maven

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

Plugin boom

  • ResultTypePlugin plugin

  • UpdatePlugin plugin

ResultTypePlugin

Introduce the JPA annotation to handle result set mappings(JavaBean/POJO).

It means with ResultTypePlugin,no longer need to be build ResultMap.

Mapping rules:

  • default name mapping rule is the same as mybatis global config.

    you can setting mapping rule in mybatis-config.xml with camel(Java Field) to underline(SQL Column)

<settings>
    <!-- default : false -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
  • to specify SQL Column,declare the property "name" in @Column annotation

  • declare the no mapping field with @Transient annotation

TypeHandler:

  • Boolean-->BooleanTypeHandler

  • Enum is default with EnumTypeHandler

    @Enumerated(EnumType.ORDINAL) --> EnumOrdinalTypeHandler

  • implement ICodeEnum to achieve custom Enum value

    @CodeEnum(CodeType.INT) --> IntCodeEnumTypeHandler

    @CodeEnum(CodeType.STRING) --> StringCodeEnumTypeHandler

    @CodeEnum priority above than @Enumerated

nested result set:

  • @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 userId;// <id property="id" column="id" />
                           
    /** default mapping rule is camel(Java Field) to underline(SQL Column) */
    private String userName;// <result property="username" column="user_name"/>

    /** enum type */
    @Enumerated(EnumType.ORDINAL)
    private SexEnum sex;// <result property="sex" column="sex" typeHandler=EnumOrdinalTypeHandler/>
    
     /** enum type,custom value */
     @CodeEnum(CodeType.INT)
     private PoliticalEnum political;// <result property="political" column="political" typeHandler=IntCodeEnumTypeHandler/>

    /** java field differ from sql column in name */
    @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>

DefinitionStatementScanner

register MappedStatement with annotation-based,only support for Insert and Update.

InsertDefinition:

  • selective: default value is false(handler null of java field)

updateDefinition:

  • selective: default value is false(handler null of java field)

  • where: SQL condition

Mapping rules and TypeHandler

  • if the field is no need resolve in SQL,declare the property "insertable" "updateable" in @Column.

  • the same as above(ResultTypePlugin rules)

e.g.

after Spring init

@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> {

}

Please view test package where has more examples.

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

搜索帮助