三大框架SSH所用jar包及项目机构图:见附件图片
文件 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>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="crs" extends="struts-default">
<action name="login" class="loginAction" method="login">
<result name="success">/WEB-INF/pages/userManagement.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
</package>
</struts>
文件 (Spring) 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<!-- <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="annotationConfiguration" class="org.compass.annotations.config.CompassAnnotationsConfiguration"/>
-->
<context:component-scan base-package="com.crs"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.crs.model.User</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean name="loginAction" class="com.crs.action.LoginAction" scope="prototype"/>
</beans>
PAS系统添加事务管理:文件 (Spring) 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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-lazy-init="true">
<context:component-scan base-package="com.pas"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.pas.model.User</value>
<value>com.pas.model.PaperBasicInfo</value>
<value>com.pas.model.Question</value>
<value>com.pas.model.QuestionStyle</value>
<value>com.pas.model.StudentScore</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.show_sql=false
hibernate.format_sql=true
</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="allMethod" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<!-- AOP注入事务 -->
<aop:config>
<!-- <aop:pointcut id="all_bs_method" expression="execution(* com.pas.service.impl.*Impl.*(..))"/>
<aop:advisor advice-ref="allMethod" pointcut-ref="all_bs_method"/> -->
<aop:advisor advice-ref="allMethod" pointcut="execution(* *..service.impl.*Impl.*(..))"/>
</aop:config>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
</beans>
文件 hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="sessionFactory">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/crs</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<property name="hbm2ddl.auto">none</property>
<property name="current_session_context_class">thread</property>
<mapping class="com.crs.model.User"/>
</session-factory>
</hibernate-configuration>
文件 jdbc.properties
#Mysql 5.5
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crs
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#hibernate.dialect=com.capgemini.lamp.util.SQLServer2008Dialect
# Needed by Hibernate3 Maven Plugin defined in pom.xml
#hibernate.connection.username=${jdbc.username}
#hibernate.connection.password=${jdbc.password}
#hibernate.connection.url=${jdbc.url}
#hibernate.connection.driver_class=${jdbc.driverClassName}
然后在MVC的Dao中的用法:
package com.crs.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.crs.dao.UserDao;
import com.crs.model.User;
//TODO Add class/interface import here and remove this line.
/**
* TODO Add class/interface description here and remove this line.
*
* @author BILLYANG, create on 2012-4-24
* Revision History:
* TODO Revised by XXXX on 201x/xx/xx, modified xxx
*/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
/*@Autowired
private SessionFactory sessionFactory;*/
private HibernateTemplate hibernateTemplate;
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@Override
public User get(Long id) {
/*Session session = sessionFactory.openSession();
User result = (User) session.get(User.class, id);
session.close();
return result;*/
return hibernateTemplate.get(User.class, id);
/*Configuration config = new AnnotationConfiguration();
config.configure();
SessionFactory sessionFactory=config.buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
User result = (User) session.get(User.class, 5);
session.getTransaction().commit();
return result;*/
}
@SuppressWarnings("unchecked")
@Override
public List<User> getAll() {
List<User> userList = new ArrayList<User>();
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
List<User> result = hibernateTemplate.findByCriteria(criteria);
if (result != null && !result.isEmpty()) {
userList = result;
}
return userList;
}
}
使用Mysql数据库简单建表语句:
drop database if exists crs;
create database crs;
use crs;
create table user(
id bigint auto_increment primary key,
username varchar(50),
password varchar(50),
realname varchar(50)
);
insert into user(username,password,realname) values('admin','admin','administrator');
insert into user(username,password,realname) values('bill','bill','yangfan');
insert into user(username,password,realname) values('allen','allen','zhangdong');
insert into user(username,password,realname) values('john','john','qiaoshili');
insert into user(username,password,realname) values('girl','girl','nvhai');
--#insert into user(username,password,realname) values('bill','bill','杨帆');
以上为搭建SSH框架重要部分,希望能对以后有所帮助