JavaSSH框架整合(struts2,hibernate4,spring4)

ssh整合思路    
    
    两两整合:struts2整合spring   spring整合hibernate
    
    前提添加web工程文件
    1)导入jar包(jar包下载自行度娘哦)
    2)在src目录下添加struts2和spring的配置文件并引入约束(注意是在src目录下)

<!-- spring约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
		

 

<!--struts2 -->
<?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">


    3)配置web.xml文件中struts2的过滤器(核心控制器)以及spring的相关配置

<!--web.xml -->
<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>
  <!-- 指定spring配置文件位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!--  延迟加载的问题的解决:OpenSessionInViewFilter  -->
  <filter> 
  	<filter-name>OpenSessionInViewFilter</filter-name>
	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class> 
  </filter>
  
  <filter-mapping> 
  	<filter-name>OpenSessionInViewFilter</filter-name> 
  	<url-pattern>/*</url-pattern> 
  </filter-mapping>

 

<!--spring -->
<bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name = "driverClass"> 
				<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name = "jdbcUrl">
				<value>jdbc:mysql:///数据库名</value>
		</property>
		<property name = "user">
				<value>用户名</value>
		</property>
		<property name="password">
				<value>登录密码</value>
		</property>
	</bean>
	
	<!-- 配置Hibernate中的sessionFactory -->
	<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">//注意版本号
		<!-- 注入连接池 -->
		<property name="dataSource" ref= "dataSource"/>
			
		
		
		<!-- 配置Hibernate的相关属性 -->
		<property name = "hibernateProperties">
			<props>
				<!-- 配置Hibernate的方言 -->
				<prop key = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 显示SQL -->
				<prop key = "hibernate.show_sql">true</prop>
				<!-- 格式化SQL 
				<prop key = "hibernate.format_sql">false</prop>
				-->
				
				<!-- 映射到DDL的自动创建 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
 		<!-- 配置引入映射文件 -->
		<property name = "mappingResources">
			<list>
				<value>com/dc/beans/studentbean/Student.hbm.xml</value>//映射文件路径
			</list>
		</property>
	</bean>


    4)建包:
        action包
        业务逻辑层中的service接口以及业务逻辑实现类
        数据持久层的dao接口及实现类
        数据库表的实体类的包bean

                 
        实体类映射文件 注意 实体类对象的属性跟映射文件中的name值对应  id值的类型

private int id;
	private String name;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
<hibernate-mapping package="com.dc.beans.studentbean">
    <!-- 1 配置类和表对应   
        class标签  
        name属性:实体类全路径  
        table属性:数据库表名称  
    -->
    <class name="com.dc.beans.studentbean.Student" table="tb_student">
        <!-- 2 配置“主键”的映射
            id标签
            name属性:实体类里面id属性名称
            column属性:生成的表字段名称
            type属性:该字段的数据类型
        -->
        <id name="id" column="id">
            <!-- 设置主键的增长方法
                increment(递增)
                identity (标识)
                sequence (序列)
                hilo (高低位)
                seqhilo(使用序列的高低位)
                native(本地)
            -->
            <generator class="increment"></generator>
        </id>
        <!-- 配置其他属性和表字段对应
            name属性:实体类属性名称
            column属性:生成表字段名称
            type属性:该字段的数据类型
        -->
        <property name="name" column="name"></property>
        <property name="password" column="password"></property>
        <!-- 
        	<property name="sex" column="sex" type="java.lang.String" length="20"></property>
         -->
    </class>
</hibernate-mapping>


    4)在spring中进行属性注入

<!-- 配置Action对象 -->
	<bean id = "studentAction" class = "com.dc.action.studentaction.StudentAction" scope = "prototype">
		<property name="studentService" ref="studentServiceImpl"/> 
	</bean>
	<!-- 配置Service对象 -->
	<bean id = "studentServiceImpl" class = "com.dc.service.studentservice.StudentServiceImpl">
		<property name = "studentDao" ref = "studentDaoImpl"/>
	</bean> 
	<!-- 配置dao实现类对象 -->
	<bean id = "studentDaoImpl" class = "com.dc.dao.studentdao.StudentDaoImpl">
		<!-- 注入SessionFactory对象
			由于UserDaoImpl继承了HibernateDaoSupport,直接注入SessionFactory对象,
			就能获得HibernateTemplate对象
		 -->
		<property name = "sessionFactory" ref = "sessionFactory"/>		
	</bean>
<!-- 配置事务管理器 -->
	<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name = "sessionFactory" ref = "sessionFactory"></property>
	</bean>
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>


    5)struts2配置文件中写好返回值以及package及其中的action   难点:占位符

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="Student" namespace="/Student" extends="struts-default">   
		<action name ="Student_*" class = "studentAction" method = "{1}">		
			<!-- 响应页面跳转	 -->	
			<result name = "login">/WEB-INF/jsp/Login.jsp</result>
		</action>
    </package>
    <!-- Add packages here -->
</struts>


    6)测试  如果能够在数据库中创建出在映射问价里给出的表 即空框架搭建完成

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值