《Struts2+Spring4+hibernate3》

工程结构

导入相关jar包:自行网上百度即可,根据环境需要

写dao类 以及实现

package com.icommon.dao;

import java.awt.HeadlessException;
import java.util.List; import org.hibernate.HibernateException; public interface BaseDao { public void saveObject(Object obj) throws HibernateException; public List login(String name,String pwd) throws HibernateException; public List getAllRecord(Object obj) throws HibernateException; public void delete(Object obj) throws HibernateException; public void update(Object obj) throws HibernateException;
}

实现:

package com.icommon.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class UserDao extends HibernateDaoSupport implements BaseDao { @Override
public void saveObject(Object obj) throws HibernateException {
getHibernateTemplate().save(obj);
} @Override
public List login(String name,String pwd) throws HibernateException {
List result = null;
System.out.println("Userdao执行了");
Object[] values = new String[]{name, pwd};
String sql="from Employee e where e.empName=? and e.password=?";
result = getHibernateTemplate().find(sql, values);
System.out.println("Userdao执行了1"+result);
return result;
} @Override
public List getAllRecord(Object obj) throws HibernateException {
List result = null;
System.out.println("Userdao执行了1"+obj);
String sql = "from " +obj;
System.out.println("sql="+sql);
result = getHibernateTemplate().find(sql);
System.out.println(result);
return result;
} @Override
public void delete(Object obj) throws HibernateException {
getHibernateTemplate().delete(obj);
} @Override
public void update(Object obj) throws HibernateException {
getHibernateTemplate().update(obj); } }

service以及实现

package com.icommon.service;

import java.util.List;

public interface UserManager {

	 public void regUser(Object obj); 

	 public List login(String name,String pwd);

	 public List getallRecord(Object obj);

	 public void delete(Object obj);

	 public void update(Object obj);
}

实现

package com.icommon.serviceimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.springframework.stereotype.Service; import com.icommon.dao.BaseDao;
import com.icommon.dao.UserDao;
import com.icommon.service.UserManager; @Service
public class UserManagerImpl implements UserManager { private BaseDao baseDao; public BaseDao getBaseDao() {
return baseDao;
} public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
} public UserManagerImpl() {
baseDao = new UserDao();
} @Override
public void regUser(Object obj) throws HibernateException{ baseDao.saveObject(obj); } @Override
public List login(String name,String pwd) {
System.out.println("name="+name+"pws="+pwd);
return baseDao.login(name,pwd); } @Override
public List getallRecord(Object obj) {
System.out.println("Object"+obj);
return baseDao.getAllRecord(obj);
} @Override
public void delete(Object obj) {
baseDao.delete(obj); } @Override
public void update(Object obj) {
baseDao.update(obj); } }

action

package com.icommon.action;

import java.lang.ProcessBuilder.Redirect;
import java.util.List; import com.icommon.base.Employee;
import com.icommon.service.UserManager;
import com.opensymphony.xwork2.ActionSupport; public class EmployeeAction extends ActionSupport{ private static final long serialVersionUID = -8201688456630641714L; Employee employee; public Employee getEmployee() {
return employee;
} public void setEmployee(Employee employee) {
this.employee = employee;
} private UserManager userManager; public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public String save(){
try{
System.out.println("action执行了");
userManager.regUser(employee);
return SUCCESS;
}catch (Exception e) {
e.printStackTrace();
return ERROR;
} } public String login(){
try {
List result = null;
System.out.println("action执行了1");
String name = employee.getEmpName();
String pwd = employee.getPassword();
result= userManager.login(name,pwd);
System.out.println("action执行了2");
if(result.size()==1){
System.out.println("action执行了3");
return SUCCESS; }else{
System.out.println("action执行了4");
return ERROR;
}
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
} public String findALLRecord(){
List result = null;
try {
result = userManager.getallRecord(Employee.class.getSimpleName());
} catch (Exception e) {
e.printStackTrace();
return ERROR;
} return SUCCESS;
}
public String update(){
try{
System.out.println("action33执行了");
userManager.regUser(employee);
return SUCCESS;
}catch (Exception e) {
e.printStackTrace();
return ERROR;
}
} public String delete(){
try{
System.out.println("action33执行了");
Employee emp = new Employee();
emp.setEmpId(employee.getEmpId());
emp.setEmpName(employee.getEmpName());
emp.setPassword(employee.getPassword());
emp.setGender(employee.getGender());
userManager.delete(emp);
return SUCCESS;
}catch (Exception e) {
e.printStackTrace();
return ERROR;
}
} }
package com.icommon.action;

import com.icommon.base.User;
import com.icommon.service.UserManager;
import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private static final long serialVersionUID = -4346820094344570846L; private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} private UserManager userManager; public void setUserManager(UserManager userManager) {
this.userManager = userManager;
} public String execute() {
try {
userManager.regUser(user);
return SUCCESS; } catch (Exception e) {
e.printStackTrace();
return ERROR;
}
} }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>userproject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 添加struts核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>conctextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>
<filter> <filter-name>SpringEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

applicationComtext.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
default-lazy-init="true">
<!-- 配置连接池: -->
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池: -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 必须设置编码格式 否则入库为乱码 -->
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置hibernate属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>com/icommon/base/User.hbm.xml</value>
</list>
</property>
<!-- 配置Hibernate的其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<!-- 开机自动生成表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <bean id="baseDao" class="com.icommon.dao.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean> <!--用户注册业务逻辑类 -->
<bean id="userManager" class="com.icommon.serviceimpl.UserManagerImpl">
<property name="baseDao">
<ref bean="baseDao" />
</property>
</bean> <!-- 用户注册的Action -->
<bean id="regAction" class="com.icommon.action.RegisterAction">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>
<bean id="employeeAction" class="com.icommon.action.EmployeeAction">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>
</beans>

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>
<include file="struts-default.xml" />
<package name="default" extends="struts-default">
<action name="register" class="regAction" method="execute">
<result name="input">/Register.jsp</result>
<result name="success">/success.jsp</result>
<result name="error">/Fail.jsp</result>
</action>
<action name="employee" class="employeeAction" method="save">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
<result name="error">/Fail.jsp</result>
</action>
<action name="userlogin" class="employeeAction" method="login">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
<result name="error">/Fail.jsp</result>
</action>
<action name="allRecord" class="employeeAction" method="findALLRecord">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
<result name="error">/Fail.jsp</result>
</action>
</package>
</struts>

