1 Star 0 Fork 23

hbyufan / mybatis-jpa

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

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.1.2</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.mybatis.jpa.plugin.ResultTypePlugin">
		</plugin>
	</plugins>
</configuration>

JavaBean

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

    @Id
    private Long userId;// <id property="id" column="user_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 user_id = #{userId}
</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

@Service
public class DefinitionStatementInit {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @PostConstruct
    public void init() {
        Configuration configuration = sqlSessionFactory.getConfiguration();
        StatementBuildable statementBuildable = new DefinitionStatementBuilder(configuration);
        DefinitionStatementScanner.Builder builder = new DefinitionStatementScanner.Builder();
        DefinitionStatementScanner definitionStatementScanner = builder.configuration(configuration).basePackages(new String[]{"com.mybatis.jpa.mapper"})
                .statementBuilder(statementBuildable).build();
        definitionStatementScanner.scan();
    }
}

Mapper

@Mapper
@Repository
public interface UserUpdateMapper {

    @InsertDefinition(selective = true)
    int insert(User user);

    @UpdateDefinition(selective = true, where = " user_id = #{userId}")
    int updateById(User user);
}

Please view test package where has more examples.

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

搜索帮助