• 站內信的實現
• 站內信
“站內信”主要實現站內用戶的溝通,有兩個基本功能。
一:點到點的消息傳送。用戶給用戶發送站內信,管理員給用戶發送站內信。
二:點到面的消息傳送。管理員給用戶(指定滿足某一條件的用戶群)群發消息
站內信的開發流程
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