SSH架构
Struts 2 + Spring + Hibernate
以Spring作为核心框架,数据持久化使用Hibernate完成,表现层使用Struts 2
Spring提供对象管理、面向切面编程等实用功能
通过Spring提供的服务简化编码、降低开发难度、提高开发效率
1.导入jar包
SSH整合体验最不好的就是添加jar包,由于三个框架所依赖的jar包非常多,其中有一些jar包可能冲突,我们应该将冲突的jar包,保留高级版本的,删掉低级版本的
2.配置xml文件
struts2.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>
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<!-- <constant name="struts.devMode" value="true" /> -->
<package name="default" namespace="/" extends="struts-default">
<action name="login">
<result>/WEB-INF/Login.jsp</result>
</action>
<action name="index" class="as">
<result name="success">/WEB-INF/write.jsp</result>
<result name="error">/WEB-INF/Login.jsp</result>
</action>
<action name="besc" class="uu">
<result name="success">/WEB-INF/write.jsp</result>
<result name="error">/WEB-INF/Login.jsp</result>
</action>
</package>
</struts>
hibernate.cfg.xml
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 在控制台输出sql语句 -->
<property name="show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 数据库生成策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 需要在hibernate.cfg.xml中添加配置信息 -->
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<!-- c3p0连接池配置 -->
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">10</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">2</property>
<!-- 当连接不足时,向数据库请求的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 缓存的最大statements对象的数目 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 连接的过期时间,单位毫秒 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 多长时间检测连接池内对象是否超时,单位秒 -->
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- 是否每次都验证连接是否有效,默认为false -->
<property name="hibernate.c3p0.validate">true</property>
</session-factory>
</hibernate-configuration>
spring.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:p="http://www.springframework.org/schema/p"
default-autowire="byType"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="userez" class="com.znsd.ssh.entities.UserEz"></bean>
<bean id="as" class="com.znsd.ssh.action.IndexAction"></bean>
<bean id="userser" class="com.znsd.ssh.service.UserService"></bean>
<bean id="userdao" class="com.znsd.ssh.dao.UserDao"></bean>
<bean id="be" class="com.znsd.ssh.dao.Beandaoimp"></bean>
<bean id="dao" class="com.znsd.ssh.service.Beanservice"></bean>
<bean id="uu" class="com.znsd.ssh.action.BeanAction"></bean>
</beans>
springDao.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置sessionfactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 加载数据源对象,spring的配置方式 -->
<!-- <property name="dataSource" ref="dataSource" /> -->
<!-- 读取hibernate配置信息,hibernate配置方式 -->
<property name="configLocation" value="classpath:config/hibernate.cfg.xml" />
<!-- 自动加载映射文件 *表示匹配该文件下的所有映射文件 -->
<property name="mappingLocations" value="classpath:com/znsd/ssh/entities/*.hbm.xml" />
</bean>
<!-- 配置事务 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务的通知方式 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- AOP切面拦截事务,事务一定是加在业务层 -->
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* com.znsd.ssh.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="serviceMethod" />
</aop:config>
</beans>
3.创建实体类以及给实体类配置hbm.xml
public class UserEz {
private Integer id;
private String age;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "UserEz [id=" + id + ", age=" + age + ", sex=" + sex + "]";
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.znsd.ssh.entities.UserEz" table="tb_user" >
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="age" column="age"/>
<property name="sex" column="sex"/>
</class>
</hibernate-mapping>
4.在数据访问层创建BeanDao类以及接口
package com.znsd.dao;
import java.io.Serializable;
import java.util.List;
public interface BeanDao<T> {
//实现添加的方法
Serializable save(T entity);
//实现删除的方法
void delete(T entity);
//根据id来删除的方法
void delete(Class<T> clazz,Serializable id);
//实现更新的方法
void update(T entity);
//根据id来获取对象
T get(Class<T> clazz, Serializable id);
//实现查询所有数据的方法
List<T> findAll(Class<T> clazz);
//实现根据条件查询的方法
List<T> findAll(Class<T> clazz,String where,Object[] paras);
}
package com.znsd.dao;
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
public class BeanDaoimp<T> extends HibernateDaoSupport implements BeanDao<T> {
@Override
public Serializable save(T entity) {
return getHibernateTemplate().save(entity);
}
@Override
public void delete(T entity) {
getHibernateTemplate().delete(entity);
}
@Override
public void delete(Class<T> clazz, Serializable id) {
T t = (T) getHibernateTemplate().get(clazz, id);
getHibernateTemplate().delete(t);
}
@Override
public void update(T entity) {
getHibernateTemplate().update(entity);
}
@Override
public T get(Class<T> clazz, Serializable id) {
return getHibernateTemplate().get(clazz, id);
}
@Override
public List<T> findAll(Class<T> clazz) {
String hql = "from " + clazz.getName();
return (List<T>) this.getHibernateTemplate().find(hql, null);
}
@Override
public List<T> findAll(Class<T> clazz, String where, Object[] paras) {
String hql = "from " + clazz.getName() + " where " + where;
return (List<T>) this.getHibernateTemplate().find(hql, paras);
}
}
5.在业务逻辑层创建类以及接口
package com.znsd.service.imp;
import java.util.List;
import com.znsd.cote.entites.Vote_item;
import com.znsd.cote.entites.Vote_subject;
import com.znsd.dao.BeanDao;
import com.znsd.dao.Hibernate;
public interface LoginServiceImp extends BeanDao<Vote_subject> {
int add_subject(String subjec_ttitle, Integer subject_type, List<String> options);
List<Vote_subject> vote_option(String name);
List<Vote_subject> vote_options(int PagesIndex, int PagesSize, String name);
int update_userid(Integer userid) ;
int update_subject(String subjec_ttitle, Integer userid);
}
package com.znsd.service;
import java.util.List;
import com.znsd.cote.entites.Vote_subject;
import com.znsd.dao.BeanDaoimp;
import com.znsd.dao.HibernateImp;
import com.znsd.service.imp.LoginServiceImp;
public class LoginService extends BeanDaoimp<Vote_subject> implements LoginServiceImp {
private HibernateImp hiber;
public HibernateImp getHiber() {
return hiber;
}
public void setHiber(HibernateImp hiber) {
this.hiber = hiber;
}
public int add_subject(String subjec_ttitle, Integer subject_type, List<String> options) {//增加投票内容
Vote_subject v=new Vote_subject();
v.setVs_title(subjec_ttitle);
v.setVs_type(subject_type);
v.setVs_fux(subject_type);
v.setVs_type(options.size());
v.setVs_control(0);
return hiber.add_subject(v,options) ;
}
public List<Vote_subject> vote_option(String name) {查询所有的投票
return hiber.vote_option(name) ;
}
public List<Vote_subject> vote_options(int PagesIndex, int PagesSize, String name) {
return hiber.vote_options(PagesIndex,PagesSize,name) ;
}
public int update_userid(Integer userid) {
return hiber.update_userid(userid) ;
}
public int update_subject(String subjec_ttitle, Integer userid) {
return hiber.update_subject(subjec_ttitle,userid) ;
}
}