所以散仙尽量会以入门者的心态,来阐述整个小项目环境的搭建以及部署开发的流程,尽量让大家通俗易懂,一目明了。
在这之前,先谈谈SSH的使用状况,SSH框架集成,仍是当前中小型企业开发的首选利器,虽然近几年,使用Spring MVC+MyBatics+Spring有所增长,但以整体的趋势来看,SSH仍是当前开发的经典主流,因为使用SSH开发,足可以应对90%的企业级网站开发,当然更大型的网站架构以及开发可能会倾向于EJB+JPA+JSF的模式开发,这种情况,已经非常罕见了,
大部分的时候,我们使用不上这种网站架构。
下面开始进入正题,先看来下项目的开发环境:
名称 | 描述 |
Web容器 | Tomcat7 |
IDE工具 | Myeclipse10.1 |
平台 | Windows |
语言 | JAVA |
JDK | 1.7 |
数据库 | MySQL5.1 |
Spring | 3.0,myeclipse自带 |
Struts | 2.1,myeclipse自带 |
Hibernate | 3.3,myeclipse自带 |
对人要求 | 保持一份不浮躁的心情,和一种积极向上心态。 |
其他 | 若侥幸搭建成功,希望你能勤加练习,并记录总结下心得,当然如果失败,任何问题可以随时联系散仙得以解决。 |
散仙本次写此篇文章,除了有完整的环境搭建步骤外,还有一个显示数据库的数据,到前台的功能,其他的删除,添加,修改功能,留给各位学习的朋友,做为练习,如果有什么不明白的地方,可以留言给散仙。
数据库表的设计,主要有2个表,一个学生表,一个班级表,有学生表里面的班级id和班级表里面的id是主外键关系,建表时候,最好先建外键表,再建主键表,截图如下:
第一步,创建一个Web项目
第二步,添加Hibernate的功能,具体步骤,如下截图所示:
然后,就可以创建一个hibernate的数据源了,后面生成实体类会用到。截图如下:
第一步,添加Spring功能,截图如下:
第三步,添加Struts的功能:
完成后,截图如下:
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ac*.xml</param-value> </context-param> <filter> <filter-name>osiv</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>osiv</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
按3层架构,写好的类结构图如下,Spring文件,为了直观,就分离开:
ac.xml里面的配置:
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="url" value="jdbc:mysql://localhost:3306/tt"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> <!-- sessionFactory工厂,Hibernate和Spring集成后,一般由Spring通过依赖注入负责管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 加载实体类配置XML --> <property name="mappingResources"> <list> <value>com/entity/Classes.hbm.xml</value> <value>com/entity/Student.hbm.xml</value> </list> </property> </bean> <!-- 定义抽象BaseDao --> <bean id="baseDao" class="com.commons.BaseDao" abstract="true"> </bean> <!-- 定义抽象业务层 --> <bean id="baseSerivices" class="com.commons.BaseSerivices" abstract="true"></bean> <!-- 定义hibernate事务管理器 --> <bean id="tx" class="org.springframework.orm.hibernate3.HibernateTransactionManager"></bean> <!-- 定义事务通知 --> <tx:advice id="txAdvice" transaction-manager="tx"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> <tx:method name="get*" propagation="SUPPORTS"/> <tx:method name="find*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <!-- 定义aop切面 --> <aop:config> <aop:pointcut expression="execution(* com.service.*.*(..))" id="pc"/> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice"/> </aop:config> </beans>
Struts的文件里内容:
<?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 name="struts.objectFactory" value="spring"></constant> <package name="zhutingting" extends="struts-default"> <action name="showAll" class="studentAction" method="selectAll"> <result name="success">/index.jsp</result> </action> </package> </struts>
项目运行的一个截图:
其他部分的,代码,散仙在这里就不截图了,后面散仙会把整个小项目例子上传上来,有需要的朋友们,可以下载下来,进行参考。
学习过程中,如遇到解决不了的问题,可以留言。