• Spring、SpringMVC、Mybatis的整合(SSM的整合)


    1.导入jar包

    a)      mysql数据库驱动jar(mybatis-3.2.2)

    b)      mybatis的jar包(mybatis-3.2.2)

    c)      spring的核心jar包

                     i.          spring-beans-4.2.2.RELEASE

                    ii.          spring-context-4.2.2.RELEASE

                   iii.          spring-core-4.2.2.RELEASE

                   iv.          spring-expression-4.2.2.RELEASE

                    v.          commons-logging-1.1.3

    d)      spring的注解(spring-aop-4.2.2.RELEASE)

    e)      事务处理以及操作数据库

                     i.          spring-aspects-4.2.2.RELEASE

                    ii.          spring-jdbc-4.2.2.RELEASE

                   iii.          spring-tx-4.2.2.RELEASE

                   iv.          com.springsource.org.aopalliance-1.0.0

                    v.          com.springsource.org.aspectj.weaver-1.6.8.RELEASE

    f)       数据源的jar包(c3p0-0.9.1.1)

    g)      springmvc的jar包

                     i.          spring-web-4.2.2.RELEASE

                    ii.          spring-webmvc-4.2.2.RELEASE

    h)      spring与mybatis整合的jar包(mybatis-spring-1.3.0)

     

    2.配置文件

    a)      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: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/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.zhiyou100.kfs"></context:component-scan>

           <!-- 配置数据源(里面存放了若干个连接对象):数据库交互的。    数据源:c3p0,druid(阿里) -->

           <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

                  <property name="user" value="root"/>

                  <property name="password" value="123"/>

                  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

                  <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>

           </bean>

          

           <!-- 配置springJdbc的模板类 -->

           <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byType">

                  <property name="dataSource" ref="dataSource"></property>

           </bean>

           <!-- 定义一个事务管理类 -->

           <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

                  <property name="dataSource" ref="dataSource"></property>

           </bean>

           <!-- 开启注解:如果你的事务管理bean的id是transactionManager,属性transaction-manager可以不写 -->

           <tx:annotation-driven transaction-manager="transactionManager"/>

    </beans>

    b)      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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

                  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">

           <!-- 包扫描 -->

           <context:component-scan base-package="com.zhiyou100.kfs.controller"></context:component-scan>

           <!-- 开启注解 -->

           <mvc:annotation-driven></mvc:annotation-driven>

           <!-- 静态资源释放 -->

           <mvc:default-servlet-handler/>

           <!-- 视图解析器 -->

           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                  <property name="prefix" value="/"></property>

                  <property name="suffix" value=".jsp"></property>

           </bean>

    </beans>

    c)      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>SpringSSM</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>encodingFilter</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>encodingFilter</filter-name>

          <url-pattern>/*</url-pattern>

      </filter-mapping>

      <!-- 加载springMVC的配置文件 -->

      <!-- 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>

                         <param-value>classpath:springMVC*.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>

         

          <!-- 指定spring的配置文件的路径 -->

          <context-param>

                 <param-name>contextConfigLocation</param-name>

                 <param-value>classpath:applicationContext.xml</param-value>

          </context-param>

          <!-- 加载spring的配置文件 -->

          <listener>

                 <!-- 默认加载/WEB-INF/applicationContext.xml -->

                 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

          </listener>

    </web-app>

  • 相关阅读:
    JavaScript Patterns and Good Parts
    [Buzz Today]2012.05.015
    The first glance at Python
    The first glance at JavaScript
    [Buzz Today]2012.05.06
    vs.net2008 下 带完善后台管理的flash+js 轮显广告
    Vs.net2008 下 Ajaxpro 使用
    允许asp上传大文件的设置
    使用AutocompleteExtender无效或者没反应,输入啥都在webservice里面跟踪不到
    C#精髓 GridView 72般绝技
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11494701.html
Copyright © 2020-2023  润新知