本文主要是介绍项目开发:SSH 之“department”模块从零开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目开发:SSH 之“department”模块从零开发
前言:
怎么从零开始做一个模块? 只给你需求,用编程语言去实现,这就是现实的公司要求的基本功。
今天小编就对项目中的一个模块“部门管理”来对SSH框架怎么用,进行一个summary,手把手的进行一个总结。
步骤:
一、设计
1,设计实体(pojo或domain)/建表
2,分析该“department” 模块有几个功能,对应的几个请求。
3,功能的实现:
(1)写Action类,写Action中的方法,确定Service中的方法
(2)写Service,ServiceImple中方法,确定DAO中方法
(3)写Dao和DaoImpl层方法(如果有了BaseDao则让Dao继承BaseDao)
(4)最后写页面,写jsp,页面写完了,增删改查也就完成了,进行测试一下就OK。
二、具体的代码实现:
1,设计实体/ 建立department表(以ID,name,description三个字段为例)
package cn.itcast.oa.domain;
import java.util.HashSet;
import java.util.Set;/*** 部门 * * @author DanielStark * */
public class Department {private Long id;private Set<User> users = new HashSet<User>();private Department parent;private Set<Department> children = new HashSet<Department>();private String name;private String description;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public Set<User> getUsers() {return users;}public void setUsers(Set<User> users) {this.users = users;}public Department getParent() {return parent;}public void setParent(Department parent) {this.parent = parent;}public Set<Department> getChildren() {return children;}public void setChildren(Set<Department> children) {this.children = children;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}
}
2,写Department.hbm.xml映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.oa.domain"><class name="Department" table="itcast_department"><id name="id"><generator class="native" /></id><property name="name" /><property name="description" /><!-- users属性,本类与User的一对多 --><set name="users"><key column="departmentId"></key><one-to-many class="User" /></set><!-- parent属性,本类与Department(上级)的多对一 --><many-to-one name="parent" class="Department" column="parentId"></many-to-one><!-- children属性,本类与Department(下级)的一对多 --><set name="children"><key column="parentId"></key><one-to-many class="Department" /></set></class></hibernate-mapping>
3,将映射文件进行配置:
4,建立连接数据库的jdbc.properties文件;和
jdbcUrl = jdbc:mysql://192.168.21.51:3306/itcastoa0720
driverClass = com.mysql.jdbc.Driver
user = root
password = 1
#jdbcUrl = jdbc:mysql://localhost:8081/itcastoa0720
5,日志log4J.properties.xml文件;
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=error, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
log4j.logger.cn.itcast.oa=debug
6,spring文件applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 自动扫描与装配bean --><context:component-scan base-package="cn.itcast.oa"/><!-- 导入外部的properties文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 指定hibernate的配置文件位置 --><property name="configLocation" value="classpath:hibernate.cfg.xml"></property><!-- 配置c3p0数据库连接池 --><property name="dataSource"><bean class="com.mchange.v2.c3p0.ComboPooledDataSource"><!-- 数据连接信息 --><property name="jdbcUrl" value="${jdbcUrl}"></property><property name="driverClass" value="${driverClass}"></property><property name="user" value="${user}"></property><property name="password" value="${password}"></property><!-- 其他配置 --><!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="3"></property><!--连接池中保留的最小连接数。Default: 3 --><property name="minPoolSize" value="3"></property><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="5"></property><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="3"></property><!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --><property name="maxStatements" value="8"></property><!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --><property name="maxStatementsPerConnection" value="5"></property><!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="1800"></property></bean></property></bean><!-- 配置声明式事务管理(采用注解的方式) --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><tx:annotation-driven transaction-manager="txManager"/>
</beans>
7,Struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><!-- 配置为开发模式 --><constant name="struts.devMode" value="true" /><!-- 把扩展名配置为action --><constant name="struts.action.extension" value="action" /><!-- 把主题配置为simple --><constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 --><!-- 当Struts2与Spring整合后,class属性可以写bean的名称 --><action name="test" class="testAction"><result name="success">/test.jsp</result></action> <!-- 岗位管理 --><action name="role_*" class="roleAction" method="{1}"><result name="list">/WEB-INF/jsp/roleAction/list.jsp</result><result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result><!-- <result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result><result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result> --><result name="toList" type="redirectAction">role_list</result></action> <!-- 部门管理 --><action name="department_*" class="departmentAction" method="{1}"><result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result><result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result><result name="toList" type="redirectAction">department_list</result></action></package><!-- Add packages here -->
</struts>
8,建表:testsessionFactory就行
单独建立一个test文件夹进行单独的测试用。
// 测试SessionFactory@Testpublic void testSessionFactory() throws Exception {SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");System.out.println(sessionFactory);}
运行sessionFactory之后表自动就出来了如下:
三、分析有几个功能,请求
比如增删改查方法,分析并先写好方法名,返回值,对应页面名称统一下来。
四,底层代码实现
1,写Action中方法,确定Service方法
package cn.itcast.oa.view.action;import java.util.List;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;import cn.itcast.oa.domain.Department;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.DepartmentService;
import cn.itcast.oa.service.RoleService;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;@Controller//这两个注入的目的是要找到Struts里面的bean---2017-5-12 22:18:45
@Scope("prototype")
//public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{//ModelDriven用于封装表达当中传过来的参数@Resourceprivate DepartmentService departmentService;// private Long id;// private String name;// private String description;private Department model = new Department();public Department getModel() {return model;}/*列表*/public String list()throws Exception{List<Department>departmentList=departmentService.findAll();ActionContext.getContext().put("departmentList",departmentList);return "list";}/*增加*/public String add()throws Exception{departmentService.save(model);return "toList";}/*增加页面*/public String addUI() throws Exception{return "saveUI";}/*删除*/public String delete() throws Exception{departmentService.delete(model.getId());return "toList";}/*修改*/public String edit() throws Exception {// 1,从数据库中获得原对象Department department = departmentService.getById(model.getId());// 2,设置要修改的属性department.setName(model.getName());department.setDescription(model.getDescription());// 3,更新到数据库中departmentService.update(department);return "toList";}/*修改页面*/public String editUI()throws Exception{//准备回显数据//取车要回显的数据Department department=departmentService.getById(model.getId()); //将取出来的数据放入栈顶就能进行回显 ActionContext.getContext().getValueStack().push(department);return "saveUI";}
}
2,service层代码:
package cn.itcast.oa.service;import java.util.List;import cn.itcast.oa.domain.Department;public interface DepartmentService{// 查询所有List<Department> findAll();void delete(Long id);void save(Department role);Department getById(Long id);void update(Department role);
}
3,ServiceImpl代码:
package cn.itcast.oa.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.oa.dao.DepartmentDao;
import cn.itcast.oa.domain.Department;
import cn.itcast.oa.service.DepartmentService;@Service //注入spring,交给容器管理
@Transactional //事物管理
public class DepartmentServiceImpl implements DepartmentService{@Resourceprivate DepartmentDao departmentDao; public List<Department> findAll() { return departmentDao.findAll();}public void delete(Long id) {departmentDao.delete(id);}public void save(Department department) {departmentDao.save(department);}public Department getById(Long id) {return departmentDao.getById(id);}public void update(Department department) {departmentDao.update(department);}
}
4,写Dao层:
package cn.itcast.oa.dao;
import cn.itcast.oa.base.BaseDao;
import cn.itcast.oa.domain.Department;public interface DepartmentDao extends BaseDao<Department>{}
5,DaoImpl层代码:
package cn.itcast.oa.dao.impl;
import org.springframework.stereotype.Repository;
import cn.itcast.oa.base.BaseDaoImpl;
import cn.itcast.oa.dao.DepartmentDao;
import cn.itcast.oa.domain.Department;@Repository//注解,交给容器管理
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implementsDepartmentDao {}
6,Dao可以继承Base,BaseDao代码如下:
package cn.itcast.oa.base;
import java.util.List;
public interface BaseDao<T> {/*** 保存实体* * @param entity*/void save(T entity);/*** 删除实体* * @param id*/void delete(Long id);/*** 更新实体* * @param entity*/void update(T entity);/*** 按id查询* * @param id* @return*/T getById(Long id);/*** 按id查询* * @param ids* @return*/List<T> getByIds(Long[] ids);/*** 查询所有* * @return*/List<T> findAll();
}
7,BaseDaoImpl层代码:
package cn.itcast.oa.base;import java.lang.reflect.ParameterizedType;
import java.util.List;import javax.annotation.Resource;import org.hibernate.Session;
import org.hibernate.SessionFactory;@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> {@Resourceprivate SessionFactory sessionFactory;private Class<T> clazz;public BaseDaoImpl() {// 使用反射技术得到T的真实类型ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型System.out.println("clazz ---> " + clazz);}/*** 获取当前可用的Session* * @return*/protected Session getSession() {return sessionFactory.getCurrentSession();}public void save(T entity) {getSession().save(entity);}public void update(T entity) {getSession().update(entity);}public void delete(Long id) {Object obj = getById(id);if (obj != null) {getSession().delete(obj);}}public T getById(Long id) {return (T) getSession().get(clazz, id);}public List<T> getByIds(Long[] ids) {return getSession().createQuery(//"FROM User WHERE id IN (:ids)")//.setParameterList("ids", ids)//.list();}public List<T> findAll() {return getSession().createQuery(//"FROM " + clazz.getSimpleName())//.list();}}
五、前台页面
1,list.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>部门列表</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script><script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script><script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script><link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" /><script type="text/javascript"></script>
</head>
<body><div id="Title_bar"><div id="Title_bar_Head"><div id="Title_Head"></div><div id="Title"><!--页面标题--><img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 部门管理</div><div id="Title_End"></div></div>
</div><div id="MainArea"><table cellspacing="0" cellpadding="0" class="TableStyle"><!-- 表头--><thead><tr align=center valign=middle id=TableTitle><td width="150px">部门名称</td><td width="150px">上级部门名称</td><td width="200px">职能说明</td><td>相关操作</td></tr></thead><!--显示数据列表--><tbody id="TableData" class="dataContainer" datakey="departmentList"><s:iterator value="#departmentList"><tr class="TableDetail1 template"><td>${name} </td><td>${parent.name} </td><td>${description} </td><td><s:a action="department_delete?id=%{id}" οnclick="return window.confirm('这将删除所有的下级部门,您确定要删除吗?')">删除</s:a><s:a action="department_editUI?id=%{id}">修改</s:a></td></tr></s:iterator> </tbody></table><!-- 其他功能超链接 --><div id="TableTail"><div id="TableTail_inside"><s:a action="department_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a></div></div>
</div><!--说明-->
<div id="Description"> 说明:<br />1,列表页面只显示一层的(同级的)部门数据,默认显示最顶级的部门列表。<br />2,点击部门名称,可以查看此部门相应的下级部门列表。<br />3,删除部门时,同时删除此部门的所有下级部门。
</div></body>
</html>
2,saveUI.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>部门设置</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script><script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script><script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script><link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
</head>
<body><!-- 标题显示 -->
<div id="Title_bar"><div id="Title_bar_Head"><div id="Title_Head"></div><div id="Title"><!--页面标题--><img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 部门信息</div><div id="Title_End"></div></div>
</div><!--显示表单内容-->
<div id=MainArea><s:form action="department_%{id == null ? 'add' : 'edit'}"><s:hidden name="id"></s:hidden><div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1"><IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 部门信息 </DIV> --></div><!-- 表单内容显示 --><div class="ItemBlockBorder"><div class="ItemBlock"><table cellpadding="0" cellspacing="0" class="mainForm"><tr><td width="100">上级部门</td><td><select name="parentId" class="SelectStyle"><option value="">请选择部门</option><option value="7">┠总经理室</option><option value="1">┠市场部</option><option value="2"> ┠咨询部</option><option value="3"> ┠招生部</option><option value="4">┠教学部</option><option value="5">┠后勤部</option></select></td></tr><tr><td>部门名称</td><td><s:textfield name="name" cssClass="InputStyle"/> *</td></tr><tr><td>职能说明</td><td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td></tr></table></div></div><!-- 表单操作 --><div id="InputDetailBar"><input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/><a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a></div></s:form>
</div><div class="Description">说明:<br />1,上级部门的列表是有层次结构的(树形)。<br/>2,如果是修改:上级部门列表中不能显示当前修改的部门及其子孙部门。因为不能选择自已或自已的子部门作为上级部门。<br />
</div>
</body>
</html>
测试:
运行服务器tomcat,输入访问地址:http://localhost:8081/ItcastOA/department_list.action
页面显示出来结果如下:
总结:
在公司中是只给需求,要求实现模块,分析出主线之后,接下了就是自己编码,上面就是一个非常有代表性的例子,一条线从前台到后台所有让整个系统运行起来的编码设计原则,代码,希望对您有用。编程技术之路,有趣、好玩,分享+。
这篇关于项目开发:SSH 之“department”模块从零开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!