java dao层编写及注释_四. DAO层和model

5710876bb466

结构

5. DAO

5710876bb466

DAO

5.1 CommentDAO

@Mapper

public interface CommentDAO {

String TABLE_NAME = " comment ";

String INSERT_FIELDS = " user_id, content, created_date, entity_id, entity_type, status ";

String SELECT_FIELDS = " id, " + INSERT_FIELDS;

@Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,

") values (#{userId},#{content},#{createdDate},#{entityId},#{entityType},#{status})"})

int addComment(Comment comment);

@Update({"update ", TABLE_NAME, " set status=#{status} where entity_id=#{entityId} and entity_type=#{entityType}"})

void updateStatus(@Param("entityId") int entityId, @Param("entityType") int entityType, @Param("status") int status);

@Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME,

" where entity_id=#{entityId} and entity_type=#{entityType} order by id desc"})

List selectByEntity(@Param("entityId") int entityId, @Param("entityType") int entityType);

@Select({"select count(id) from ", TABLE_NAME, " where entity_id=#{entityId} and entity_type=#{entityType} "})

int getCommentCount(@Param("entityId") int entityId, @Param("entityType") int entityType);

}

1)DAO在JavaWeb开发中的定位

一个典型的DAO实现有下列几个组件:

1.一个DAO接口(CRUD)

2.一个实现DAO接口的具体类

3.数据传递对象(DTO)(POJO):有些时候叫做值对象(VO)或领域模型(domain)

2)@Mapper注解

作用:持久化

一种持久化方式:

在一个包中写一个DAO的接口,在另一个包里面写DAO的实现,使用sqlMapClient来从***-sql.xml中读取相应的sql。

spring+Mybatis的项目,一种新的持久化方式:

只写一个dao的接口,在接口的方法中直接注解上用到的sql语句,接口上方多了一个@Mapper注解。

3)@Param

mybatis中使用@param和不使用区别

1,使用@Param注解

当以下面的方式进行写SQL语句时:

@Select("select column from table where userid = #{userid} ")

public int selectColumn(int userid);

当你使用了使用@Param注解来声明参数时,如果使用 #{} 或 ${} 的方式都可以。

@Select("select column from table where userid = ${userid} ")

public int selectColumn(@Param("userid") int userid);

当你不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错。

@Select("select column from table where userid = ${userid} ")

public int selectColumn(@Param("userid") int userid);

2,不使用@Param注解

不使用@Param注解时,参数只能有一个,并且是Javabean。在SQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。

// 这里id是user的属性

@Select("SELECT * from Table where id = ${id}")

Enchashment selectUserById(User user);

5.2 LoginTicketDAO

@Mapper

public interface LoginTicketDAO {

String TABLE_NAME = "login_ticket";

String INSERT_FIELDS = " user_id, expired, status, ticket ";

String SELECT_FIELDS = " id, " + INSERT_FIELDS;

@Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,

") values (#{userId},#{expired},#{status},#{ticket})"})

int addTicket(LoginTicket ticket);

@Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where ticket=#{ticket}"})

LoginTicket selectByTicket(String ticket);

@Update({"update ", TABLE_NAME, " set status=#{status} where ticket=#{ticket}"})

void updateStatus(@Param("t

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值