Spring, SpringMVC, Mybatis integration (integration of SSM)

1. Import jar package

a) mysql database driver jar (mybatis-3.2.2)

b) mybatis a jar (mybatis-3.2.2)

c) spring core jar package

                 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 annotations (spring-aop-4.2.2.RELEASE)

e) operating a database transaction processing, and

                 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) a data source a jar (c3p0-0.9.1.1)

g) springmvc jar package

                 i.          spring-web-4.2.2.RELEASE

                ii.          spring-webmvc-4.2.2.RELEASE

h) spring mybatis integrated with a jar (mybatis-spring-1.3.0)

 

 

 

2. Profiles

a) spring configuration file

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

       <! - source configuration data (stored inside a number of connection objects): database interaction. Data source: c3p0, druid (Ali) ->

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

Guess you like

Origin www.cnblogs.com/kfsrex/p/11494701.html