实现业务逻辑代码
6、创建各层的java程序
代码的结构如图:
?
?
源代码为:
?
UserDO.java
?
?
package com.alibaba.webx3.messageboard.dao.object; import java.util.Date; public class UserDO { private String username; private String password; private int id; private Date gmtModify; private Date gmtCreate; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getGmtModify() { return gmtModify; } public void setGmtModify(Date gmtModify) { this.gmtModify = gmtModify; } public Date getGmtCreate() { return gmtCreate; } public void setGmtCreate(Date gmtCreate) { this.gmtCreate = gmtCreate; } }
?
?
MessageDO.java
?
?
package com.alibaba.webx3.messageboard.dao.object; import java.util.Date; public class MessageDO { private int id; private String title; private String author; private String content; private Date gmtModify; private Date gmtCreate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getGmtModify() { return gmtModify; } public void setGmtModify(Date gmtModify) { this.gmtModify = gmtModify; } public Date getGmtCreate() { return gmtCreate; } public void setGmtCreate(Date gmtCreate) { this.gmtCreate = gmtCreate; } }
?
UserDAO.java
?
?
package com.alibaba.webx3.messageboard.dao; import com.alibaba.webx3.messageboard.dao.object.UserDO; public interface UserDAO { public boolean addUser(UserDO userDO); public boolean deleteUser(UserDO userDO); public UserDO getByUsername(String username); public boolean updateUser(UserDO userDO); }??
MessageDAO.java
?
package com.alibaba.webx3.messageboard.dao; import java.util.List; import java.util.Map; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public interface MessageDAO { public boolean addMessage(MessageDO messageDO); public MessageDO getMessageById(int id); public List<MessageDO> getMessageList(Map<String, Integer> map); public boolean deleteMessageById(int id); public boolean updateMessage(MessageDO messageDO); }??
UserDAOImpl.java
?
package com.alibaba.webx3.messageboard.dao.impl; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.alibaba.webx3.messageboard.dao.UserDAO; import com.alibaba.webx3.messageboard.dao.object.UserDO; public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO { public boolean addUser(UserDO userDO) { Object result = getSqlMapClientTemplate().insert("insertUser", userDO); return ((Integer) result > 0); } public boolean deleteUser(UserDO userDO) { int result = getSqlMapClientTemplate().delete("deleteuser", userDO); return (result > 0); } public UserDO getByUsername(String username) { return (UserDO) getSqlMapClientTemplate().queryForObject("selectByUsername", username); } public boolean updateUser(UserDO userDO) { int result = getSqlMapClientTemplate().update("updateuser", userDO); return (result > 0); } }
?
?
MessageDAOImpl.java
?
?
package com.alibaba.webx3.messageboard.dao.impl; import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.alibaba.webx3.messageboard.dao.MessageDAO; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public class MessageDAOImpl extends SqlMapClientDaoSupport implements MessageDAO { public boolean addMessage(MessageDO messageDO) { Object result = getSqlMapClientTemplate().insert("insertMessage", messageDO); return ((Integer) result > 0); } public MessageDO getMessageById(int id) { return (MessageDO) getSqlMapClientTemplate().queryForObject("selectById", id); } @SuppressWarnings("unchecked") public List<MessageDO> getMessageList(Map<String, Integer> map) { return getSqlMapClientTemplate().queryForList("selectBylist", map); } public boolean deleteMessageById(int id) { int result = getSqlMapClientTemplate().delete("deleteMessage", id); return (result > 0); } public boolean updateMessage(MessageDO messageDO) { int result = getSqlMapClientTemplate().update("updateMessage", messageDO); return (result > 0); } }??
UserService.java
?
package com.alibaba.webx3.messageboard.biz.service; import com.alibaba.webx3.messageboard.dao.object.UserDO; public interface UserService { public boolean addUser(UserDO userDO); public boolean deleteUser(UserDO userDO); public UserDO getByUsername(String username); public boolean updateUser(UserDO userDO); }??
MessageService.java
?
package com.alibaba.webx3.messageboard.biz.service; import java.util.List; import java.util.Map; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public interface MessageService { public boolean addMessage(MessageDO messageDO); public MessageDO getMessageById(int id); public List<MessageDO> getMessageList(Map<String, Integer> map); public boolean deleteMessageById(int id); public boolean updateMessage(MessageDO messageDO); }??
UserServiceImpl.java
?
package com.alibaba.webx3.messageboard.biz.service.impl; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.webx3.messageboard.biz.service.UserService; import com.alibaba.webx3.messageboard.dao.UserDAO; import com.alibaba.webx3.messageboard.dao.object.UserDO; public class UserServiceImpl implements UserService { @Autowired private UserDAO userDAO; public boolean addUser(UserDO userDO) { return userDAO.addUser(userDO); } public boolean deleteUser(UserDO userDO) { return userDAO.deleteUser(userDO); } public UserDO getByUsername(String username) { return userDAO.getByUsername(username); } public boolean updateUser(UserDO userDO) { return userDAO.updateUser(userDO); } }??
MessageServiceImpl.java
?
package com.alibaba.webx3.messageboard.biz.service.impl; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.webx3.messageboard.biz.service.MessageService; import com.alibaba.webx3.messageboard.dao.MessageDAO; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public class MessageServiceImpl implements MessageService { @Autowired private MessageDAO messageDAO; public boolean addMessage(MessageDO messageDO) { return messageDAO.addMessage(messageDO); } public MessageDO getMessageById(int id) { return messageDAO.getMessageById(id); } public List<MessageDO> getMessageList(Map<String, Integer> map) { return messageDAO.getMessageList(map); } public boolean deleteMessageById(int id) { return messageDAO.deleteMessageById(id); } public boolean updateMessage(MessageDO messageDO) { return messageDAO.updateMessage(messageDO); } }
?
UserVO.java
?
package com.alibaba.webx3.messageboard.module.vo; public class UserVO { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }??
UserAction.java
?
?
package com.alibaba.webx3.messageboard.module.action; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.Navigator; import com.alibaba.citrus.turbine.dataresolver.FormGroup; import com.alibaba.webx3.messageboard.biz.service.UserService; import com.alibaba.webx3.messageboard.dao.object.UserDO; import com.alibaba.webx3.messageboard.module.vo.UserVO; public class UserAction { @Autowired private UserService userService; //登陆 public void doLogin(@FormGroup("login") UserVO user,Context context, Navigator nav){ String username=user.getUsername(); String password=user.getPassword(); UserDO userdo=null; boolean success; //根据用户名获得用户记录 userdo=userService.getByUsername(username); if(userdo==null){ context.put("message", "用户名不存在!"); nav.forwardTo("index"); return; } //校验密码是否正确 if(password.equals(userdo.getPassword())){ success=true; } else{ success=false; } //判断执行转向和重定向 if(success){ context.put("username", user.getUsername()); nav.forwardTo("message/messageList"); }else{ context.put("message", "密码错误!"); nav.forwardTo("index"); } } //退出 public void doLogout(Navigator nav) { //退出操作 //转到首页 nav.redirectTo("messageBoardLink").withTarget("index"); } }??
MessageAction.java
?
?
package com.alibaba.webx3.messageboard.module.action; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.citrus.turbine.Navigator; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.citrus.turbine.dataresolver.Params; import com.alibaba.webx3.messageboard.biz.service.MessageService; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public class MessageAction { @Autowired private MessageService messageService; //添加留言 public void doAddMessage(@Params MessageDO messageDO, Navigator nav) { // 没有取到页面的数据 if (messageDO == null) { nav.redirectTo("messageBoardLink").withTarget("message/addMessage").withParameter("message", "页面提交失败!"); } boolean result = false; // 插入数据库 result = messageService.addMessage(messageDO); // 校验结果及跳转 if (result) { nav.redirectTo("messageBoardLink").withTarget("message/messageList"); } else { nav.redirectTo("messageBoardLink").withTarget("message/addMessage").withParameter("message", "数据库操作失败!"); } } //删除留言 public void doDelete(@Param(name = "messageId") int messageId, Navigator nav) { // 没有取到页面的数据 if (messageId <= 0) { nav.redirectTo("messageBoardLink").withTarget("message/messageList").withParameter("message", "页面提交失败!"); } boolean result = false; // 删除数据 result = messageService.deleteMessageById(messageId); // 校验结果及跳转 if (result) { nav.redirectTo("messageBoardLink").withTarget("message/messageList"); } else { nav.redirectTo("messageBoardLink").withTarget("message/messageList").withParameter("message", "数据库操作失败!"); } } //修改留言 public void doModifyMessage(@Params MessageDO messageDO, Navigator nav) { //生成修改时间 SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); //获取修改者 String modifyauthor = messageDO.getAuthor(); //去掉上次修改的作者信息 String content = messageDO.getContent(); if(!StringUtil.isBlank(content)){ int index = content.indexOf("-"); if(index>=0){ content = content.substring(0, index); } //生成修改信息 content=content+"------(此留言被用户 "+modifyauthor+" 修改过,时间: "+df.format(new Date())+")"; } else{ //生成修改信息 content="------(此留言被用户 "+modifyauthor+" 修改过,时间: "+df.format(new Date())+")"; } messageDO.setContent(content); messageDO.setAuthor(null); boolean result = false; // 插入数据库 result = messageService.updateMessage(messageDO); // 校验结果及跳转 if (result) { nav.redirectTo("messageBoardLink").withTarget("message/messageList"); } else { nav.redirectTo("messageBoardLink").withTarget("message/messageList").withParameter("message", "修改操作失败!"); } } }?
?
RegisterAction.java
?
?
package com.alibaba.webx3.messageboard.module.action; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.Navigator; import com.alibaba.citrus.turbine.dataresolver.FormGroup; import com.alibaba.webx3.messageboard.biz.service.UserService; import com.alibaba.webx3.messageboard.dao.object.UserDO; public class RegisterAction { @Autowired private UserService userService; //用户注册 public void doRegister(@FormGroup(name="register") UserDO user, Context context,Navigator nav ){ //检查已存在用户名,可以用jquery实现 UserDO reserDO =userService.getByUsername(user.getUsername()); if(reserDO!=null){ context.put("message", "用户名已存在,请重新注册!"); nav.forwardTo("register"); return; } //保存用户注册 UserDO userDO = new UserDO(); userDO.setUsername(user.getUsername()); userDO.setPassword(user.getPassword()); boolean result=false; result = userService.addUser(userDO); if(result){ context.put("username", user.getUsername()); nav.forwardTo("message/messageList"); }else{ context.put("message", "注册失败!"); nav.forwardTo("register"); } } }
?
?
addMessage.java
?
?
package com.alibaba.webx3.messageboard.module.screen.message; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; public class addMessage { public void execute(@Param(name="message") String message,Context context){ context.put("message", message); } }?
?
MessageList.java
?
?
package com.alibaba.webx3.messageboard.module.screen.message; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.webx3.messageboard.biz.service.MessageService; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public class MessageList { @Autowired private MessageService messageService; public void execute(@Param(name="message") String message,Context context){ context.put("message", message); //获取登陆的用户名 //查出一部分内容 Map<String,Integer> map = new HashMap<String,Integer>(); map.put("from", 0); map.put("size", 10); List<MessageDO> messageList= messageService.getMessageList(map); if(messageList==null){ context.put("message", "数据库操作失败!"); return; } context.put("messageList", messageList); } }
?
?
ModifyMessage.java
?
?
package com.alibaba.webx3.messageboard.module.screen.message; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.webx3.messageboard.biz.service.MessageService; import com.alibaba.webx3.messageboard.dao.object.MessageDO; public class ModifyMessage { @Autowired private MessageService messageService; public void execute(@Param(name="messageId")int messageId,Context context){ MessageDO messageDO; messageDO = messageService.getMessageById(messageId); if(messageDO==null){ context.put("message", "获取数据失败!"); } else{ context.put("messageDO", messageDO); } } }
?
最后将这些类配置到biz-bean.xml中,然后将其配置到webx-messageboard.xml中,实现注入到spring容器中。
?
?
biz-bean.xml
?
?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd default-autowire="byName"> <!-- service beans --> <bean id="userService" class="com.alibaba.webx3.messageboard.biz.service.impl.UserServiceImpl" /> <bean id="messageService" class="com.alibaba.webx3.messageboard.biz.service.impl.MessageServiceImpl" /> <!-- DAO beans --> <bean id="userDAO" class="com.alibaba.webx3.messageboard.dao.impl.UserDAOImpl" /> <bean id="messageDAO" class="com.alibaba.webx3.messageboard.dao.impl.MessageDAOImpl" /> </beans>?
?
?
webx-messageboard.xml
?
?
...... <!-- beans配置 --> <beans:import resource="common/biz-bean.xml" /> ......?
?
?