• SSM框架整合


    SSM:SpringMVC、Spring、Mybatis三大框架。

    Spring 在其中充当粘合剂的作用,无条件管理SpringMVC和Mybatis两大框架。

    其中,Spring和SpringMVC其实可以说是父子关系,但是它们并不是同一个容器。故Spring 粘合 SpringMVC可以做到无缝集成;

    Spring 粘合 Mybatis 需要通过 SqlSessionFactoryBean 对象以及通过 MapperScannerConfigurer 对象来为Mapper接口创建代理对象以便将Mapper接口“实例化”并执行。

            

     SpringMVC 核心配置 applicationContext-mvc.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3         xmlns:context="http://www.springframework.org/schema/context" 
     4         xmlns:aop="http://www.springframework.org/schema/aop" 
     5         xmlns:mvc="http://www.springframework.org/schema/mvc" 
     6         xmlns:tx="http://www.springframework.org/schema/tx" 
     7         xmlns:util="http://www.springframework.org/schema/util"
     8         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     9         xsi:schemaLocation="http://www.springframework.org/schema/beans 
    10                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    11                             http://www.springframework.org/schema/context
    12                             http://www.springframework.org/schema/context/spring-context-3.2.xsd
    13                             http://www.springframework.org/schema/util
    14                             http://www.springframework.org/schema/util/spring-util-3.2.xsd
    15                             http://www.springframework.org/schema/aop
    16                             http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    17                             http://www.springframework.org/schema/tx
    18                             http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    19                             http://www.springframework.org/schema/mvc
    20                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    21                             
    22         
    23         <!-- 开启包扫描 -->
    24         <context:component-scan base-package="com.simon.Controller"/>
    25         <!-- 开启mvc注解 -->
    26         <mvc:annotation-driven/>
    27         
    28         <!-- 视图解析器    拼接真实路径    在视图名称上添加前缀和后缀-->
    29         <bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    30             <property name="prefix" value="/WEB-INF/"/>
    31             <property name="suffix" value=".jsp"/>
    32         </bean>
    33         
    34         
    35                             
    36 </beans>

     Mybatis 核心配置sqlMapConfig.xml:

    1 <?xml version="1.0" encoding="UTF-8" ?>
    2 <!DOCTYPE configuration
    3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5 <configuration>
    6     <!--只需配置缓存,引入第三方缓存插件 -->
    7 
    8 </configuration>

    Spring配置文件 applicationContext.xml:【将mybatis框架整合到Spring中】

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3         xmlns:context="http://www.springframework.org/schema/context" 
     4         xmlns:aop="http://www.springframework.org/schema/aop" 
     5         xmlns:mvc="http://www.springframework.org/schema/mvc" 
     6         xmlns:tx="http://www.springframework.org/schema/tx" 
     7         xmlns:util="http://www.springframework.org/schema/util"
     8         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     9         xsi:schemaLocation="http://www.springframework.org/schema/beans 
    10                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    11                             http://www.springframework.org/schema/context
    12                             http://www.springframework.org/schema/context/spring-context-3.2.xsd
    13                             http://www.springframework.org/schema/util
    14                             http://www.springframework.org/schema/util/spring-util-3.2.xsd
    15                             http://www.springframework.org/schema/aop
    16                             http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    17                             http://www.springframework.org/schema/tx
    18                             http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    19                             http://www.springframework.org/schema/mvc
    20                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    21         
    22         <!-- 开启包扫描 -->
    23         <context:component-scan base-package="com.simon.Service"/>
    24         
    25         <!-- 获取数据库连接 -->
    26             <!-- 引入properties配置文件  'classpath:' 路径到src目录下-->    
    27         <context:property-placeholder location="classpath:/JDBC.properties"/>
    28         
    29             <!-- 获取数据源 -->
    30         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    31             <property name="driverClass" value="${jdbcDriver}"/>
    32             <property name="jdbcUrl" value="${jdbcUrl}"/>
    33             <property name="user" value="${jdbcUser}"/>
    34             <property name="password" value="${jdbcPassword}"/>
    35         </bean>
    36         
    37         <!-- 配置事务管理 -->
    38         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    39             <property name="dataSource" ref="dataSource"></property>
    40         </bean>
    41         <!-- 配置事务通知 -->
    42         <!-- transaction-manager="transactionManager"  该属性若配置事务管理时设置了id="transcationManager"则该属性可省略不配 -->
    43         <tx:advice id="txadvice" transaction-manager="transactionManager">
    44             <tx:attributes>
    45                 <!-- 配置在对数据进行增删改时会添加事务 查的操作可以加也可不加  其它方法不能加事务 -->
    46                 <tx:method name="add*" propagation="REQUIRED"/>
    47                 <tx:method name="delete*" propagation="REQUIRED"/>
    48                 <tx:method name="update*" propagation="REQUIRED"/>
    49                 <tx:method name="find*" propagation="SUPPORTS"/>
    50                 <tx:method name="*" read-only="true"/>
    51             </tx:attributes>
    52         </tx:advice>
    53         <!-- 配制切面 -->
    54         <aop:config>
    55             <aop:pointcut expression="execution(* com.simon.Service..*.*(..))" id="pc"/>
    56             <!-- aop:advisor  advice 和 pointcut的适配器 -->
    57             <aop:advisor advice-ref="txadvice" pointcut-ref="pc"/>
    58         </aop:config>
    59         
    60         
    61         <!-- 粘合 Mybatis框架 -->
    62         <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    63             <!-- 引入Mybatis 核心配置文件 -->
    64             <property name="configLocation" value="classpath:/sqlMapConfig.xml"/>
    65             <!-- 获取数据源 -->
    66             <property name="dataSource" ref="dataSource" />
    67             <!-- 引入映射文件 -->
    68             <property name="mapperLocations" value="classpath:/com/simon/domin/*.xml"/>
    69         </bean>
    70         
    71         <!-- 通过接口方式执行sql后需要有Spring来创建代理对象 -->
    72         <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    73             <property name="basePackage" value="com.simon.Mapper"/>
    74         </bean>
    75                             
    76 </beans>

     web容器配置文件 webxml:

     1 <!-- 配置前端控制器 -->
     2   <servlet>
     3       <servlet-name>springmvc</servlet-name>
     4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     5       <!-- 将springmvc和Spring配置文件引入  -->
     6       <!-- 将Spring容器交给web容器管理 -->
     7       <init-param>
     8           <param-name>contextConfigLocation</param-name>
     9           <param-value>classpath:/applicationContext*.xml</param-value>
    10       </init-param>
    11   </servlet>
    12   <servlet-mapping>
    13       <servlet-name>springmvc</servlet-name>
    14       <url-pattern>*.action</url-pattern>
    15   </servlet-mapping>
    16   
    17   <!-- 配置过滤器 解决中文乱码问题-->
    18   <filter>
    19       <filter-name>encodingfiler</filter-name>
    20       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    21       <!-- 配置编码 -->
    22       <init-param>
    23           <param-name>encoding</param-name>
    24           <param-value>utf-8</param-value>
    25       </init-param>
    26   </filter>
    27   <filter-mapping>
    28       <filter-name>encodingfiler</filter-name>
    29       <url-pattern>/*</url-pattern>
    30   </filter-mapping>
    31   
  • 相关阅读:
    掌游 新项目:我采用NH连接MySql所遇到的问题
    运行时动态将字符串编译为C#可执行代码
    数据库连接字符串用IP死活连不上,只能用计算机名连,ADO或ORM错误提示为表“对象”无效
    File.Copy是Copy完再执行下一句的,自己验证过了。
    window.external.XXX 判断是否有这个方法
    java构建工具——ant使用
    大数据学习之路(1)Hadoop生态体系结构
    Java经典代码片段——使用NIO进行快速的文件拷贝
    Java代码片段——向文件末尾添加内容
    JAVA几种常见的编码格式(转)
  • 原文地址:https://www.cnblogs.com/tongxuping/p/7102037.html
Copyright © 2020-2023  润新知