当前位置: 代码迷 >> Eclipse >> 使用MyEclipse 8.5调整SSH框架和基础配置
  详细解决方案

使用MyEclipse 8.5调整SSH框架和基础配置

热度:4   发布时间:2016-04-23 01:02:19.0
使用MyEclipse 8.5整合SSH框架和基础配置
  1. 首先创建一个Web工程——添加Spring框架支持——添加Spring AOP & Core & Persistence Core & JDBC类库支持。类库
  2. 在MyEclipse中准备数据库,确认数据库连接成功后,加入Hibernate支持,在配置Hibernate选项时请使用Spring来管理Hibernate的配置文件,选择已存在的Spring的配置文件进行管理,选择数据库之后选择不需要创建SessionFactory工厂类。
  3. 加入Struts2.1的支持,选择Core和SpringLibraries 2个包,点击完成。
  4. 反转数据库表,在Spring配置文件可以看到一个内置的SessionFactory,可以在prop标签中可以加入一些hibernate配置。
    <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.DerbyDialect</prop>	<prop key="hibernate.show_sql">true</prop>        <prop key="hibernate.hbm2ddl.auto">update</prop>     </props></property><property name="mappingResources">	<list>		<value>com/xwj/entity/Student.hbm.xml</value>		</list>	</property></bean>
    ?例如:hibernate.show_sql,hibernate.hbm2ddl.auto 值可以为"create"时当服务器启动时,会自动检查数据库是否存在映射关系的表格,如果没有将会进行创建,当创建完成后请改为"update"。
  5. Hibernate会配置一个默认的数据源class="org.apache.commons.dbcp.BasicDataSource",这个数据源是效率比较低的一个默认数据源可以把它换成C3P0的数据源
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">		<property name="driverClass" value="${driverClass}">		</property>		<property name="jdbcUrl" value="${jdbcUrl}">		</property>		<property name="user" value="classiccars"></property>		<property name="password" value="myeclipse"></property>		<property name="maxPoolSize" value="50" />		<property name="minPoolSize" value="1" />		<property name="initialPoolSize" value="1" />		<property name="maxIdleTime" value="60" />		<property name="checkoutTimeout" value="2000" /></bean>
    ?
  6. 在项目中可以加入名为xxxx.properties的配置文件 对Spring配置文件进行配置,这样可以更方便的配置Spring,只需要改造properties文件即可对Spring配置。在Spring配置文件中可以使用EL表达式获取值。
  7. 基本Spring配置文件,值得注意的是Struts.xml中Action 的class=" xxxxx" 不是写的是类名,而是Spring管理的bean的名称。
  8. 最后需要使用web加载Spring容器,在web.xml中配置一个加载上下文的监听器,首先需要加入一个Spring 3.0 web Libraies的jar包。加入以下代码。
    <context-param>	<param-name>contextConfigLocation</param-name>	<param-value>classpath:applicationContext*.xml</param-value></context-param><listener>	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
    ?
  9. 差不多基本的整合就完成了。jar包其实可以随便加,因为MyEclipse这么多包看起来也头疼,最主要的是获得核心的配置文件,得到配置文件后可以把这些jar全部remove掉,这样也不会冲突,加入自己需要的jar包即可,本人刚学SSH整合,如果错误欢迎指正。

?

?

  相关解决方案