mapperdb 介绍
mapperdb
作用于 mybatis 的 mapper 的单表增删改查(mybatis v3.2.4+,无xml配置配置哦)。
1、entity 配置
package com.demo.domain; import java.util.Date; import javax.annotation.Resource; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.SelectKey; import org.apache.ibatis.type.JdbcType; @Resource public class Blog { private Integer id; private String title; private Date publishtime; private String content; public void setId(Integer id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setPublishtime(Date publishtime) { this.publishtime = publishtime; } public void setContent(String content) { this.content = content; } // call identity() // SELECT LAST_INSERT_ID() @Result(id = true) @SelectKey(statement = "SELECT LAST_INSERT_ID()", before = false, keyProperty = "id", keyColumn = "id", resultType = Integer.class) public Integer getId() { return id; } public String getTitle() { return title; } @Result(jdbcType=JdbcType.TIMESTAMP) public Date getPublishTime() { return publishtime; } public String getContent() { return content; } }
Result可以配置 TypeHandler
2、mybatis 配置
public void t() { Object b = null; // Mapper 添加 MapperRegistryProxy proxy = MapperRegistryProxy.use(sqlSessionFactory); proxy.addMapper(BlogMapper.class); sqlSession sqlSession = sqlSessionFactory.openSession(true); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); }
MapperRegistryProxy 注册 mapper 模板.
3、spring 整合
<bean class="com.mapperdb.spring.MapperScannerConfigurerSupprot"> <property name="basePackage" value="com.demo.mapper" /> <property name="sqlSessionfactorybeanName" value="sqlSessionFactory" /> </bean>
其中 MapperScannerConfigurerSupprot 是 mapper 模板的扫描器 .
4、mapper 使用方式
package com.demo.mapper; import com.demo.domain.Blog; import com.mapperdb.mapper.Mapper; public interface BlogMapper extends Mapper<Blog> { }
其中 BlogMapper 继承已经定义好的 mapper 模板 . 还可以实现自己的 mapper 模板.
5、注意事项
text 建议和 mapper 模板和 mybatis 的 mapper 不放在同一个包中,方便扫描。
mapperdb 官网
https://git.oschina.net/Bitter/mapperdb.git
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。