pojo类
public class Message {
int mid;
String content;
Date createTime; 时间的属性
String img;
String userName;
public static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 因为有时间的属性
public class User {
int uid;
String uname;
String upass;
String ugender;
dao类
public interface IMessageDao {
添加
void sendMessage(Message message);
查询所有
List<Message> findAllMessages();
List<Message> findMessagesByName(String name);
删除
void deleteMessage(String id);
//模糊查询
List<Message> findMessageByLike(String content);
public interface IUserDao {
//查询所有用户
List<User> getUsers();
service类
@Service
@Transactional(readOnly = true)
public class MessageServiceImpl implements IMessageService {
@Autowired
private IMessageDao dao;
@Override
@Transactional(readOnly = false)
public void sendMessage(Message message) {
dao.sendMessage(message);
}
@Override
public List<Message> findAllMessages() {
return dao.findAllMessages();
}
@Override
public List<Message> findMessagesByName(String name) {
return dao.findMessagesByName(name);
}
@Override
@Transactional(readOnly = false)
public void deleteMessage(String id) {
dao.deleteMessage(id);
}
@Override
public List<Message> findMessageByLike(String content) {
return dao.findMessageByLike(content);
}
@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao dao;
@Override
public List<User> getUsers() {
return dao.getUsers();
}
controller类
@Controller
public class MessageAction {
@Autowired
private IUserService userService;
@Autowired
private IMessageService messageService;
@RequestMapping(value = "toSend",method = RequestMethod.GET)
public String toSend(Model model){
List<User> users=userService.getUsers();
model.addAttribute("users",users);
return "send";
}
@RequestMapping(value = "sendMessage",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> sendMessage(String names,String content) throws Exception{
Map<String,Object> map=new HashMap<String,Object>();
names= URLDecoder.decode(names,"UTF-8");
content=URLDecoder.decode(content,"UTF-8");
Message message=new Message();
for (String name :names.split(",")) {
message.setUserName(name);
message.setCreateTime(new Date());
message.setContent(content);
messageService.sendMessage(message);
}
String msg="发送成功";
map.put("msg",msg);
return map;
}
@RequestMapping(value = "toMessage",method = RequestMethod.GET)
public String toMessage(Model model){
List<User> users=userService.getUsers();
List<Message> messages=messageService.findAllMessages();
model.addAttribute("users",users);
model.addAttribute("messages",messages);
return "message";
}
@RequestMapping(value = "findMessagesByName",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> findMessagesByName(String name) throws Exception{
name=URLDecoder.decode(name,"utf-8");
Map<String,Object> map=new HashMap<String,Object>();
List<Message> messages;
if (name.equals("0")){
messages=messageService.findAllMessages();
}
else {
messages=messageService.findMessagesByName(name);
}
map.put("messages",messages);
return map;
}
@RequestMapping(value = "deleteMessage",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> deleteMessage(String ids) throws Exception{
ids=URLDecoder.decode(ids,"utf-8");
for (String id:ids.split(",")) {
messageService.deleteMessage(id);
}
Map<String,Object> map=new HashMap<String,Object>();
List<Message> messages=messageService.findAllMessages();
map.put("messages",messages);
return map;
}
@RequestMapping(value = "/findMessageByLike",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> findMessageByLike(String content) throws Exception{
Map<String,Object> map=new HashMap<String,Object>();
content=URLDecoder.decode(content,"utf-8");
if (!content.equals("")){
List<Message> messages=messageService.findMessageByLike(content);
map.put("messages",messages);
}
else {
List<Message> messages=messageService.findAllMessages();
map.put("messages",messages);
}
return map;
}
}
sql语句 mapper
<mapper namespace="com.bw.dao.IMessageDao">
<select id="findAllMessages" resultType="Message">
SELECT mid,content,createtime AS createTime,u_name AS userName FROM tb_message
</select>
<select id="findMessagesByName" resultType="Message" parameterType="String">
SELECT mid,content,createtime AS createTime,u_name AS userName FROM tb_message WHERE u_name=#{name}
</select>
<insert id="sendMessage" parameterType="Message" useGeneratedKeys="true">
INSERT INTO tb_message (content,createtime,u_name) VALUES (#{content},#{createTime},#{userName})
</insert>
<delete id="deleteMessage" parameterType="java.util.List">
DELETE FROM tb_message WHERE mid = #{id}
</delete>
<select id="findMessageByLike" parameterType="String" resultType="Message">
SELECT mid,content,createtime AS createTime,u_name AS userName from tb_message where content
like CONCAT(CONCAT('%',#{content}),'%')
</select>
<mapper namespace="com.bw.dao.IUserDao">
<select id="getUsers" resultType="User">
SELECT * FROM tb_user
</select>
jsp界面
第一个界面 index.jsp
<a href="toSend.action">发送页面</a>
<a href="toMessage.action">查看信息页面</a>
第二个界面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html>
<head>
<title>发送信息</title>
<script src="/js/jquery-1.8.3.js"></script>
<script>
function sendMessage() {
var msg=$("#msg").val();
var checkName=[];
$("input:checked").each(function () {
checkName.push($(this).val());
});
checkName=checkName.toString();
var names=encodeURI(checkName);
var content=encodeURI(msg);
$.ajax({
type : "GET",
url: "sendMessage.action",
data: {names:names,content:content},
dataType: "json",
success: function(data) {
$("input:checked").prop("checked",false);
$("#msg").val("");
alert(data.msg);
},
error: function () {
alert("系统繁忙,请稍后重试");
}
});
}
</script>
</head>
<body>
<h2>站内信息发送页面</h2>
<form>
<table>
<tr><td>请选择联系人</td><td>
<c:forEach items="${users}" var="one">
<input type="checkbox" value="${one.uname}" name="uids">${one.uname}
</c:forEach>
</td></tr>
<tr><td>请输入消息内容</td><td><textarea id="msg"></textarea></td></tr>
<tr><td></td><td><input type="button" value="发送" οnclick="sendMessage()"></td></tr>
</table>
</form>
</body>
</html>
第三个界面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html>
<head>
<title>查看信息</title>
<script src="/js/jquery.min.js"></script>
<script>
function findMessagesByName() {
var name=$("#s1").val();
name=encodeURI(name);
$.ajax({
type:"GET",
url: "findMessagesByName.action",
data: {name:name},
dataType: "json",
success: function (data) {
var html="<tr>" +
"<td width=\"200px\">消息id</td>" +
"<td width=\"200px\">消息内容</td>" +
"<td width=\"200px\">时间</td>" +
"<td width=\"200px\">接收人</td>" +
"<td width=\"200px\">操作</td>" +
"</tr>";
var list=data.messages;
for(var i=0;i<list.length;i++){
html+="<tr>" +
"<td><input type='checkbox' id='id' value='"+list[i].mid+"'></td>" +
"<td>"+list[i].mid+"</td>" +
"<td>"+list[i].content+"</td>" +
"<td>"+list[i].createTime+"</td>" +
"<td>"+list[i].userName+"</td>" +
"<td><input type=\"button\" value=\"删除\" οnclick='deleteMessage("+list[i].mid+")'></td>" +
"</tr>";
}
$("#t1").html(html);
},
error: function () {
alert("系统繁忙,请稍后再试");
}
});
}
function deleteMessage(ids) {
id.toString();
ids=encodeURI(ids);
$.ajax({
type: "GET",
url: "deleteMessage.action",
data: {ids:ids},
dataType: "json",
success: function (data) {
var html="<tr>" +
"<td width=\"200px\">消息id</td>" +
"<td width=\"200px\">消息内容</td>" +
"<td width=\"200px\">时间</td>" +
"<td width=\"200px\">接收人</td>" +
"<td width=\"200px\">操作</td>" +
"</tr>";
var list=data.messages;
for(var i=0;i<list.length;i++){
html+="<tr>" +
"<td><input type='checkbox' id='id' value='"+list[i].mid+"'></td>" +
"<td>"+list[i].mid+"</td>" +
"<td>"+list[i].content+"</td>" +
"<td>"+list[i].createTime+"</td>" +
"<td>"+list[i].userName+"</td>" +
"<td><input type=\"button\" value=\"删除\" οnclick='deleteMessage("+list[i].mid+")'></td>" +
"</tr>";
}
$("#t1").html(html);
},
error: function () {
alert("系统繁忙,请稍后重试");
}
});
}
function deleteSomeMessages() {
var ids=[];
$("input:checked").each(function () {
ids.push($(this).val());
});
ids.toString();
ids=encodeURI(ids);
$.ajax({
type: "get",
url: "deleteMessage.action",
data: {ids:ids},
dataType: "json",
success: function (data) {
var html="<tr>" +
"<td width=\"200px\">消息id</td>" +
"<td width=\"200px\">消息内容</td>" +
"<td width=\"200px\">时间</td>" +
"<td width=\"200px\">接收人</td>" +
"<td width=\"200px\">操作</td>" +
"</tr>";
var list=data.messages;
for(var i=0;i<list.length;i++){
html+="<tr>" +
"<td><input type='checkbox' id='id' value='"+list[i].mid+"'></td>" +
"<td>"+list[i].mid+"</td>" +
"<td>"+list[i].content+"</td>" +
"<td>"+list[i].createTime+"</td>" +
"<td>"+list[i].userName+"</td>" +
"<td><input type=\"button\" value=\"删除\" οnclick='deleteMessage("+list[i].mid+")'></td>" +
"</tr>";
}
$("#t1").html(html);
},
error: function () {
alert("系统繁忙,请稍后再试");
}
});
}
function searchContent() {
var msg=$("#content").val();
var content=encodeURI(msg);
$.ajax({
type: "get",
url: "findMessageByLike.action",
data: {content:content},
dataType:"json",
success: function (data) {
var html="";
var list=data.messages;
for(var i=0;i<list.length;i++){
html+="<tr>" +
"<td><input type='checkbox' id='id' value='"+list[i].mid+"'></td>" +
"<td>"+list[i].mid+"</td>" +
"<td>"+list[i].content+"</td>" +
"<td>"+list[i].createTime+"</td>" +
"<td>"+list[i].userName+"</td>" +
"<td><input type=\"button\" value=\"删除\" οnclick='deleteMessage("+list[i].mid+")'></td>" +
"</tr>";
}
$("#t1").html(html);
},
error: function () {
alert("系统繁忙,请稍后重试");
}
});
}
</script>
</head>
<body>
<h2>站内信息查看页面</h2>
<form>
<select id="s1" οnchange="findMessagesByName()">
<option value="0" selected="selected">所有人</option>
<c:forEach items="${users}" var="one">
<option value="${one.uname}">${one.uname}</option>
</c:forEach>
</select>
<input id="content">
<input type="button" value="查询" οnclick="searchContent()">
<input type="button" value="批量删除" οnclick="deleteSomeMessages()">
<table border="1px" id="t1">
<tr>
<td></td>
<td width="200px">消息id</td>
<td width="200px">消息内容</td>
<td width="200px">时间</td>
<td width="200px">接收人</td>
<td width="200px">操作</td>
</tr>
<c:forEach items="${messages}" var="one">
<tr>
<td><input type="checkbox" id="id" value="${one.mid}"></td>
<td>${one.mid}</td>
<td>${one.content}</td>
<td>${one.createTime}</td>
<td>${one.userName}</td>
<td><input type="button" value="删除" οnclick="deleteMessage(${one.mid})"></td>
</tr>
</c:forEach>
</table>
<a href="toSend.action">返回发送页面</a>
</form>
</body>
</html>
$.ajax({
type:"get",
url:"/book/sendBookInfo.action",//需要用来处理ajax请求的action
data:{//设置数据源
content: content,
bookNames:bookNames
},
dataType:"json",//设置需要返回的数据类型
success:function(data){
var count = data.count;
if(count == 1)
{
alert("发送成功");
$(":checkbox").removeAttr("checked");
$("#content").val("");
}
else
{
alert("发送失败");
}
},
error:function(){
alert("系统异常,请稍后重试!");
}
});