框架搭建,逆向生成bean类dao类,配置文件(第三天)
第三天;
框架搭建
1.打开myeclispe
File=>New=>Web Project

2.输入项目名 myTest并点击Finish

3.添加Spring支持
右键项目名
MyEclispe=>Add Spring Capabilities...

如图所示我们要选中Spring2.5下面的5项
1.Spring2.5 AOP Libraries
2.Spring2.5 Core Libraries
3.Spring2.5 Persistence Core Libraries
4.Spring2.5 Persistence JDBC Libraries
5.Spring2.5 Web Libraries
然后 Next==》Finish

2.添加Hibernate支持
右键项目名
MyEclispe=>Add Hibernate Capabilities...


点击Next

继续点击Next,选中oracle


点击Finish
3.添加Struts2支持
右键项目名
MyEclispe=>Add Struts Capabilities...

如图所示,选中Struts2.1,然后点击Next

选中两项,点击Finish

至此S2SH框架搭建完毕

上面红框中的都可以去掉,换成jar包,储存在
WebRoot=》WEB-INF=》lib文件夹里面
我把这个项目涉及到的所有jar,js,css,图片储存在百度云盘里面,可供下载
同时我建立了四个包,导入了EasyUi所需js,css和图片,使架构更清晰

hibernate逆向工程
1.打开DB Browser视图

2.双击oracle,然后双击Connected to oracle,然后双击MYDB,按住Ctrl选中我们的四张表ALLCLASS,ERRORSUBJECT,SUBJECT,USERINFO,然后右键选择Hibernate Reverse Engineering

3.如图,将各个数据补齐,然后点击Next

4.id Generator输入sequence,直接点击Finish

5.如图,在com.jianshu.bean逆向生成了8个java类,4个.hbm.xml文件 ,我们把含有DAO的4个java类移动到com.jianshu.dao*

6.com.jianshu.bean里面有4个java类,4个hibernate配置文件,每个配置文件对应一个java类(也叫实体类),在这里不建议修改

7.在这里我自己动手生成对应的Action,Service

8.下一步由Spring接管这些java类,我们打开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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:orcl">
</property>
<property name="username" value="mydb"></property>
<property name="password" value="mydb"></property>
</bean>
<!-- 缓冲了HIbernate自动生成SQL语句和其他的映射数据 还缓冲了一些将来有可能重复利用的数据 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<!-- 在bean包里面都有对应的配置文件 -->
<value>com/jianshu/bean/Userinfo.hbm.xml</value>
<value>com/jianshu/bean/Errorsubject.hbm.xml</value>
<value>com/jianshu/bean/Subject.hbm.xml</value>
<value>com/jianshu/bean/Allclass.hbm.xml</value></list>
</property>
</bean>
<!-- Hibernate事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务拦截 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 配置aop,配置对哪些类的方法进行事务管理,当前com.myhopu.service包中的子包、类中所有方法需要 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jianshu.service.*.*(..))" />
</aop:config>
<!-- dao层 在这里id的首字母要小写 -->
<bean id="UserinfoDAO" class="com.jianshu.dao.UserinfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="ErrorsubjectDAO"
class="com.jianshu.dao.ErrorsubjectDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="SubjectDAO" class="com.jianshu.dao.SubjectDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="AllclassDAO" class="com.jianshu.dao.AllclassDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- service层 scope="prototype"在这里指单实例-->
<bean id="allclassService" class="com.jianshu.service.AllclassService" scope="prototype"/>
<bean id="subjectService" class="com.jianshu.service.SubjectService" scope="prototype"/>
<bean id="userinfoService" class="com.jianshu.service.UserinfoService" scope="prototype"/>
<bean id="errorsubjectService" class="com.jianshu.service.ErrorsubjectService" scope="prototype"/>
<!-- action层 -->
<bean id="allclassAction" class="com.jianshu.action.AllclassAction" scope="prototype"/>
<bean id="subjectAction" class="com.jianshu.action.SubjectAction" scope="prototype"/>
<bean id="userinfoAction" class="com.jianshu.action.UserinfoAction" scope="prototype"/>
<bean id="errorsubjectAction" class="com.jianshu.action.ErrorsubjectAction" scope="prototype"/>
</beans>
9.如图,是不是发现Spring中对应的java文件图标右上角出现个“S”,这就证明配置生效

同时在上图我们看见了三个xml后缀的文件
分别是
1.web.xml ---------------------------用来初始化工程配置信息,启动加载级别,写过滤器或监听器等配置文件
2.struts.xml---------------------------判断要调用哪个Action去处理用户请求
3.applicationContext.xml---------ApplicationContext的中文意思是“应用前后关系”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等
在这之中
applicationContext.xml已经配置好了
让我们看看web.xml把
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<!-- 指定了首页,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推。 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 用来定位applicationContext.xml文件的上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<filter>
<!-- Hibernate 允许对关联对象、属性进行延迟加载,
但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。
如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层,
当 Web 层访问到那些需要延迟加载的数据时,
由于加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常
而Spring为我们提供的OpenSessionInViewFilter过滤器为我们很好的解决了这个问题。
OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。
目的是为了实现"Open Session in View"的模式 -->
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!-- struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<!-- 启用延迟加载(openSessionInViewFilter) -->
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 启用struts2过滤器 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
接着我们打开strus.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>
<package name="default" namespace="/answer" extends="struts-default">
</package>
</struts>
第三天总结
1.搭建S2SH框架
2.逆向生成Dao,bean
3.对三个配置文件要理解
第四天:登录