ssm整合

时间:2019-09-09
本文章向大家介绍ssm整合,主要包括ssm整合使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、引入所有的jar包

  1. mysql数据库驱动jar
  mysql-connector-java-5.1.46.jar
  2. mybatis 的jar包
  mybatis-3.2.2.jar
  3. spring的核心jar。
  commons-logging-1.1.3.jar
  spring-beans-4.2.2.RELEASE.jar
  spring-context-4.2.2.RELEASE.jar
  spring-core-4.2.2.RELEASE.jar
  spring-expression-4.2.2.RELEASE.jar

  4. spring的注解。
  spring-aop-4.2.2.RELEASE.jar
  5. 事物处理。
  spring-jdbc-4.2.2.RELEASE.jar
  spring-tx-4.2.2.RELEASE.jar
  com.springsource.org.aopalliance-1.0.0.jar
  com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

  6. 数据源的jar包
  c3p0-0.9.1.1.jar

  7. springmvc的jar包。
  spring-web-4.2.2.RELEASE.jar
  spring-webmvc-4.2.2.RELEASE.jar

  8. spring与mybatis整合的jar包
  mybatis-spring-1.3.0.jar

二、配置文件

  1.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

    <!-- 包扫描 -->
    <context:component-scan base-package="com.zhiyou.cyf"/>
    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 定义数据源   使用的是c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${name}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
    </bean>
    <!-- 定义一个SessionFactory工厂类的bean,把mybatis配置文件放进来,这里不额外引用mybatis文件 -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 为该包下的实体类起别名 -->
        <!-- <property name="typeAliasesPackage" value="com.zhiyou.cyf.bean"></property> -->
        <!-- 加载mybatis的映射文件 -->
        <property name="mapperLocations" value="classpath:com/zhiyou/cyf/mapper/*.xml"></property>
        <!-- 如果使用mybatis配置文件 -->
        <!-- <property name="configLocation" value=""></property> -->
    </bean>
    <!-- 配置mybatis接口的实现类userDao=session.getMapper(UserDao.class) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!-- 注意这里的属性是value -->
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> <property name="basePackage" value="com.zhiyou.cyf.dao"></property> </bean> <!-- 事务管理 --> <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务管理方式设置 --> <tx:advice id="advice" transaction-manager="tm"> <tx:attributes> <tx:method name="select*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 切面的配置 --> <aop:config> <aop:pointcut expression="execution(* com.zhiyou.cyf.service.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/> </aop:config> </beans>

  2.springmvc配置文件

<?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-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
    <!-- 开启包扫描 -->
    <context:component-scan base-package="com.zhiyou.cyf.controller"/>
    <!-- 开启注解 -->
    <mvc:annotation-driven/>
    <!-- 释放静态资源 -->
    <mvc:default-servlet-handler/>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/user/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

  3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" id="WebApp_ID" version="3.1">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>encoding</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>encoding</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 配置springmvc文件的路径 -->
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 指定spring配置文件的路径 -->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

原文地址:https://www.cnblogs.com/psxfd4/p/11495019.html