zoukankan      html  css  js  c++  java
  • SSM 记录

    前言:本过程从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

    3)事务管理,在spring-mybatis增加如下配置

      <!-- 事务管理 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource" />
        </bean>
        <tx:advice id="advice" transaction-manager="transactionManager">
        	<tx:attributes>
        		<tx:method name="*" read-only="true" propagation="REQUIRED"/>
        	</tx:attributes>
        </tx:advice>
        <aop:config>
        	<aop:pointcut expression="execution(* com.ljh.dao.*.*(..))" id="aopTest"/>
        	<aop:advisor pointcut-ref="aopTest"  advice-ref="advice" />
        </aop:config>
    

    效果是为com.ljh.dao包下所有接口中的所有方法,设置只读的事务。

      

    ·

  • 相关阅读:
    vue.js环境的搭建
    图片上传简单demo及springboot上传图片
    mybatise 模糊查询
    thymeleaf th:onclick 传参
    thymeleaf的特殊属性赋值
    无限分类的设计及前后台代码
    mysql 多个属性排序查询
    java添加对象成功后想知道当前添加对象的id
    SpringBoot2.x集成MQTT实现消息推送
    linux下安装MQTT服务器
  • 原文地址:https://www.cnblogs.com/mao-yan/p/7405957.html
Copyright © 2011-2022 走看看