hibernate映设文件

<?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 package="com.icommon.base">
<class name="User" table="user">
<id name="userId"
type="int">
<column name="userId" />
<generator class="native" />
</id>
<property name="username" column="userName" type="string"
not-null="true" length="16"></property>
<property name="password" column="password" type="string"
not-null="true" length="16" />
<property name="gender" column="gender" type="int" length="1" />
</class>
<class name="Employee"
table="EMPLOYEE">
<id name="empId" type="int">
<column name="EMPID" />
<generator class="native" />
</id>
<property name="empName"
type="string">
<column name="EMPNAME" />
</property>
<property name="password"
type="string">
<column name="PASSWORD" />
</property>
<property name="gender"
type="int">
<column name="GENDER" />
</property>
</class>
</hibernate-mapping>

完成了一些简单的封装,要开发新功能只需在action  hibernate映射文件,struts.xml applicationcontext.xml文件编写代码即可 调用公共组件可完成增删改查,功能不完善 需要什么公共自行添加即可

页面见到就不列出,自行设计

搭建SSH框架心得的更多相关文章

  1. myeclipse搭建SSH框架

    搭建SSH框架 Struts+hibernater+spring架构(myeclipse) 右击,首先加入spring,加入hibernater,再加入struts2 复制jar包(把tomcat发布 ...

  2. MyEclipse8.5快速搭建SSH框架

    来源于:http://jingyan.baidu.com/article/a378c960a78125b3282830cc.html MyEclipse8.5快速搭建SSH框架 使用版本: Strut ...

  3. [JavaEE] SSH框架笔记_eclipse搭建SSH框架详解

    SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题.下面我介绍一下SSH框架搭建的全过程. 第一步:准备工作. 下载好eclipse,Struts2,Spring,Hibe ...

  4. Maven环境下搭建SSH框架

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Maven:3.3.9 Struts2:2.5.10 Spring:4.3.8.RELEASE Hibernate:5.1.7.Fina ...

  5. Maven环境下搭建SSH框架之Spring整合Hibernate

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...

  6. 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...

  7. eclipse Indigo搭建SSH框架详解

    SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题.下面我介绍一下SSH框架搭建的全过程. 第一步:准备工作.   下载好eclipse,Struts2,Spring,Hi ...

  8. Java之基于Eclipse搭建SSH框架(下)

    在上篇博客里,我简介了Tomcat滴配置与Struts2滴搭建,假设对这个还不会滴童鞋去看一下我滴上篇博客<Java之基于Eclipse搭建SSH框架(上)>.今天我们接着上篇博客滴内容. ...

  9. MeEclipse搭建SSH框架之———大体框架

    必备:MyEclipse软件,SSH需要的jar包,数据库,连接数据库的驱动jar包 先搭建大体框架,再加别的东西,要不都不知道哪里错了 一.新建web项目 MyEclipse左边右键--->N ...

随机推荐

  1. 关于transactionscope 事务的脏数据

    在一个项目中,关于新客的检测,如果查询到积分记录就是老客,因为检测的之前先积分,因为使用的是事务,造成本地代码没提交前已经有该会员的积分记录,而到数据库中却查询不到记录.造成永远都是老客,解决的方法是 ...

  2. 【HNOI 2016】大数

    Problem Description 小 B 有一个很大的数 \(S\),长度达到了 \(N\) 位:这个数可以看成是一个串,它可能有前导 \(0\),例如 00009312345 .小 B 还有一 ...

  3. 【HNOI 2018】毒瘤

    Problem Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(例如给一个区间内的数同时加上 \(c ...

  4. Table组件设置文字超出宽度显示省略号,鼠标悬停以悬浮框显示

    一.设置文字超出宽度显示省略号 注意点: 1.  需要指定column的width属性,否则列头跟内容可能不对齐.需要留一列不设置宽度以适应弹性布局. 2. 列宽度width必须大于ellipsis的 ...

  5. Inception部署

    Inception是集审核.执行.回滚于一体的一个SQL自动化运维系统,基于MySQL代码修改,官方文档地址:https://mysql-inception.github.io/inception-d ...

  6. sublime text3使用

    设置tab键为若干空格 { "tab_size": 4, "translate_tabs_to_spaces": true }

  7. 【XAF问题】多个属性验证RuleUniqueValue

    一.问题 1. 在XAF中如何验证多个属性唯一值? 二.解决方法 使用RuleCombinationOfPropertiesIsUnique [RuleCombinationOfPropertiesI ...

  8. Mock.js——数据模板定义

    1. 安装 npm install mockjs --save-dev //安装开发依赖 2. 数据模板定义规则 Mock.mock({...}) String: 'string|num1-num2' ...

  9. Netty客户端发送消息并同步获取结果

    客户端发送消息并同步获取结果,其实是违背Netty的设计原则的,但是有时候不得不这么做的话,那么建议进行如下的设计: 比如我们的具体用法如下: NettyRequest request = new N ...

  10. js插入HTML代码,渲染页面

    <style>td{border:solid 1px #000;}</style> <table><tr id="data_list"&g ...