对于ssh简单的整合配置(项目下载地址:http://download.csdn.net/detail/flymoringbird/9604356):
hibernate :
1.建立bean:(1)提供get set 方法 (2)提供构造方法:空构造 非空构造 【如:User】
2.建立bean 的映射关系:
(1)注解的形式
//当前的类是一个持久化类,是User这个类。他映射了一个表User。所对应的 数据库是users
@Entity
@Table(name = "User", catalog = "users")
1.建立bean:(1)提供get set 方法 (2)提供构造方法:空构造 非空构造 【如:User】
2.建立bean 的映射关系:
(1)注解的形式
//当前的类是一个持久化类,是User这个类。他映射了一个表User。所对应的 数据库是users
@Entity
@Table(name = "User", catalog = "users")
// 主键 :@Id 主键生成方式:strategy = "increment"
//映射表中id这个字段,不能为空,并且是唯一的
@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
//映射表中id这个字段,不能为空,并且是唯一的
@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
(2)User.hbm.xml
<class name="User" table="users" catalog="bbs">
<id name="id" type="java.lang.Integer">
<column name="userId" />
<generator class="native" />
</id>
<class name="User" table="users" catalog="bbs">
<id name="id" type="java.lang.Integer">
<column name="userId" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" not-null="true">
<comment>用户账户</comment><!-- 这个是数据库注释作用 -->
</column>
</property>
<column name="username" length="20" not-null="true">
<comment>用户账户</comment><!-- 这个是数据库注释作用 -->
</column>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" not-null="true">
<comment>账户密码</comment>
</column>
</property>
</class>
(3)hibernate.cfg.xml配置
<session-factory>
<!-- 数据库配置-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/bbs
</property>
<property name="connection.username">root</property>
<property name="connection.password">zkpk</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 显示查询sql语句 和 是否由bean生成数据库表的配置 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">none</property>
<property name="current_session_context_class">thread</property>
<property name="connection.release_mode">auto</property>
<property name="transaction.auto_close_session">true</property>
<column name="password" length="20" not-null="true">
<comment>账户密码</comment>
</column>
</property>
</class>
(3)hibernate.cfg.xml配置
<session-factory>
<!-- 数据库配置-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/bbs
</property>
<property name="connection.username">root</property>
<property name="connection.password">zkpk</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 显示查询sql语句 和 是否由bean生成数据库表的配置 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">none</property>
<property name="current_session_context_class">thread</property>
<property name="connection.release_mode">auto</property>
<property name="transaction.auto_close_session">true</property>
<!-- <mappingclass="com.oracle.strutsdemo.model.javabeans.User" /> 可采用注解的形式 -->
<mapping resource="com/oracle/struts/model/pojos/User.hbm.xml" /><!--加载映射文件-->
</session-factory>
<mapping resource="com/oracle/struts/model/pojos/User.hbm.xml" /><!--加载映射文件-->
</session-factory>
Spring:
1.建立dao 【如: UserDao】
(1)
/*通过注解的方式生命事务处理: 增, 删 ,改 一般需要提交事务*/
@Transactional(propagation=Propagation.REQUIRED)
public User login(User user);
@Transactional(propagation=Propagation.REQUIRED)
public int register(User user);
@Transactional(propagation=Propagation.REQUIRED)
public int update(User user);
(2)
在 spring的配置文件【 applicationContext.xml】中
<!-- 声明spring的AOP事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
2.在每个相互依赖的类中 通过注解声明:
(1)
@Component(value="userAction")//Spring的注解声明:实现控制反转(自动装配)
@Component(value="userDAO")
@Component(value="userService")
【如: UserDAO userDAO; 在userService中声明后 ,提供get set方法 直接使用,无需实例化
@Component(value="userService")
public class UserService {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public User processLogin(User user){
public User processLogin(User user){
// user.setPassword(MD5.MD5(user.getPassword()));
return userDAO.login(user);
}
.....
}
】
AOP:dao-->service--->action(每一个都需要配置@Component) 依赖关系
(2)
在 spring的配置文件【 applicationContext.xml】中
1. 在applicationContext.xml中beans中声明:default-autowire="byName"
2.<!--扫描哪些包里面的组件要被spring工厂自动装配和注入依赖组件 -->
<context:component-scan base-package="com.oracle.struts.model.dao"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.service"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.model.actions"></context:component-scan>
3.hibernate--->Spring整合
<!-- 声明hibernate的sessionFactory交给spring托管 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
4.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">
<!-- 声明spring的AOP事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 声明哪些包里面的组件要被spring工厂自动装配和注入依赖组件 -->
<context:component-scan base-package="com.oracle.struts.model.dao"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.service"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.model.actions"></context:component-scan>
<!-- 声明hibernate的sessionFactory交给spring托管 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 声明spring的事务管理器bean组件 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
1.建立dao 【如: UserDao】
(1)
/*通过注解的方式生命事务处理: 增, 删 ,改 一般需要提交事务*/
@Transactional(propagation=Propagation.REQUIRED)
public User login(User user);
@Transactional(propagation=Propagation.REQUIRED)
public int register(User user);
@Transactional(propagation=Propagation.REQUIRED)
public int update(User user);
(2)
在 spring的配置文件【 applicationContext.xml】中
<!-- 声明spring的AOP事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
2.在每个相互依赖的类中 通过注解声明:
(1)
@Component(value="userAction")//Spring的注解声明:实现控制反转(自动装配)
@Component(value="userDAO")
@Component(value="userService")
【如: UserDAO userDAO; 在userService中声明后 ,提供get set方法 直接使用,无需实例化
@Component(value="userService")
public class UserService {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public User processLogin(User user){
public User processLogin(User user){
// user.setPassword(MD5.MD5(user.getPassword()));
return userDAO.login(user);
}
.....
}
】
AOP:dao-->service--->action(每一个都需要配置@Component) 依赖关系
(2)
在 spring的配置文件【 applicationContext.xml】中
1. 在applicationContext.xml中beans中声明:default-autowire="byName"
2.<!--扫描哪些包里面的组件要被spring工厂自动装配和注入依赖组件 -->
<context:component-scan base-package="com.oracle.struts.model.dao"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.service"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.model.actions"></context:component-scan>
3.hibernate--->Spring整合
<!-- 声明hibernate的sessionFactory交给spring托管 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
4.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">
<!-- 声明spring的AOP事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 声明哪些包里面的组件要被spring工厂自动装配和注入依赖组件 -->
<context:component-scan base-package="com.oracle.struts.model.dao"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.service"></context:component-scan>
<context:component-scan base-package="com.oracle.struts.model.actions"></context:component-scan>
<!-- 声明hibernate的sessionFactory交给spring托管 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 声明spring的事务管理器bean组件 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
Struts:
在struts中主要是针对Action的配置:
实现跳转配置
1.通过Struts.xml配置
<!--
<package name="user" namespace="/" extends="struts-default">
<action name="UserAction" class="com.oracle.strutshibernate.model.actions.UserAction">
<result name="loginSuccess">/index.jsp</result>
<result name="loginFails">/login.jsp</result>
<result name="registerSucess">/login.jsp</result>
</action>
</package>
-->
2.通过注解的方法
@ParentPackage(value="struts-default")//struts注解
@Namespace(value="/")
@Action(value="UserAction")
@Results(
{
@Result(location="/index.jsp"),
@Result(name="fail",location="/login.jsp"),
@Result(name="registerFail",location="/register.jsp"),
@Result(name="registerSucess",location="/index.jsp"),
@Result(name="updateFail",location="/update.jsp"),
@Result(name="updateSucess",location="/index.jsp")
}
)
3.action类 的设置
(1).声明javabean对象
(2).声明调用的service方类
(3).提供以上的get set方法
(4).在jsp页面的表单数据name=“username”对应bean中所定义的属性值:直接获取使用即可!
(5).action类的继承和实现:public class UserAction extends ActionSupport implements SessionAware
(6).其中所用的方法定义样式:
public String login() {
User loginedUser = userService.processLogin(user);
session.put("loginedUser", loginedUser);
if (loginedUser!=null)
return SUCCESS;
else
return "fail";
}
返回值:String 类型:通过注解或者strut.xml的action配置实现跳转
4.struts--->Spring整合
<!-- 具体的调用和跳转已注解 -->
<!-- 将Struts bean的交给spring 管理-->
<constant name="struts.objectFactory" value="spring"></constant>
5.struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- constant 可以覆盖掉 在 struts.properties文件里的配置 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.devMode" value="false"></constant><!-- 配置开发模式启用 -->
<constant name="struts.configuration.xml.reload" value="true" /> <!-- 配置自动加载配置文件 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><!-- 启用Action动态方法调用 -->
<!-- 具体的调用和跳转已注解 -->
<!-- 将Struts bean的交给spring 管理-->
<constant name="struts.objectFactory" value="spring"></constant>
</struts>
<!-- 将Struts bean的交给spring 管理-->
<constant name="struts.objectFactory" value="spring"></constant>
</struts>