使用IDEA搭建SSH基本架构,创建好数据库(orcal)和项目后,开始
目录
3.第三部,创建好三个框架的核心文件(本篇使用spring管理数据源,hibernate.cfg.xml可有可无)
4.第四布,编写DAO层,我写的是继承BaseDao抽象泛型,实现接口的模式
1.天才第一步,先加好相关jar文件
2.第二布,配置web.xml
web.xml
<!--声明上下文初始参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对spring框架中的ContextLoaderListener类进行监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring 管理Session -->
<filter>
<filter-name>OpenSessionInviewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInviewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--将全部请求定位到struts2过滤器 -->
<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>
3.第三部,创建好三个框架的核心文件(本篇使用spring管理数据源,hibernate.cfg.xml可有可无)
applicationContext.xml
dao:
//配置dao
<!-- 定义数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 指定JDBC驱动类 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="jboa" />
<property name="password" value="123" />
<property name="initialSize" value="20"/>
</bean>
<!-- 定义SessionFactory Bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!--添加hibernate配置参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:landun/pojo</value>
</list>
</property>
<!--加载由注解定义的持久化类 -->
<!-- <property name="packagesToScan" value="landun.pojo" />-->
</bean>
transaction :
//配置tx
//http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
//http://www.springframework.org/schema/aop/spring-aop.xsd
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="register" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod"
expression ="execution( * landun.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
<!--事务注解驱动-->
<tx:annotation-driven transaction-manager="txManager"/>
最后将它两都导入进applicationContext.xml
//http://www.springframework.org/schema/context/spring-context-4.2.xsd
<!-- 扫描注解 -->
<context:component-scan base-package="landun"/>
<import resource="applicationContext-*.xml" />
4.第四布,编写DAO层,我写的是继承BaseDao抽象泛型,实现接口的模式
BaseDao:
直接继承HibernateDaoSupport类,并创建一个方法用来自动注入sessionFactory
编写dao实现类继承Basedao,并使用注解创建bean(可以不使用注解,使用配置文件单个创建,个人喜欢注解,简便不是一点半点~)
5.第五步,创建业务层编写action然后实现和页面的交互
service:
action:
一个基本的ssh架构就搭建完了,看完点个赞~qaq