Myeclipse搭建SSH框架的Web项目

1.新建web项目

2.引入strust2框架包和依赖文件

右击项目,Config Facets -> Instal Apache Struts(2X) Facets

 

3.添加spring依赖包和配置文件

a、方法和添加struts2相同

b、编辑/src/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: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-2.0.xsd 
           http://www.springframework.org/schema/aop    
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
           http://www.springframework.org/schema/tx    
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd   ">
   <bean id="sessionFactory"
   	class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
   	<property name="configLocation"
   		value="classpath:hibernate.cfg.xml">
   	</property>
   </bean>
   	<!-- 配置事务管理器 指定其作用的sessionFactory把事务交给Spring去处理 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<!-- 利用注解方式实现事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />
	<bean id="template2" class="org.springframework.orm.hibernate3.HibernateTemplate"
		scope="prototype">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>	
	<bean id="user" class="com.lirong.pojo.Users" scope="prototype"></bean>
    <!-- 以上系统配置 	-->


	<!-- 以下为添加类映射实例-->
    <!--数据库pojo类映射-->
	<bean id="user2" class="com.lirong.pojo.Users" scope="prototype"></bean>
    <!--service层类定义-->
	<bean id="userServices2" class="com.lirong.services.UserService" scope="prototype">
        <!--hinernate层数据库操作类定义-->
		<property name="template" ref="template2"></property>
	</bean>

	<bean id="checkUserInfo" class="com.lirong.action.CheckUserInfoAction">
	<property name="user" ref="user2"></property>
	<property name="userService" ref="userServices2"></property>
	</bean>

	
		
		

	
	
	
	
   
   
</beans>

4.建立数据库类连接,并将数据库表结构反转,自动生成对应pojo类

windows -> show view -> other -> DB Brower

右击Myeclipse Derby 新建连接,填写oracle连接数据(以oracle为例,其他数据库也类似),并导入oracle所依赖的包

5、导入hibernate包和配置文件

下图警告Hibernate版本不同时,要back回上一步,修改hibernate版本

6.利用myclipse使数据库类自动生成对象类和hibernate所需类配置文件

打开新建好的数据库连接,找到对应的用户名下的Table->表名->右击

ps:下列涉及的native:为主键生成方式(自动生成),其他生成方式请自行百度

最后会在项目中生成Users.java 和 Users.hbm.xml文件

大功告成!

posted @ 2018-07-17 19:49  榕先生  阅读(8)  评论(0)    收藏  举报  来源