码迷,mamicode.com
首页 > 其他好文 > 详细

SSM 记录

时间:2017-08-22 18:38:25      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:web   word   ror   cdata   drive   ast   null   autowire   close   

前言:本过程从0开始,先是导入最核心的jar包,然后随着ssm中的功能实现,打包===>启动===>报错,一步步解决问题,增加额外的必须的jar包来熟悉ssm

1、导包(核心包)

mybatis:mybatis

msql:mysql-connector-java

spring:spring、spring-core、spring-web、spring-context、spring-beans

spring-mvc:spring-webmvc

mybatis-spring:mybatis-spring

2、配置文件

spring-mvc.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: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.xsd  
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 配置成拦截所有的请求,会有效@RequestMapper,如果是配置成*.do则无效 -->
        <mvc:annotation-driven />
        <!-- 扫描指定包下含有特定注解的类 -->
        <context:component-scan base-package="com.test.*"/>
        <!-- 静态资源请求,启动容器的默认的handler来处理 -->
        <mvc:default-servlet-handler/>
        <!-- 试图解析器 -->
        <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
</beans>

spring-mybatis.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 数据库连接开始 -->
    <context:property-placeholder location="/WEB-INF/classes/datasource.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="username" value="${jdbc_username}"/>
        <property name="url" value="${url}"/>
        <property name="password" value="${pwd}"/>
    </bean>
    <!-- 数据库连接结束 -->
</beans>

使用的context、mvc、tx、aop……标签时,需加上相应的协议

3、完善过程与问题记录

1)java.lang.NullPointerException

org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
问题:maven的传递依赖导致spring 4.2与spring 2.5包冲突,解决,在pom加入如下依赖,去掉spring依赖。
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion>
            <artifactId>spring</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>

2)增加mybatis数据库操作,在spring-mybatis.xml增加配置

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mapperAlias.xml" />
    </bean>
    
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.test.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>

  2.1)打包启动 错:java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport,增加spring-tx依赖

  2.2)即上之后 打包启动 错误信息有java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy 增加spring-jdbc的依赖

  2.3)单元测试   nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.csdn.training.service.BeanB] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 原因是缺少bean的依赖注入。

参考:http://blog.csdn.net/jiangchao858/article/details/51586515

·

 

SSM 记录

标签:web   word   ror   cdata   drive   ast   null   autowire   close   

原文地址:http://www.cnblogs.com/mao-yan/p/7405957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!