最近無聊,就想着做一下ssm的整合~在網上先去找資料,但是發現大多都不是入門級。尤其是需要哪些jar,都沒有註明出來。
那麼我就寫一篇,算作是入門的教程,高手勿拍磚~
一、準備工作
首先去找struts的. http://struts.apache.org/ 下載最新的struts 2.3.7. (在發這篇文章前我發現已經升級到了2.3.8)
然後再去找spring的. http://www.springsource.org/spring-framework 下載spring-framework-3.2.0.RELEASE-dist
最後自然就是MyBatis3.0的 http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DMyBatis 下載MyBatis的-3.1.1-bundle
二、提取需要用到的jar
這裏呢,我就直接做ssm的整合。 不分解做。所以不理解的童鞋可以先照我的做。 下去後可以自己度娘,谷哥去找分解整合。
struts需要的jar: spring 所需的jar: MyBatis所需的jar 其他所需jar
struts2-core-2.3.7.jar spring-aop.jar mybatis-3.1.1.jar commons-dbcp-1.4.jar
xwork-core-2.3.7.jar spring-beans.jar mybatis-spring-1.0.0.jar commons-pool.jar
commons-fileupload-1.2.2.jar spring-context.jar dom4j.jar
commons-lang3-3.1.jar spring-core.jar log4j.jar
commons-logging-1.1.1.jar spring-jdbc.jar cglib-2.2.jar
freemarker-2.3.19.jar spring-orm.jar classes12.jar
ognl-3.0.5.jar spring-tx.jar
javassist-3.12.0.GA.jar spring-web.jar
commons-collections-3.1.jar spring-expression.jar
struts2-spring-plugin-2.3.7.jar
OK,我們已經提取出了整合所需的基本jar。 本人用的是oracle數據庫,所以引入了classes12.jar。可以根據自己的需求換。
我們先把這些jar放到我們項目的lib下。這個我想大家都會。
到現在爲止我們把所需要的jar全部導入進去了。 但是還沒有關聯。那麼現在我們開始做關聯配置。
applicationContext.xml
<?xml
version= "1.0" encoding= "UTF-8" ?> <beans
xmlns= "http://www.springframework.org/schema/beans" xsi:schemaLocation="http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd
http: //www.springframework.org/schema/aop
http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd
"> <!--
配置DataSource數據源 --> <bean
class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property
name= "locations" > <value>classpath:jdbc.properties</value> </property> </bean> <bean
id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" > <property
name= "driverClassName" value= "${db.driver}" /> <property
name= "url" value= "${db.url}" /> <property
name= "username" value= "${db.username}" /> <property
name= "password" value= "${db.password}" /> <property
name= "initialSize" value= "${db.initialSize}" /> <property
name= "maxActive" value= "${db.maxActive}" /> <property
name= "validationQuery" value= "${db.validationQuery}" /> <!--<property
name= "defaultAutoCommit" value= "${db.defaultAutoCommit}" ></property> --></bean> <!--
配置事務管理器,注意這裏的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事務就沒有作用了 --> <bean
id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property
name= "dataSource" ref= "dataSource" /> </bean> <!--
創建SqlSessionFactory,同時指定數據源 --> <bean
id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property
name= "dataSource" ref= "dataSource" /> <!--
<property
name= "configLocation" value= "classpath:mybatis-config.xml" ></property> --> </bean> <!--
配置事務的傳播特性 --> <bean
id= "baseTransactionProxy" class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract = "true" > <property
name= "transactionManager" ref= "transactionManager" /> <property
name= "transactionAttributes" > <props> <prop
key= "add*" >PROPAGATION_REQUIRED</prop> <prop
key= "edit*" >PROPAGATION_REQUIRED</prop> <prop
key= "remove*" >PROPAGATION_REQUIRED</prop> <prop
key= "insert*" >PROPAGATION_REQUIRED</prop> <prop
key= "update*" >PROPAGATION_REQUIRED</prop> <prop
key= "del*" >PROPAGATION_REQUIRED</prop> <prop
key= "cancel*" >PROPAGATION_REQUIRED</prop> <prop
key= "*" >readOnly</prop> </props> </property> </bean> <!--
單獨配置一個Mapper; 這種模式就是得給每個mapper接口配置一個bean --> <!--
<bean
id= "accountMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" >
<property
name= "mapperInterface" value= "com.hoo.mapper.AccountMapper" /> <property
name= "sqlSessionFactory" ref= "sqlSessionFactory" />
</bean>
<bean
id= "companyMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" >
<property
name= "mapperInterface" value= "com.hoo.mapper.CompanyMapper" /> <property
name= "sqlSessionFactory" ref= "sqlSessionFactory" />
</bean> -->
<!--
通過掃描的模式,掃描目錄在com/hoo/mapper目錄下,所有的mapper都繼承SqlMapper接口的接口, 這樣一個bean就可以了 <br> <bean
class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <property
name= "basePackage" value= "com.mobile.mapper" /> <property
name= "markerInterface" value= "com.mobile.mapper.SqlMapper" /> </bean>--> </beans> |
jdbc.properties
1 db.driver=oracle.jdbc.driver.OracleDriver 2 db.url=jdbc:oracle:thin:@localhost:1521:orcl 3 db.username= 4 db.password= 5 db.alias=OraclePool 6 db.minIdle=5 7 db.maxIdle=10 8 db.maxWait=5 9 db.maxActive=20 10 db.initialSize=10 11 db.logWriter= 12 db.validationQuery=SELECT 1 FROM DUAL
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 別名 --> <typeAliases> </typeAliases> </configuration>
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> <package name="my-default" abstract="true" extends="struts-default"> </package> </struts>
到此爲止,我們已經把框架整合到了一起。
部署到tomcat運行。
2012-12-29 11:38:13 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_25\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;F:\oracle\product\11.1.0\db_1\bin;C:\Program Files\Broadcom\Broadcom 802.11 Network Adapter;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\Java\jdk1.6.0_25\bin;C:\Program Files\Java\jdk1.6.0_25\jre\bin;F:\Program Files\apache-maven-3.0.4\bin;f:\program files\eclipse; 2012-12-29 11:38:13 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ssm' did not find a matching property. 2012-12-29 11:38:13 org.apache.coyote.http11.Http11Protocol init 信息: Initializing Coyote HTTP/1.1 on http-8080 2012-12-29 11:38:13 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 482 ms 2012-12-29 11:38:13 org.apache.catalina.core.StandardService start 信息: Starting service Catalina 2012-12-29 11:38:13 org.apache.catalina.core.StandardEngine start 信息: Starting Servlet Engine: Apache Tomcat/6.0.32 2012-12-29 11:38:14 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext 2012-12-29 11:38:14,936 [org.apache.ibatis.logging.LogFactory]-[DEBUG] Logging initialized using 'org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl' adapter. 2012-12-29 11:38:14,949 [org.mybatis.spring.SqlSessionFactoryBean]-[DEBUG] Property 'configLocation' not specified, using default MyBatis Configuration 2012-12-29 11:38:16,155 [org.mybatis.spring.SqlSessionFactoryBean]-[DEBUG] Property 'mapperLocations' was not specified or no matching resources found 2012-12-29 11:38:17 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8080 2012-12-29 11:38:17 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8009 2012-12-29 11:38:17 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/22 config=null 2012-12-29 11:38:17 org.apache.catalina.startup.Catalina start 信息: Server startup in 3410 ms
有錯誤信息~~ 請不要緊張。是因爲我們並沒有把mybatis完全整合進來。
配置文件裏註釋掉了引用。這些等項目架構規劃好都可以放開使用。
本人也是初次做ssm的整合。
謹以此文獻給那些想學習ssm 整合的童鞋們。