《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框架心得的更多相关文章
-
myeclipse搭建SSH框架
搭建SSH框架 Struts+hibernater+spring架构(myeclipse) 右击,首先加入spring,加入hibernater,再加入struts2 复制jar包(把tomcat发布 ...
-
MyEclipse8.5快速搭建SSH框架
来源于:http://jingyan.baidu.com/article/a378c960a78125b3282830cc.html MyEclipse8.5快速搭建SSH框架 使用版本: Strut ...
-
[JavaEE] SSH框架笔记_eclipse搭建SSH框架详解
SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题.下面我介绍一下SSH框架搭建的全过程. 第一步:准备工作. 下载好eclipse,Struts2,Spring,Hibe ...
-
Maven环境下搭建SSH框架
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Maven:3.3.9 Struts2:2.5.10 Spring:4.3.8.RELEASE Hibernate:5.1.7.Fina ...
-
Maven环境下搭建SSH框架之Spring整合Hibernate
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...
-
搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...
-
eclipse Indigo搭建SSH框架详解
SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题.下面我介绍一下SSH框架搭建的全过程. 第一步:准备工作. 下载好eclipse,Struts2,Spring,Hi ...
-
Java之基于Eclipse搭建SSH框架(下)
在上篇博客里,我简介了Tomcat滴配置与Struts2滴搭建,假设对这个还不会滴童鞋去看一下我滴上篇博客<Java之基于Eclipse搭建SSH框架(上)>.今天我们接着上篇博客滴内容. ...
-
MeEclipse搭建SSH框架之———大体框架
必备:MyEclipse软件,SSH需要的jar包,数据库,连接数据库的驱动jar包 先搭建大体框架,再加别的东西,要不都不知道哪里错了 一.新建web项目 MyEclipse左边右键--->N ...
随机推荐
-
arcgis desktop 10.1 license manager无法启动问题解决
19:44:36 (ARCGIS) Vendor daemon can't talk to lmgrd (License server machine is down or not respondin ...
-
c#窗体虚线图形验证码设计
/************************窗体验证码背景图形设计及核心代码**********/ using System;using System.Collections.Generic;u ...
-
按键精灵 vbs 获取网页源码 xp系统被拒绝
如下面的代码所示,获取新浪博客某个指定网页的源码 verurl = "http://blog.sina.com.cn/s/blog_9ea1db7b0101o7ch.html?" ...
-
jS弹出新窗口被拦截的解决方法
使用ajax处理数据,在回调中跳转到或打开新页面,这时就会被浏览器拦截 解决方案:先用window.open打开一个窗口,然后修改该窗口地址 var w = window.open('about:bl ...
-
pymysql模块使用
一.写函数的原因 写这个函数的原因就是为了能够不每次在用Python用数据库的时候还要在写一遍 做个通用函数做保留,也给大家做个小小的分享,函数不是最好的,希望有更好的代码的朋友能提出 互相学习 二 ...
-
通过R语言统计考研英语(二)单词出现频率
通过R语言统计考研英语(二)单词出现频率 大家对英语考试并不陌生,首先是背单词,就是所谓的高频词汇.厚厚的一本单词,真的看的头大.最近结合自己刚学的R语言,为年底的考研做准备,想统计一下最近考研英语( ...
-
SQL中以count或sum为条件的查询方式
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
-
关于Cocos2d-x中增加暂停按钮的步骤
1.在GameScene.cpp的init方法中先定义一个里面放着可变换并在变换的时候会响应事件的MenuItem的Menu,这个Menu里面的可变换MenuItem又由两个小MenuItem组成,每 ...
-
contOS下安装Tomcat
安装tomcat之前要安装Java环境 也就是JDK 配置Java环境变量 我的jdk安装在 /usr/local/ 目录下 命令 vi /etc/profile 编辑这个文件添加环境 ...
-
python基础之数据类型之元组和字典
四.元组 1.用途:元组是不可变的列表,能存多个值,但只能取,不能改 2.定义:name = (‘alex’, ’egon’, ‘wxx’) 在()内用,分割开,可存放任意类型的值 强调:x = (‘ ...