• 站内信的实现
• 站内信
“站内信”主要实现站内用户的沟通,有两个基本功能。
一:点到点的消息传送。用户给用户发送站内信,管理员给用户发送站内信。
二:点到面的消息传送。管理员给用户(指定满足某一条件的用户群)群发消息
站内信的开发流程
1. Database Column
2. Model:模型定义,和数据库相匹配
3. DAO:数据读取
4. Service:服务包装
5. Controller:业务入口
6. Test
1. Database Column
private int
id
;
private int
from_Id
;
private int
to_Id
;
private
String
content
;
private
Date
createdDate
;
private int
hasRead
;
2. Model:模型定义,和数据库相匹配
package
com.LG.model
;
import
java.util.Date
;
/**
*
@Author
liguo
*
@Description
*
@Data
2018-09-06 14:21
*/
public class
Message
{
private int
id
;
private int
fromId
;
private int
toId
;
private
String
content
;
private
Date
createdDate
;
private int
hasRead
;
public int
getId
()
{
return
id
;
}
public void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public int
getFromId
()
{
return
fromId
;
}
public void
setFromId
(
int
fromId
)
{
this
.
fromId
=
fromId
;
}
public int
getToId
()
{
return
toId
;
}
public void
setToId
(
int
toId
)
{
this
.
toId
=
toId
;
}
public
String
getContent
()
{
return
content
;
}
public void
setContent
(
String
content
)
{
this
.
content
=
content
;
}
public
Date
getCreatedDate
()
{
return
createdDate
;
}
public void
setCreatedDate
(
Date
createdDate
)
{
this
.
createdDate
=
createdDate
;
}
public int
getHasRead
()
{
return
hasRead
;
}
public void
setHasRead
(
int
hasRead
)
{
this
.
hasRead
=
hasRead
;
}
public
String
getConversationId
()
{
if
(
fromId
<
toId
)
{
return
String
.
format
(
"%d_%d"
,
fromId
,
toId
)
;
}
else
{
return
String
.
format
(
"%d_%d"
,
toId
,
fromId
)
;
}
}
public void
setConversationId
(
String
conversationId
)
{
String
conversationId1
=
conversationId
;
}
}
3. DAO:数据读取
package
com.LG.dao
;
import
com.LG.model.Message
;
import
org.apache.ibatis.annotations.
Insert
;
import
org.apache.ibatis.annotations.
Mapper
;
import
org.apache.ibatis.annotations.
Param
;
import
org.apache.ibatis.annotations.
Select
;
import
java.util.
List
;
/**
*
@Author
liguo
*
@Description
*
@Data
2018-09-06 14:38
*/
@Mapper
public interface
MessageDAO
{
String
TABLE_NAME
=
" message "
;
String
INSERT_FIELDS
=
" from_id, to_id, content, has_read, conversation_id, created_date "
;
String
SELECT_FIELDS
=
" id, "
+
INSERT_FIELDS
;
@Insert
(
{
"insert into "
,
TABLE_NAME
,
"("
,
INSERT_FIELDS
,
") values (#{fromId},#{toId},#{content},#{hasRead},#{conversationId},#{createdDate})"
}
)
int
addMessage
(
Message
message
)
;
@Select
(
{
"select "
,
SELECT_FIELDS
,
" from "
,
TABLE_NAME
,
" where conversation_id=#{conversationId} order by created_date desc limit #{offset}, #{limit}"
}
)
List
<
Message
>
getConversationDetail
(
@Param
(
"conversationId"
)
String
conversationId
,
@Param
(
"offset"
)
int
offset
,
@Param
(
"limit"
)
int
limit
)
;
@Select
(
{
"select "
,
INSERT_FIELDS
,
" , count(id) as id from ( select * from "
,
TABLE_NAME
,
" where from_id=#{userId} or to_id=#{userId} order by created_date desc) tt group by conversation_id order by created_date desc limit #{offset}, #{limit}"
}
)
//实现分页功能;offset是分的页数目 ,limit为每页显示的内容;
List
<
Message
>
getConversationList
(
@Param
(
"userId"
)
int
userId
,
@Param
(
"offset"
)
int
offset
,
@Param
(
"limit"
)
int
limit
)
;
@Select
(
{
"select count(id) from "
,
TABLE_NAME
,
" where has_read=0 and to_id=#{userId} and conversation_id=#{conversationId}"
}
)
int
getConversationUnreadCount
(
@Param
(
"userId"
)
int
userId
,
@Param
(
"conversationId"
)
String
conversationId
)
;
}
4. Service:服务包装
5. Controller:业务入口
6. Test