# 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
[](https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis)
[]()
[](http://search.maven.org/#artifactdetails%7Ccom.github.cnsvili%7Cmybatis-jpa%7C2.1.3%7C)
[](LICENSE)
:book: English Documentation | [:book: 中文文档](README.md)
The plugins for mybatis, in order to provider the ability to handler jpa.
## maven
```xml
com.littlenbmybatis-jpa2.1.3
```
## Plugin boom
+ ResultTypePlugin []()
+ UpdatePlugin []()
### 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)
```xml
```
+ 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
```xml
```
JavaBean
```JAVA
@Entity
public class UserArchive {//
@Id
private Long userId;//
/** default mapping rule is camel(Java Field) to underline(SQL Column) */
private String userName;//
/** enum type */
@Enumerated(EnumType.ORDINAL)
private SexEnum sex;//
/** enum type,custom value */
@CodeEnum(CodeType.INT)
private PoliticalEnum political;//
/** java field differ from sql column in name */
@Column(name = "gmt_create")
private Date createTime;//
}//
```
mapper.xml
```xml
```
### 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
```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 {
}
```
Please view test package where has more examples.