ssm整合及事务配置

简介

ssm框架使我们常用的一种框架,ssm整合其实是用spring去整合mybatis和springmvc,所以思路和步骤都比较清晰。

spring整合mybatis

首先我们在spring主配置文件中去整合mybatis。主要是将sqlSessionFactory工厂交给spring Ioc容器管理。

<context:property-placeholder location="classpath:db.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}" />
    </bean>

    <!--配置生产SqlSession对象的工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--扫描pojo包,给包下所有pojo对象起别名-->
        <property name="typeAliasesPackage" value="com.itcc.domain"/>
    </bean>

    <!--扫描接口包路径,生成包下所有接口的代理对象,并且放入spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itcc.dao"/>
    </bean>

spring事务配置

运用aop进行声明式事务配置,其中查询操作设为只读且不一定要事务,而其他操作必须要有事务。

<!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务的通知-->
    <tx:advice id="advice">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置切面-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.itcc.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
    </aop:config>

spring整合springmvc

springMVC其实本来就属于spring,整合起来很简单,分清他们各自要扫描的包,spring主要负责service层,springmvc负责web层,然后在服务器启动时就去加载spring配置文件,因为在web层我们调用service层方法时需要service对象,而在此之前我们并没有加载spring配置文件,所以容器中并没有这个bean队象。

springmvc配置

 <!-- 扫描controller的注解,别的不扫描 -->
    <context:component-scan base-package="com.itcc.controller">
    </context:component-scan>

    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- JSP文件所在的目录 -->
        <property name="prefix" value="/pages/" />
        <!-- 文件的后缀名 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 设置静态资源不过滤 -->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/plugins/" mapping="/plugins/**" />

    <!-- 开启对SpringMVC注解的支持 -->
    <mvc:annotation-driven />

web.xml配置,

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springConfig.xml</param-value>
  </context-param>

  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 解决中文乱码过滤器 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM整合是指将SpringSpringMvc和Mybatis这三个框架进行整合,以提高项目的开发效率和代码的复用性。下面是SSM整合的思路与配置详解: 1. 配置准备: 在开始整合之前,需要确保你已经配置好了Java开发环境和相应的IDE工具。此外,还要保证你已经下载安装了SpringSpringMvc和Mybatis的相关依赖库。 2. 配置过程: 2.1 引入依赖: 首先,在你的项目引入SpringSpringMvc和Mybatis的相关依赖库。你可以通过在pom.xml文件添加相应的依赖来完成这一步骤。这些依赖库包括spring-core、spring-webmvc、mybatis等。 2.2 配置Web.xml文件: 在Web.xml文件,你需要配置SpringSpringMvc的相关配置。这包括配置DispatcherServlet、ContextLoaderListener和字符编码过滤器等。可以通过在Web.xml文件添加相应的配置来完成这一步骤。 2.3 配置SpringMvc.xml文件: 在SpringMvc.xml文件,你需要配置SpringMvc的相关配置,包括扫描控制器、视图解析器、静态资源路径等。可以通过在SpringMvc.xml文件添加相应的配置来完成这一步骤。 2.4 配置mybatis.xml文件: 在mybatis.xml文件,你需要配置Mybatis的相关配置,包括数据库连接信息、Mapper扫描路径等。可以通过在mybatis.xml文件添加相应的配置来完成这一步骤。 2.5 配置applicationContext.xml文件: 在applicationContext.xml文件,你需要配置Spring的相关配置,包括扫描注解、声明事务管理器等。可以通过在applicationContext.xml文件添加相应的配置来完成这一步骤。 3. mybatis逆向工程: 如果你想要通过数据库表生成相应的实体类、Mapper接口和Mapper XML文件,可以使用mybatis逆向工程来完成。你需要配置相应的代码生成器,包括数据库连接信息、表名、生成路径等。 总结起来,SSM整合的思路是将SpringSpringMvc和Mybatis三个框架整合在一起,通过配置文件和相关依赖库来实现。具体的配置过程包括引入依赖、配置Web.xml文件、配置SpringMvc.xml文件、配置mybatis.xml文件和配置applicationContext.xml文件。如果需要生成实体类和Mapper文件,可以使用mybatis逆向工程来完成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SSM整合思路与配置详解](https://blog.csdn.net/slysxy/article/details/107710775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SSM整合,手把手教程,详解思路讲解](https://blog.csdn.net/liyingjie2001/article/details/124809314)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值