• struts 2.3.7+spring3.2.0+MyBatis3.1 整合


               最近无聊,就想着做一下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"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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就可以了 
    <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 整合的童鞋们。

             

  • 相关阅读:
    LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
    LeetCode 216. 组合总和 III(Combination Sum III)
    LeetCode 179. 最大数(Largest Number)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)
    指针变量、普通变量、内存和地址的全面对比
    MiZ702学习笔记8——让MiZ702变身PC的方法
    你可能不知道的,定义,声明,初始化
    原创zynq文章整理(MiZ702教程+例程)
  • 原文地址:https://www.cnblogs.com/zhangnanblog/p/2838582.html
Copyright © 2020-2023  润新知