SSH整合笔记
SSH:spring+struts+hibernate.
一:所需jar:
需要注意的是:
hibernate+spring需要Spring-orm-xxx.jar
struts+spring需要struts2-spring-plugin-2.3.24.jar
二:三个框架各自用处
spring:对各层的类进行初始化
struts:接收页面过来的请求,然后去后台找对应的业务类
hibernate:对数据进行操作管理
三:配置文件
在spring配置文件中需要配置
<!-- SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- dao层 --> <bean id="dao" class="cn.ssh.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- Service层 --> <bean id="service" class="cn.ssh.service.UserService"> <property name="dao" ref="dao"></property> </bean> <!-- action层 --> <bean id="useraction" class="cn.ssh.action.UserAction" scope="prototype"> <property name="service" ref="service"></property> </bean>
其中不仅需要给dao,service,action层配置,还需要将sessionFactory配置在这里,为了保证一个项目里只有一个sessionFactory,也不用程序员用代码创建sessionFactory这样繁琐.
当然,在dao实现类里必须得植入一个sessionFactory局部变量(get/set).
在struts配置文件中需要配置
<package name="default" namespace="/" extends="struts-default"> <action name="addUser" class="useraction" > <result name="success">/index.jsp</result> <result name="input">/MyJsp.jsp</result> </action> </package>
struts配置文件中没有需要添加的节点
在hibernate配置文件中需要配置
<hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">wuao</property> <property name="connection.password">123</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <property name="hibernate.connection.autocommit">true</property> <property name="format_sql">true</property> <mapping class="cn.ssh.entity.User"/> </session-factory> </hibernate-configuration>
hibernate配置文件中只加了一个自动提交的事物节点:<property name="hibernate.connection.autocommit">true</property>
四:各层调用
dao层调用entity
service层调用dao
action层调用service
dao层
service层
action层
五:ssh简单流程图