同步操作将从 littlenb/mybatis-jpa 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
English Documentation | 中文文档
The plugins for mybatis, in order to provider the ability to handler jpa.
<dependency>
<groupId>com.littlenb</groupId>
<artifactId>mybatis-jpa</artifactId>
<version>2.1.2</version>
</dependency>
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>
register MappedStatement with annotation-based,only support for Insert and Update.
selective: default value is false(handler null of java field)
where: SQL condition
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。