框架技术--S2SH框架整合(使用myeclipse自动生成)

来源:互联网 发布:特效图片制作软件 编辑:程序博客网 时间:2024/06/06 01:31

简单在这里通过图片记录下通过Myeclipse自动生成Struts2+spring+hibernate的步骤。

 

1、首先创建一个项目,邮件项目名,选择MyEclipse选项,会展示一个出一块有Add*的域,我们选择Add Hibernate Capabilities(增加Hibernate的配置)。

 

2、选择Hibernate3.2 导入必须的jar包,并且选中jar包要拷贝的路径。

 

3、点击下一步。

 

4、这里不进行数据库驱动的配置,因为咱们要让spring托管hibernate,所以这个配置一会配置到spring的配置文件中,此处取消勾选,下一步。

 

5、sessionFactory也由spring创建,此处也不勾选,finish。

 

6、到此hibernate已经整合到项目组中,我们可以看到多了一个hibernate.cfg.xml的配置文件。

 

7、在重复第一步操作,增加spring配置文件,详见图片,

 

8、选择spring配置文件存放的路径,下一步。

 

9、一直下一步,最后会自动生成一个applicationContext.xml的配置文件,只保留这些,其他清空即可。

 

10、我们这里手动引入下Struts2的这些JAR包,可以在官网上下载。

 

11、引入JAR包 COPY到lib目录。

 

12、在引入spring和struts2整合的插件包,可以到spring官网下载。

 

13、然后点击MyEclipse右上角,选中MyEclipse DataBase Exploer,右边会变成DB Browser。

 

14、右键MyEclipse Derby 选中NEW,弹出如图的框,然后选中我们自己本地的数据库,我这里是Mysql。呗忘了要到驱动包哈

 

15、一直next,完成后会将数据库中信息映射到我们的MyEclipse中,详见截图,此时你会看到你自己创建的表。

 

16、右键选中表明,这里我们要生成hibernate的映射实体类文件,就是实体类属性和数据库字段的关联。

 

17、选中后会弹出一个提示框,我们选择下实体类存放的包。

 

18、下面坐下勾选,然后next。

 

19、选中以下主键ID的生成策略。next

 

20、选中下面的A->B B->A,如果有当前表有外键关联的话,需要勾选他会自动帮你创建关联关系,反正就选上吧有就生成,没有也不会有影响滴。

 

21、finis后,会在我们选中的路径下生成实体类和数据库的映射配置文件。

 

22、将struts2信息注册到web.xml上。

 

23、这里我们要在src目录下创建一个struts.xml,内容详见我的struts整合那篇博客的讲解。

到此呢一个S2SH框架搭建好了,但是想要实现功能还要去完善每个xml中的配置,后期开发主要完善applicationContext.xml及struts.xml这两个配置文件。

具体某一块不了解可以详见下面的博客:

框架技术--S2SH框架整合(struts2部分)

框架技术--S2SH框架整合(hibernate部分) No 1

框架技术--S2SH框架整合(spring部分)No 1

框架技术--S2SH框架整合(spring部分)No 2--属性注入

框架技术--S2SH框架整合(spring部分)No 3--声明式事务

 

附 配置文件

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>/WEB-INF/applicationContext.xml</param-value></context-param><!-- 使用ContextLoaderListener初始化Spring容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 定义Struts2的核心Filter --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><!-- 让Struts2的核心Filter拦截所有请求 --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

 

struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 配置了系列常量 --><constant name="struts.ui.theme" value="simple" /><constant name="struts.i18n.encoding" value="UTF-8" /><constant name="struts.custom.i18n.resources" value="globalMessages" /><package name="default" extends="struts-default"><!--===========下面是Action=====--><action name="userAction" class="userAction" method="findUserGrade"><result name="success">/user/userList.jsp</result></action><action name="userPrepareInsertAction" class="userAction" method="prepareInsert"><result name="success">/user/userInsert.jsp</result></action><action name="userInsertAction" class="userAction" method="userInsert"><result name="success"  type="redirect">userAction.action</result></action><action name="userDelAction" class="userAction" method="userDel"><result name="success"  type="redirect">userAction.action</result></action><action name=""><result>.</result></action><action name="*"><result>/{1}.jsp</result></action></package></struts>


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-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="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url"value="jdbc:mysql://localhost:3306/gp"></property><property name="username" value="root"></property><property name="password" value="gaopeng"></property></bean><!-- dataSource注入到sessionFactory --><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.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="mappingResources"><list><value>com/gp/user/dto/Usergrade.hbm.xml</value></list></property></bean><!-- 事务管理器--> <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="add*" propagation="REQUIRED"/><tx:method name="del*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/><tx:method name="save*" propagation="REQUIRED"/><tx:method name="*" read-only="true"/></tx:attributes></tx:advice><!-- 那些类的哪些方法参与事务 --><aop:config><aop:pointcut id="allServiceMethod" expression="execution(* com.gp.user.service.*.*(..))"/><aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/></aop:config><!-- 事务拦截器<bean id="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor"><property name="transactionManager"><ref bean="transactionManager" /></property><property name="transactionAttributes"><props><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean> --><!-- BeanNameAutoProxyCreator  该bean无需被引用,所以没有id属性 <beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="beanNames"><list><value>userServiceImpl</value></list></property><property name="interceptorNames"><list><value>transactionInterceptor</value></list></property></bean>--><!-- dao层注入sessionFactory --><bean id="userDaoImpl" class="com.gp.user.dao.impl.UserDaoImpl"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><!-- service层注入dao --><bean id="userServiceImpl" class="com.gp.user.service.impl.UserServiceImpl"><property name="userDao"><ref bean="userDaoImpl" /></property></bean><!-- action层注入service --><bean id="userAction" class="com.gp.user.action.UserAction"><property name="userService"><ref bean="userServiceImpl" /></property></bean></beans>


 

原创粉丝点击