SSM框架——整合注意事项

SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
本文是博主对上述文章的整合时遇到的问题的汇总
首先给出最终的主要xml文件
pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>org.springmvcgroupId>
  <artifactId>springmvcartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>springmvc Maven Webappname>
  <url>http://maven.apache.orgurl>
    <properties>  
              
            <spring.version>4.0.2.RELEASEspring.version>  
              
            <mybatis.version>3.2.6mybatis.version>  
              
            <slf4j.version>1.7.7slf4j.version>  
            <log4j.version>1.2.17log4j.version>  
        properties>  

        <dependencies>  
            <dependency>  
                <groupId>junitgroupId>  
                <artifactId>junitartifactId>  
                <version>4.11version>  
                  
                <scope>testscope>  
            dependency>  

              
            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-coreartifactId>  
                <version>${spring.version}version>  
            dependency>  

            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-webartifactId>  
                <version>${spring.version}version>  
            dependency>  
            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-oxmartifactId>  
                <version>${spring.version}version>  
            dependency>  
            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-txartifactId>  
                <version>${spring.version}version>  
            dependency>  

            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-jdbcartifactId>  
                <version>${spring.version}version>  
            dependency>  

            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-webmvcartifactId>  
                <version>${spring.version}version>  
            dependency>  
            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-aopartifactId>  
                <version>${spring.version}version>  
            dependency>  

            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-context-supportartifactId>  
                <version>${spring.version}version>  
            dependency>  

            <dependency>  
                <groupId>org.springframeworkgroupId>  
                <artifactId>spring-testartifactId>  
                <version>${spring.version}version>  
            dependency>  
              
            <dependency>  
                <groupId>org.mybatisgroupId>  
                <artifactId>mybatisartifactId>  
                <version>${mybatis.version}version>  
            dependency>  
              
            <dependency>  
                <groupId>org.mybatisgroupId>  
                <artifactId>mybatis-springartifactId>  
                <version>1.2.2version>  
            dependency>  
              
            <dependency>  
                <groupId>javaxgroupId>  
                <artifactId>javaee-apiartifactId>  
                <version>7.0version>  
            dependency>  
              
            <dependency>  
                <groupId>mysqlgroupId>  
                <artifactId>mysql-connector-javaartifactId>  
                <version>5.1.30version>  
            dependency> 
               
            <dependency>
                <groupId>com.oraclegroupId>
                <artifactId>ojdbc6artifactId>
                 <version>11.2.0.1.0version>
            dependency>
              
            <dependency>  
                <groupId>commons-dbcpgroupId>  
                <artifactId>commons-dbcpartifactId>  
                <version>1.2.2version>  
            dependency>  
              
            <dependency>  
                <groupId>jstlgroupId>  
                <artifactId>jstlartifactId>  
                <version>1.2version>  
            dependency>  
              
              
            <dependency>  
                <groupId>log4jgroupId>  
                <artifactId>log4jartifactId>  
                <version>${log4j.version}version>  
            dependency>  


              
            <dependency>  
                <groupId>com.alibabagroupId>  
                <artifactId>fastjsonartifactId>  
                <version>1.1.41version>  
            dependency>  


            <dependency>  
                <groupId>org.slf4jgroupId>  
                <artifactId>slf4j-apiartifactId>  
                <version>${slf4j.version}version>  
            dependency>  

            <dependency>  
                <groupId>org.slf4jgroupId>  
                <artifactId>slf4j-log4j12artifactId>  
                <version>${slf4j.version}version>  
            dependency>  
              
              
            <dependency>  
                <groupId>org.codehaus.jacksongroupId>  
                <artifactId>jackson-mapper-aslartifactId>  
                <version>1.9.13version>  
            dependency>  
              
            <dependency>  
                <groupId>commons-fileuploadgroupId>  
                <artifactId>commons-fileuploadartifactId>  
                <version>1.3.1version>  
            dependency>  
            <dependency>  
                <groupId>commons-iogroupId>  
                <artifactId>commons-ioartifactId>  
                <version>2.4version>  
            dependency>  
            <dependency>  
                <groupId>commons-codecgroupId>  
                <artifactId>commons-codecartifactId>  
                <version>1.9version>  
            dependency>  
        dependencies>  
  <build>
    <finalName>net.cxpfinalName>
  build>
project>

这里博主因为采用的是oracle数据库,所以要导入ojdbc6驱动程序,因为oracle没有授权maven使用ojdbc,所以我们要自己导入ojdbc驱动程序到maven仓库:
1.先在cmd下运行(-Dfile后面的值写你oracle安装目录下的ojdbc包,因为下载oracle会提供ojdbc驱动程序包,所以可以不用在重新下载)
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=F:\app\lenovo\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar
2.然后在pom.xml文件中添加

            <dependency>
                <groupId>com.oraclegroupId>
                <artifactId>ojdbc6artifactId>
                 <version>11.2.0.1.0version>
            dependency>

com.oracle是maven里ojdbc所在的包名,写到这里就够了,不用再写子包。

spring-mvc文件


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    
    <context:component-scan base-package="net.cxp.controller">context:component-scan>

    
    <mvc:annotation-driven enable-matrix-variables="true" />

    
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8value>
            list>
        property>
    bean>

    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            list>
        property>
    bean>

    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    bean>
beans>

这里需要注意的是mappingJacksonHttpMessageConverter。因为spring4.1转json不在使用MappingJacksonHttpMessageConverter,而是使用MappingJackson2HttpMessageConverter,所以使用spring4.1及以上版本的要注意了,改变后的具体写法自行百度吧。

spring-mybatis文件

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  

    
    <context:component-scan base-package="net.cxp">context:component-scan>

    
    <bean id="configProperties"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="locations">
            <list>
                <value>classpath*:/jdbc.propertiesvalue>
            list>
        property>
    bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        
        <property name="initialSize" value="${initialSize}">property>
        
        <property name="maxActive" value="${maxActive}" >property>
        
        <property name="maxIdle" value="${maxIdle}" >property>
        
        <property name="minIdle" value="${minIdle}" >property>
        
        <property name="maxWait" value="${maxWait}" >property>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:net/cxp/mapping/*.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="net.cxp.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

beans>

这里博主被这句话折腾了好久

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

注意是sqlSessionFactoryBeanName,而不是sqlSessionFactoryBean,后面的是value而不是ref,sqlSessionFactory不是bean吗,应该是ref而不是value才对啊,具体解释参考
mybatis-spring集成:配置多数据库源中遇到的问题–MapperScannerConfigurer配置 ,不这样写的话读jdbc.properties文件时总会报string类型不能转为int型的错误。

最后一点是单元测试的时候,你不能直接run单元测试。
1.首先你要先把mapping文件复制到target\classes…文件夹下,如
F:\Workspaces\springmvc Maven Webapp\target\classes\net\cxp,因为此文件夹下没有映射xml文件。
2.然后右键项目名–>run as–>maven test,之后在进行单元测试。不进行这步的话(F:\Workspaces\springmvc Maven Webapp\target\test-classes),你的test-classes文件夹下没有编译好的测试类class文件,自然会报类没有发现的异常。参考maven中运行junit4报测试类class not found错误——maven test命令

你可能感兴趣的:(javaee,ssm)