SSH整合简单实例
1、配置struts.xml文件:
<?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>
<constant name="struts.action.extension" value="action" />
<package name="s2sh" namespace="/user" extends="struts-default">
<action name="user_*" method="{1}" class="com.java1234.action.UserAction">
<result name="success">/success.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
2、配置hibernate.cfg.xml文件:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--方言-->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 自动更新 -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
3、配置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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@132.224.24.118:1521:jsfx"/>
<property name="username" value="sett_analyse"/>
<property name="password" value="g4el_yj_ha"/>
</bean>
<!-- session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<!-- 自动扫描注解方式配置的hibernate类文件 这样的话就不需要写hbm映射文件-->
<property name="packagesToScan">
<list>
<value>com.java1234.entity</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.java1234.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
<!-- 自动加载构建bean -->
<context:component-scan base-package="com.java1234" />
</beans>
4、User.java
package com.java1234.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="t_user")
public class User {
private Integer id;
private String userName;
private String password;
@Id
@GenericGenerator(name = "generator", strategy = "native")
@GeneratedValue(generator = "generator")
@Column(name = "id", length=11)
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "userName", length = 20)
public String getUserName() { return userName; }
public void setUserName(String userName) {
this.userName = userName; }
@Column(name = "password", length = 20)
public String getPassword() {
return password; }
public void setPassword(String password) {
this.password = password;
} }
5、UserAction.java
package com.java1234.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.stereotype.Controller;
import com.java1234.entity.User;
import com.java1234.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
@Controller public class UserAction extends ActionSupport implements ServletRequestAware{
/** * */ private static final long serialVersionUID = 1L;
private HttpServletRequest request;
@Resource private UserService userService;
private User user; private String error;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user; }
public String getError() {
return error; }
public void setError(String error) {
this.error = error; }
public String login()throws Exception{
HttpSession session=request.getSession();
User currentUser=userService.findUserByNameAndPassword(user);
if(currentUser!=null){
session.setAttribute("currentUser", currentUser);
return SUCCESS;
}else{
error="用后名或者密码错误!"; return ERROR;
} }
@Override public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub this.request=request; }
}
6、UserServiceImpl.java
package com.java1234.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.java1234.dao.BaseDao;
import com.java1234.entity.User;
import com.java1234.service.UserService;
@Service("userService") public class UserServiceImpl implements UserService{
@Resource private BaseDao<User> baseDao;
@Override public void saveUser(User user) {
// TODO Auto-generated method stub baseDao.save(user); }
@Override public void updateUser(User user) {
// TODO Auto-generated method stub
baseDao.update(user); }
@Override public User findUserById(int id) {
return baseDao.get(User.class, id); }
@Override public void deleteUser(User user) {
baseDao.delete(user); }
@Override public List<User> findAllList() {
return baseDao.find("from User"); }
@Override public User findUserByNameAndPassword(User user) {
return baseDao.get("from User u where u.userName=? and u.password=?",
new Object[]{user.getUserName(),user.getPassword()}); }
}
7、BaseDaOImpl.java
package com.java1234.dao.impl;
import java.io.Serializable; import java.util.List;
import org.hibernate.Query; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.java1234.dao.BaseDao;
@Repository("baseDao") @SuppressWarnings("all") public class BaseDaOImpl<T> implements BaseDao<T> {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory; }
@Autowired public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory; }
private Session getCurrentSession() {
return sessionFactory.getCurrentSession(); }
public Serializable save(T o) {
return this.getCurrentSession().save(o); }
public void delete(T o) {
this.getCurrentSession().delete(o); }
public void update(T o) {
this.getCurrentSession().update(o); }
public void saveOrUpdate(T o) {
this.getCurrentSession().saveOrUpdate(o); }
public List<T> find(String hql) {
return this.getCurrentSession().createQuery(hql).list(); }
public List<T> find(String hql, Object[] param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
q.setParameter(i, param[i]);
} }
return q.list(); }
public List<T> find(String hql, List<Object> param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
} }
return q.list(); }
public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
if (page == null || page < 1) { page = 1; }
if (rows == null || rows < 1) {
rows = 10; }
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
q.setParameter(i, param[i]); } }
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); }
public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
if (page == null || page < 1) { page = 1; }
if (rows == null || rows < 1) { rows = 10; }
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
} }
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
public T get(Class<T> c, Serializable id) {
return (T) this.getCurrentSession().get(c, id); }
public T get(String hql, Object[] param) {
List<T> l = this.find(hql, param);
if (l != null && l.size() > 0) {
return l.get(0);
} else { return null;
} }
public T get(String hql, List<Object> param) {
List<T> l = this.find(hql, param);
if (l != null && l.size() > 0) {
return l.get(0); }
else {
return null; } }
public Long count(String hql) {
return (Long) this.getCurrentSession().createQuery(hql).uniqueResult(); }
public Long count(String hql, Object[] param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
q.setParameter(i, param[i]); } } return (Long) q.uniqueResult(); }
public Long count(String hql, List<Object> param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
} }
return (Long) q.uniqueResult(); }
public Integer executeHql(String hql) {
return this.getCurrentSession().createQuery(hql).executeUpdate(); }
public Integer executeHql(String hql, Object[] param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
q.setParameter(i, param[i]);
} }
return q.executeUpdate(); }
public Integer executeHql(String hql, List<Object> param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
} }
return q.executeUpdate(); }
}
8、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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>S2SH</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 添加对spring的支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 定义Spring监听器,加载Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 添加对struts2的支持 -->
<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>
<!-- Session延迟加载到页面 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
SSH整合简单实例的更多相关文章
- SSH整合简单例子
说明:简单SSH整合,struts版本2.3.32,spring版本3.2.9,hibernate版本3.6.10 一.开发步骤 1 引jar包,创建用户library.使用的包和之前博文相同,可以参 ...
- [转]SSH整合 简单的增删改查
Database 使用 Mysql , 建立User 表 CREATE TABLE `user` ( `id` int(11) NOT NULL, `firstName` varchar(20) ...
- 【转载】Ssh整合开发介绍和简单的登入案例实现
Ssh整合开发介绍和简单的登入案例实现 Ssh整合开发介绍和简单的登入案例实现 一 介绍: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开 ...
- ssh远程登录命令简单实例
ssh远程登录命令简单实例 ssh命令用于远程登录上Linux主机. 常用格式:ssh [-l login_name] [-p port] [user@]hostname 更详细的可以用ssh -h查 ...
- 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)
Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...
- SSH整合框架+mysql简单的实现
SSH整合框架+mysql简单的实现 1. 框架整合原理: struts2整合Spring 两种: 一种struts2自己创建Action,自动装配Service : 一种 将Action交给Spri ...
- 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】
一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...
- mysql+ssh整合样例,附源代码下载
项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/ ...
- 从MVC和三层架构说到SSH整合开发
相信很多人都认同JavaWeb开发是遵从MVC开发模式的,遵从三层架构进行开发的,是的,大家都这么认同.但是相信大家都会有过这样一个疑问,if(MVC三层模式==三层架构思想)out.println( ...
随机推荐
- loadrunner资源过滤器
通过该功能可以实现排除某个资源,很实用 Download Filters功能 帮助在回放脚本的时候对某些特定的访问进行屏蔽,解决页面读取中跨服务器带来数据影响的问题. 过滤规则中有3中策略,即URL. ...
- hdu 1370 Biorthythms 中国剩余定理
Biorhythms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Android利用Fiddler进行网络数据抓包
最新最准确内容建议直接访问原文:Android利用Fiddler进行网络数据抓包 主要介绍Android及IPhone手机上如何进行网络数据抓包,比如我们想抓某个应用(微博.微信.墨迹天气)的网络通信 ...
- SpringMVC解析4-DispatcherServlet逻辑脉络
HttpServlet提供了不同的服务方法,它们是doDelete(),doGet(),doOptions(),doPost(),doPut(),和doTrace(),它会根据不同的请求形式将程序引导 ...
- AngularJS学习之表单
1.HTML控件:以下HTML input元素被称为HTML控件: **input元素 **select元素 **button元素 **textarea元素 2.AngularJS表单实例: < ...
- Python与Hack
1.Python的函数:关键字def()表示函数开始,可以在括号内填写任何变量,然后这些变量会被以引用的方式传递给函数,也就是说,函数内对这些变量的任何改变都会影响它们在主调函数中的值: 2.迭代:用 ...
- 归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...
- 网站迁移时候,发现<head>内容都到body里了
遇到的问题截图如下: 这个是编码问题,需要把所有涉及的文件保存成UTF-8 without BOM,手动的话可以用notepad++ 如果网站支持php,这边提供了一个php的脚本(clearBom. ...
- The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547
Sudoku Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- SOAPUI测试步骤之流量控制(Conditional Goto)
1. TestSteps流量控制 1.1.有条件转到一步步测试 Conditional Goto拥有任意数量的的XPath表达式伴随相应的目标测试步骤.这些被应用到在先前的采样测试的最近的响应; 配置 ...