《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. Maven解决包冲突

    依赖树 $ mvn dependency:tree [WARNING] [WARNING] Some problems were encountered while building the effe ...

  2. 论文笔记:Heterogeneous Memory Enhanced Multimodal Attention Model for Video Question Answering

    Heterogeneous Memory Enhanced Multimodal Attention Model for Video Question Answering 2019-04-25 21: ...

  3. ZJOI 2014 星系调查(推导)

    题意 https://loj.ac/problem/2201 思路 说白了就是一条路径上有 \(n\) 个二维坐标,求一条直线使得所有点到此直线的距离和最小. 设这条直线为 \(y=kx+b\) ,距 ...

  4. git命令-切换分支

    git一般有很多分支,我们clone到本地的时候一般都是master分支,那么如何切换到其他分支呢? 1. 查看远程分支 $ git branch -a 我在mxnet根目录下运行以上命令: ~/mx ...

  5. Unity 根据手机陀螺仪,实现流动UI效果

    Unity 根据手机陀螺仪,实现流动UI效果 设置Canvas 模式设置为 Screen Space - Camera 指定Camera 挂载脚本 挂载Target using System; usi ...

  6. 【NET Core】Nuget包发布流程

    1.新建一个.NET Core类库 2.新增一个方法,并编译项目 3.下载Nuget.exe,与刚才新建的类库放在同一目录下 下载地址:https://www.nuget.org/downloads ...

  7. vue_全局注册过滤器

    在一个项目中, 某些过滤器全局都有可能用的到, 统一管理并自动化全局注册是很方便的. 代码如下, 后续只需要在src/filters/index.js中添加方法就可以全局使用过滤器了. // src/ ...

  8. h5视频播放

    h5视频播放 一直在写js原生的东西,感觉总是停滞不前,现在我们应该学一些h5新的特性,因为我们毕竟是从事前端的,下面我们一起来写一个视频播放吧 1,html <div class=" ...

  9. web 后台返回json格式数据的方式(status 406)

    1.在类上使用注解 @RestController public class HttpComentInterface { } 2.在方法是使用注解  @ResponseBody @RequestMap ...

  10. 在线批量将gps经纬度坐标转换为百度经纬度坐标

    1.首先打开百度api示例页面: 在浏览器地址栏中输入:http://developer.baidu.com/map/jsdemo.htm#a5_3 2.修改代码 如下图,将需要批量转换的坐标,按规则 ...