标签:struts hibernate spring mysql 项目实战
上次我们完成了日志模块的配置和基础增删改查类,下面我们根据用户的需求来正式开发项目的业务模块。
package cn.edu.hpu.tax.user.entity; import java.io.Serializable; import java.util.Date; public class User implements Serializable{ private String id; private String name; private String account; private String password; //所属部门 private String dept; //头像(相对地址) private String headImg; //性别 private boolean gender; private String email; private String mobile; //备注 private String memo; private Date birthday; private String state; //用户状态 public final static String USER_STATE_VALID = "1";//有效 public final static String USER_STATE_INVALID = "0";//无效 public User() { } public User(String id, String name, String account, String password, String dept, String headImg, boolean gendar, String email, String mobile, String memo, Date birthday, String state) { this.id = id; this.name = name; this.account = account; this.password = password; this.dept = dept; this.headImg = headImg; this.gender = gendar; this.email = email; this.mobile = mobile; this.memo = memo; this.birthday = birthday; this.state = state; } //get与set方法省略 }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.edu.hpu.tax.user.entity.User" table="user"> <id name="id" type="java.lang.String"> <column name="id" length="32" /> <generator class="uuid.hex" /> </id> <property name="name" type="java.lang.String"> <column name="name" length="20" not-null="true" /> </property> <property name="dept" type="java.lang.String"> <column name="dept" length="20" not-null="true" /> </property> <property name="account" type="java.lang.String"> <column name="account" length="50" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="50" not-null="true" /> </property> <property name="headImg" type="java.lang.String"> <column name="headImg" length="100" /> </property> <property name="gender" type="java.lang.Boolean"> <column name="gender" /> </property> <property name="email" type="java.lang.String"> <column name="email" length="50" /> </property> <property name="mobile" type="java.lang.String"> <column name="mobile" length="20" /> </property> <property name="birthday" type="java.util.Date"> <column name="birthday" length="10" /> </property> <property name="state" type="java.lang.String"> <column name="state" length="1" /> </property> <property name="memo" type="java.lang.String"> <column name="memo" length="200" /> </property> </class> </hibernate-mapping>
package cn.edu.hpu.tax.user.dao; import cn.edu.hpu.tax.core.dao.BaseDao; import cn.edu.hpu.tax.user.entity.User; public interface UserDao extends BaseDao<User>{ }
package cn.edu.hpu.tax.user.dao.impl; import cn.edu.hpu.tax.core.dao.impl.BaseDaoImpl; import cn.edu.hpu.tax.user.dao.UserDao; import cn.edu.hpu.tax.user.entity.User; public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{ }
package cn.edu.hpu.tax.user.service; import java.io.Serializable; import java.util.List; import cn.edu.hpu.tax.user.entity.User; public interface UserService { //新增 public void save(User user); //更新 public void update(User user); //根据id删除 public void delete(Serializable id); //根据id查找 public User findObjectById(Serializable id); //查找列表 public List<User> findObjects(); }
package cn.edu.hpu.tax.user.service.impl; import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import cn.edu.hpu.tax.user.dao.UserDao; import cn.edu.hpu.tax.user.entity.User; import cn.edu.hpu.tax.user.service.UserService; @Service("userService") public class UserServiceImpl implements UserService{ @Resource private UserDao userDao; @Override public void save(User user) { userDao.save(user); } @Override public void update(User user) { userDao.update(user); } @Override public void delete(Serializable id) { userDao.delete(id); } @Override public User findObjectById(Serializable id) { return userDao.findObjectById(id); } @Override public List<User> findObjects() { return userDao.findObjects(); } }
package cn.edu.hpu.tax.user.action; import java.util.List; import javax.annotation.Resource; import cn.edu.hpu.tax.user.entity.User; import cn.edu.hpu.tax.user.service.UserService; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ @Resource private UserService userService; private List<User> userList; private User user; //列表页面 public String listUI(){ userList=userService.findObjects(); return "listUI"; } //跳转到新增页面 public String addUI(){ return "addUI"; } //保存新增 public String add(){ if(user!=null){ userService.save(user); } return listUI(); } //跳转到编辑界面 public String editUI(){ if(user!=null && user.getId()!=null){ user=userService.findObjectById(user.getId()); } return "editUI"; } //保存编辑 public String edit(){ if(user!=null){ userService.update(user); } return listUI(); } //删除 public String delete(){ if(user!=null && user.getId()!=null){ userService.delete(user.getId()); } return listUI(); } //批量删除 public String deleteSelected(){ return listUI(); } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }基础的我们都写完了,以后需要添加新的代码,我们暂时先写这些。
<!-- 用来注入sessionFactory的抽象类 --> <bean id="xDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 继承了注入sessionFactory的抽象类,不用反复出入sessionFactory --> <bean id="userDao" class="cn.edu.hpu.tax.user.dao.impl.UserDaoImpl" parent="xDao"></bean> <!-- 扫描Service --> <context:component-scan base-package="cn.edu.hpu.tax.user.service.impl"></context:component-scan> </beans>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="user-action" namespace="/tax" extends="struts-default"> <action name="user_*" class="cn.edu.hpu.tax.user.action.UserAction" method="{1}"> <result name="{1}">/WEB-INF/jsp/tax/user/{1}.jsp</result> </action> </package> </struts>
<!-- 包含user的struts配置文件 --> <include file="cn/edu/hpu/tax/user/conf/user-struts.xml"/>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% pageContext.setAttribute("basePath", request.getContextPath()+"/") ; %> <script type="text/javascript" src="${basePath}js/jquery/jquery-1.10.2.min.js"></script> <link href="${basePath}css/skin1.css" rel="stylesheet" type="text/css" />
<%@include file="/common/header.jsp" %>
我们的用户列表界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户管理</title> <%@include file="/common/header.jsp" %> <script type="text/javascript"> //全选、全反选 function doSelectAll(){ // jquery 1.6 前 //$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked")); //prop jquery 1.6+建议使用 $("input[name=selectedRow]").prop("checked", $("#selAll").is(":checked")); } </script> </head> <body class="rightBody"> <form name="form1" action="" method="post" enctype="multipart/form-data"> <div class="p_d_1"> <div class="p_d_1_1"> <div class="content_info"> <div class="c_crumbs"><div><b></b><strong>用户管理</strong></div> </div> <div class="search_art"> <li> 用户名:<s:textfield name="user.name" cssClass="s_text" id="userName" cssStyle="width:160px;"/> </li> <li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li> <li style="float:right;"> <input type="button" value="新增" class="s_button" onclick="doAdd()"/> <input type="button" value="删除" class="s_button" onclick="doDeleteAll()"/> <input type="button" value="导出" class="s_button" onclick="doExportExcel()"/> <input name="userExcel" type="file"/> <input type="button" value="导入" class="s_button" onclick="doImportExcel()"/> </li> </div> <div class="t_list" style="margin:0px; border:0px none;"> <table width="100%" border="0"> <tr class="t_tit"> <td width="30" align="center"><input type="checkbox" id="selAll" onclick="doSelectAll()" /></td> <td width="140" align="center">用户名</td> <td width="140" align="center">帐号</td> <td width="160" align="center">所属部门</td> <td width="80" align="center">性别</td> <td align="center">电子邮箱</td> <td width="100" align="center">操作</td> </tr> <tr bgcolor="f8f8f8"> <td align="center"><input type="checkbox" name="selectedRow" value=""/></td> <td align="center">xxx</td> <td align="center"></td> <td align="center"></td> <td align="center"></td> <td align="center"></td> <td align="center"> <a href="javascript:doEdit(id)">编辑</a> <a href="javascript:doDelete(id)">删除</a> </td> </tr> </table> </div> </div> <div class="c_pate" style="margin-top: 5px;"> <table width="100%" class="pageDown" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right"> 总共1条记录,当前第 1 页,共 1 页 <a href="#">上一页</a> <a href="#">下一页</a> 到 <input type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1" max="" value="1" /> </td> </tr> </table> </div> </div> </div> </form> </body> </html>
现在还是什么都没有的状态,我们下一次完善我们的这些操作包括输出信息。
转载请注明出处:http://blog.csdn.net/acmman/article/details/49329509
版权声明:本文为博主原创文章,未经博主允许不得转载。
【SSH项目实战】国税协同平台-4.用户管理需求分析&CRUD方法1
标签:struts hibernate spring mysql 项目实战
原文地址:http://blog.csdn.net/acmman/article/details/49329509