2019-7-19

今日心得

SSM整合

spring和springMVC是天然集成,所以只需要解决mybats和spring整合的问题,中间项目mybatis-sping项目进行整合,重点就是在整合mybatis和spring。

  • 由spring容器管理mybatis这个mapper
  • 由spring利用声明式事务(AOP)进行事务综合管理

调用过程

要遵循Controller–Service接口–ServiceImpt实现类–Mapper接口模式

mark

mark

mark

mark

mark

@ResponseBody

直接将结果返回在当前页面。

关于配置文件

mark

  1. applicationContext.xml

    用于扫描所有的spring文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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.xsd">
    <!--    引入spring和其他整合的配置文件-->
        <import resource="classpath:spring/spring-*.xml"/>
    
    </beans>
    
  2. spring-mybatis.xml

    用于解决spring与mybatis的整合问题,其中,这里面需要进行扫描mapper,配置数据库信息,声明事务

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:cache="http://www.springframework.org/schema/cache"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:lang="http://www.springframework.org/schema/lang"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:task="http://www.springframework.org/schema/task"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
           http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            ">
    
    
    
        <!--指定mapper接口存放在该包下-->
        <context:component-scan base-package="zoe.ssm.mapper">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        <!--引入数据库相关信息配置文件-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <!--如果有需要,把所有的属性全部写到properties文件中-->
            <!--c3p0连接池的私有属性-->
            <property name="maxPoolSize" value="30"/>
            <property name="minPoolSize" value="10"/>
            <!--关闭连接后不自动commit-->
            <property name="autoCommitOnClose" value="false"/>
            <!--获取连接超时时间-->
            <property name="checkoutTimeout" value="100000"/>
            <!--连接失败后尝试次数-->
            <property name="acquireRetryAttempts" value="2"/>
        </bean>
        <!--最后关键一步,如何整合mybatis-->
        <!--1.注入一股mybatis的sqlsessionFactory这就是我们所需要的关键步骤2.声明式事务管理器-->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--引入mapper文件-->
            <!--要求mapper文件必须在mapper包下-->
            <property name="mapperLocations" value="classpath:mapper/*.xml"/>
            <property name="configuration">
                <bean class="org.apache.ibatis.session.Configuration">
                    <!--可以加入驼峰命名法其他mybatis的配置也就是mybatis.cfg.xml的相关配置都会转移到这里-->
                    <property name="mapUnderscoreToCamelCase" value="true"/>
                </bean>
            </property>
    
        </bean>
        <!--持久化接口-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="zoe.ssm.mapper"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
        </bean>
        <!--事务管理 使用数据源事务管理类进行管理-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <tx:advice transaction-manager="transactionManager" id="transactionAdvice">
            <!--事务管理的相关值以及它的传播性-->
            <tx:attributes>
                <!--查询相关配置为只读select开头或者get或者query-->
                <tx:method name="select*" read-only="true"/>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="query*" read-only="true"/>
                <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
            </tx:attributes>
        </tx:advice>
        <!--使用AOP对事务管理的范围进行织入明确几个点1.对哪些地方需要进行事务管理execution书写,明确边界,2使用什么策略去管理
      策略我们使用了tx:advice全部书写与其中,在我们的aop的advice当中只需要去引用这个事务管理器者的建议即可-->
        <aop:config>
            <aop:pointcut id="txCut" expression="execution(* zoe.ssm.serevice..*.*(..))"/>
            <aop:advisor advice-ref="transactionAdvice" pointcut-ref="txCut"/>
        </aop:config>
        <!--采用注解进行事务管理,请在service的实现类上面加上@Transanction注解-->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>
    
  3. spring-servlet

    扫描注解包,配置页面逻辑

    <?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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
           >
    
        <context:component-scan base-package="zoe.ssm">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        </context:component-scan>
    
    <!--    <context:component-scan base-package="zoe.ssm"/>-->
    
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <mvc:annotation-driven>
    
        </mvc:annotation-driven>
        <mvc:default-servlet-handler/>
    
    </beans>
    
  4. spring-context.xml

    由于在spring-servlet里面扫描注解包的时候,排除了servlet

    因此在这里进行一个补充

    <?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:aop="http://www.springframework.org/schema/aop"
           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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--1.spring容器注册-->
        <context:annotation-config/>
        <!--2.自动扫描配置-->
        <context:component-scan base-package="zoe.ssm"/>
        <!--3.激活aop注解方式的代理-->
        <aop:aspectj-autoproxy/>
    
    </beans>
    
    

