MyBatis的注解

基于注解的mybatis和spring整合:[url]http://huangmin001.iteye.com/blog/1185806[/url]
[color=red]这个文章说的很详细,很值得一看[/color].


Mapper中注解(Annotation)的使用示例:[url]http://puras.cn/mybatis-annotation-example[/url]

Mapper:

public interface RoleMapper {
final String SELECT_ALL = "SELECT * FROM nap_roles";
final String SELECT_BY_ID = "SELECT * FROM nap_roles WHERE id=#{id}";
final String INSERT = "INSERT INTO nap_roles(name, description, ctime, mtime) VALUES(#{name}, #{description}, #{creationTime}, #{modifiedTime})";
final String UPDATE = "UPDATE nap_roles SET name=#{name}, description=#{description}, mtime=#{modifiedTime} WHERE id=#{id}";
final String DELETE = "DELETE FROM nap_roles WHERE id=#{id}";
final String SELECT_BY_PAGE_BEGIN = "SELECT * FROM nap_roles";
final String SELECT_BY_PAGE_END = " limit #{startPosition}, #{maxResult}";
final String COUNT = "SELECT COUNT(id) FROM nap_roles WHERE 1=1 ";

@Insert(INSERT)
@Options(useGeneratedKeys = true, keyProperty = "id")
void create(Role role);

@Update(UPDATE)
void update(Role role);

@Delete(DELETE)
void delete(Long id);

@Select(SELECT_ALL)
@Results(value = {
@Result(property="id"),
@Result(property="name"),
@Result(property="description"),
@Result(property="creationTime", column="ctime"),
@Result(property="modifiedTime", column="mtime")
})
List findAll();

@SelectProvider(type = RoleProvider.class, method = "findByPage")
@Results(value = {
@Result(property="id"),
@Result(property="name"),
@Result(property="description"),
@Result(property="creationTime", column="ctime"),
@Result(property="modifiedTime", column="mtime")
})
List findByPage(Map params);

@Select(SELECT_BY_ID)
@Results(value = {
@Result(property="id"),
@Result(property="name"),
@Result(property="description"),
@Result(property="creationTime", column="ctime"),
@Result(property="modifiedTime", column="mtime")
})
Role findById(Long id);

@SelectProvider(type = RoleProvider.class, method = "getCount")
int getCount(Role role);

@Select(COUNT)
int count();
}



在使用比较复杂的SQL时,会用到各种Provider来处理。

虽然都改用Annotation了,但还有些地方不是特别的爽。

1、ResultMap,不知道怎么来定义一个对象,所有的都引用一个;

2、所有的SQL都写到JAVA里了,修改起来,也是件麻烦事。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值