本文共 7495 字,大约阅读时间需要 24 分钟。
最近无聊,就想着做一下ssm的整合~在网上先去找资料,但是发现大多都不是入门级。尤其是需要哪些jar,都没有注明出来。
那么我就写一篇,算作是入门的教程,高手勿拍砖~
一、准备工作
首先去找struts的. 下载最新的struts 2.3.7. (在发这篇文章前我发现已经升级到了2.3.8)
然后再去找spring的. 下载spring-framework-3.2.0.RELEASE-dist
最后自然就是MyBatis3.0的 下载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= "" xmlns:aop= "" xmlns:tx= "" xmlns:xsi= "" 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=2010 db.initialSize=1011 db.logWriter=12 db.validationQuery=SELECT 1 FROM DUAL
mybatis.xml
struts.xml
到此为止,我们已经把框架整合到了一起。
部署到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-80802012-12-29 11:38:13 org.apache.catalina.startup.Catalina load信息: Initialization processed in 482 ms2012-12-29 11:38:13 org.apache.catalina.core.StandardService start信息: Starting service Catalina2012-12-29 11:38:13 org.apache.catalina.core.StandardEngine start信息: Starting Servlet Engine: Apache Tomcat/6.0.322012-12-29 11:38:14 org.apache.catalina.core.ApplicationContext log信息: Initializing Spring root WebApplicationContext2012-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 Configuration2012-12-29 11:38:16,155 [org.mybatis.spring.SqlSessionFactoryBean]-[DEBUG] Property 'mapperLocations' was not specified or no matching resources found2012-12-29 11:38:17 org.apache.coyote.http11.Http11Protocol start信息: Starting Coyote HTTP/1.1 on http-80802012-12-29 11:38:17 org.apache.jk.common.ChannelSocket init信息: JK: ajp13 listening on /0.0.0.0:80092012-12-29 11:38:17 org.apache.jk.server.JkMain start信息: Jk running ID=0 time=0/22 config=null2012-12-29 11:38:17 org.apache.catalina.startup.Catalina start信息: Server startup in 3410 ms
有错误信息~~ 请不要紧张。是因为我们并没有把mybatis完全整合进来。
配置文件里注释掉了引用。这些等项目架构规划好都可以放开使用。
本人也是初次做ssm的整合。
谨以此文献给那些想学习ssm 整合的童鞋们。
转载地址:http://dmpoi.baihongyu.com/