小结

  1. 可能是由于以前写关于网站有关的东西,都会习惯性是有jsp,比如用jsp查看数据是不是对的,也没有去关注控制台的显示结果,不管什么事情都想要用jsp,总觉得jsp可以查看很多东西,此次才有了全新的看法:

    • 使用@ResponseBody,可以在当前页面进行结果的显示
  2. 对于数据库的配置,比较不了解的还是dataSource这一块,就是当我们把数据库的配置信息(jdbc.properties)分离出来后,需要用到一个bean来把数据库的信息注册进去,而注册的时候class有很多种数据源,比如有阿里巴巴的,我们公司自己封装的,我自己则是在网上找了一个com.mchange.v2.c3p0.ComboPooledDataSource,也不知道能不能用,就是照着配置。

  3. 扫描自动注解这一块,由于每个网上的demo有不同的想法,很多都是base直接全部扫描,但是这次我找的demo是有进行分离的,就是虽然是自动扫描自动注解,但是他把controller、service、mapper分开扫描,就是每次扫描全包的时候,就是会用到“expression”,因此就会把上面的分开来。

  4. 在spring-mybatis中,利用来实现把在mybatis中的mybatis.cfg.xml的内容加进来,比如说驼峰转换之类的setting配置,实现了很好的整合

  5. 对于事务处理还是有点懵。。

     <!--事务管理 使用数据源事务管理类进行管理-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <tx:advice transaction-manager="transactionManager" id="transactionAdvice">
            <!--事务管理的相关值以及它的传播性-->
            <tx:attributes>
                <!--查询相关配置为只读select开头或者get或者query-->
                <tx:method name="select*" read-only="true"/>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="query*" read-only="true"/>
                <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
            </tx:attributes>
        </tx:advice>
        <!--使用AOP对事务管理的范围进行织入明确几个点1.对哪些地方需要进行事务管理execution书写,明确边界,2使用什么策略去管理
      策略我们使用了tx:advice全部书写与其中,在我们的aop的advice当中只需要去引用这个事务管理器者的建议即可-->
        <aop:config>
            <aop:pointcut id="txCut" expression="execution(* zoe.ssm.serevice..*.*(..))"/>
            <aop:advisor advice-ref="transactionAdvice" pointcut-ref="txCut"/>
        </aop:config>
        <!--采用注解进行事务管理,请在service的实现类上面加上@Transanction注解-->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
  6. 最后就是要去整理一下从SSM一步一步搭建起来的笔记吧,虽然感觉大致上有了全新的理解,但是碰到问题还是不懂得看报错,很多时候都看懂不报错是在写什么,百度的时候也都把整个报错都拷贝过去查找,要找实际性的解决方案实际上太少了,所以以后还是要加强看报错的能力吧。

遇到的问题

  1. mark

    问题出在于spring的依赖版本不一致

  2. mark

    没有赋参数

    mark

    因此要加入参数:

    mark

其他tip

  1. Alt+Shift+C 查看历史记录
  2. Ctrl+空格 快速补齐img图片路径,也可以补齐标签
  3. Ctrl+Shift+Enter快速补齐if,do-while,try-catch
  4. Ctrl+Shift+I 查看某个定义,而不需要打开对应的文件
  5. Ctrl+Shift+up or down 全局修改某个关键字
  6. Alt+Shift+F10 运行调试
  7. Ctrl+Shift+I 快速定义查看器
  8. Ctrl+Alt+F7 快速插入符号名称对应的方法
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值