Posted on 2008-12-15 17:50
李春生 閱讀(4124)
評論(1) 編輯 收藏 所屬分類:
maven2
今天抽空練習(xí)下用maven2+myeclipse搭建一個ssh框架,把搭建過程簡單記錄一下,以便以后方便查閱
1.準(zhǔn)備工作:jdk1.6,maven2.0.9,myeclipse6.0.1,mysql數(shù)據(jù)庫
2.jdk的安裝配置,以及maven2在eclipse里的配置網(wǎng)上的例子很多就不在重復(fù)
3.用m2搭建主項目工程s2shdemo,在myeclipse中點(diǎn)擊new-other-maven project-create a simple project輸入groupId,ArtifactId,在這里別的地方都可以根據(jù)自己的愛好填寫,但打包方式packaging必須選擇pom選項。點(diǎn)下一步后點(diǎn)擊完成即可。
4.完成web層:我在這里用的是m2的命令行搭建一個web工程,進(jìn)入cmd運(yùn)行如下命令:
mvn archetype:create -DgroupId=com.lcsssh.web.action -DartifactId=s2shdemo -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter -DarchetypeVersion=2.0.11.2-SNAPSHOT -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository
其中有標(biāo)志的地方可以根據(jù)自己需要修改,這樣一個struts2和spring的web工程就搭建成功。
5.完成service層:在myeclipse中選擇file-other-maven module填寫module name并選擇父工程,也就是第3部你所建立起來的工程。進(jìn)入下一步就如第三步一樣填寫所需選項,記住這里packaging應(yīng)選擇jar或其他不能用pom。
6.如第五步搭建dao層。
7.整合:以上步驟完成后開始配置,我把配置有關(guān)的文件全部放到web模塊下的resources目錄下
配置web.xml文件,加入如下代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>s2shdemo</display-name>
<distributable />
<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>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>cn.allwap.backend.action</param-value>
</init-param>
</filter>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring 刷新Introspector防止內(nèi)存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- session超時定義,單位為分鐘 -->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
配置web action applicationContext-action.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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="helloWorldAction" class="com.lcsssh.web.action.HelloWorldAction" scope="prototype"/>
<bean id="adminInfoAction" class="com.lcsssh.web.action.AdminInfoAction" scope="prototype">
<property name="adminInfoServiceImpl" ref="adminInfoServiceImpl"/>
</bean>
</beans>
配置業(yè)務(wù)層applicationContext-service.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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"
default-autowire="byName">
<bean id="adminInfoServiceImpl" class="com.lcsssh.service.impl.AdminInfoServiceImpl" scope="prototype">
<property name="adminInfoDAO" ref="adminInfoDAO"></property>
</bean>
</beans>
配置dao 層applicationContext-dao.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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"
default-autowire="byName">
<bean id="adminInfoDAO" class="com.lcsssh.dao.impl.AdminInfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
配置application.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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="username">
<value>user</value>
</property>
<property name="password">
<value>123456</value>
</property>
<property name="maxActive">
<value>20</value>
</property>
<property name="maxIdle">
<value>5</value>
</property>
<property name="maxWait">
<value>-1</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/lcsssh/bo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
<!-- dao object defintions -->
<import resource="applicationContext-dao.xml"></import>
<!-- business object defintions -->
<import resource="applicationContext-service.xml"></import>
<!-- web action object defintions -->
<import resource="applicationContext-action.xml"></import>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="register">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="valid*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<value>transactionInterceptor</value>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor">
</property>
</bean>
</beans>
所有配置文件配置完成。然后在父工程pom.xml下加入第4步創(chuàng)建的web模塊工程,主要代碼如下所示
<modules>
<module>webProject</module>
<module>serviceProject</module>
<module>daoProject</module>
</modules>
好了,到此為止所有的配置完成
8.根據(jù)數(shù)據(jù)庫表生成dao 層代碼,用myeclipse的生成工具生成即可。另外工程中所有依賴包都在web工程下的pom.xml中
為了節(jié)省篇幅,沒有截圖,希望大家體諒,本人水平有限,有不當(dāng)之處敬請包含!!!
代碼下載地址:http://m.tkk7.com/Files/lcs868/s2shdemo.zip