SpringMvc与Mybatis整合详解

SpringMvc与Mybatis整合

1.目的

   学习SpringMvc与Mybatis怎样整合在一起。

2.环境准备

1) jdk版本:jdk1.7或jdk1.8

2) IDE:eclipse

3) Tomcat:Apache Tomcat v7.0

4) 数据库:mysqlnavicat for mysql

3.整合

3.1.新建一个web项目

 SpringMvc与Mybatis整合详解

注意:这里选择Dynamic Web Module 2.5 ,因为2.5是主流,默认eclipse的WebContent \WEB-INF\目录下创建web.xml的,而3.0则默认没有web.xml文件

 SpringMvc与Mybatis整合详解

3.2.添加整合所需的jar包

 SpringMvc与Mybatis整合详解

注:所有的包已经放到文件jar里面了

3.3.web.xml配置

3.3.1.代码如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance 

                   http://www.springmodules.org/schema/cache/springmodules-cache.xsd 

                   http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"  

         xmlns="http://java.sun.com/xml/ns/javaee" 

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

                     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>SpringMvc-Mybatis</display-name>

  <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

    <!-- Spring配置文件 -->

<context-param>

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

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

</context-param>

 

<!-- 编码过滤器 -->

<filter>

        <filter-name>encodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <async-supported>true</async-supported>

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

 

<!-- Spring监听器 -->

<listener>

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

</listener>

<!-- 添加对springmvc的支持 -->

<servlet>

        <servlet-name>springMVC</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

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

                <param-value>classpath:spring-mvc.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

        <async-supported>true</async-supported>

</servlet>

<servlet-mapping>

        <servlet-name>springMVC</servlet-name>

        <url-pattern>*.do</url-pattern>

</servlet-mapping>

</web-app>

 

3.3.2.代码详解:

1) Spring配置文件:applicationContext.xml(下面会介绍)

2) 编码过滤器:选择UTF-8,解决中文乱码问题

3) Spring监听器:org.springframework.web.context.ContextLoaderListener

4) 添加对springmvc的支持

    SpringMVC配置文件:spring-mvc.xml(下面会介绍)


3.4.spring-mvc.xml配置

3.4.1.代码如下:

<?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:p="http://www.springframework.org/schema/p"  

    xmlns:aop="http://www.springframework.org/schema/aop"   

    xmlns:context="http://www.springframework.org/schema/context"  

    xmlns:jee="http://www.springframework.org/schema/jee"  

    xmlns:tx="http://www.springframework.org/schema/tx"  

    xsi:schemaLocation="    

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  

        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    

 

<!-- 使用注解的包,包括子集 -->

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

 

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

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

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

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

</bean>

</beans> 

 

3.4.2.代码详解:

1)  使用注解的包,包括子集:自动扫描"com.java1234.controller"包下的所有类,作为controller层的类

2) 视图解析器:controller层返回的数据会解析到相应的 .jsp里面


3.5.applicationContext.xml配置

3.5.1.代码如下:

<?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:p="http://www.springframework.org/schema/p"  

    xmlns:aop="http://www.springframework.org/schema/aop"   

    xmlns:context="http://www.springframework.org/schema/context"  

    xmlns:jee="http://www.springframework.org/schema/jee"  

    xmlns:tx="http://www.springframework.org/schema/tx"  

    xsi:schemaLocation="    

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  

        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    

        

<!-- 自动扫描 -->

<context:component-scan base-package="com.java1234.dao" />

<context:component-scan base-package="com.java1234.service" />

<!-- 配置数据源 -->

<bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">

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

        <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis2"/>

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

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

</bean>

 

<!-- 配置mybatissqlSessionFactory -->

<bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">

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

        <!-- 自动扫描mappers.xml文件 -->

        <property name="mapperLocations" value="classpath:com/java1234/mappers/*.xml"></property>

        <!-- mybatis配置文件 -->

        <property name="configLocation" value="classpath:mybatis-config.xml"></property>

</bean>

 

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.java1234.dao" />

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

</bean>

 

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->

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

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

</bean>

<!-- 配置事务通知属性 -->  

    <tx:advice id="txAdvice" transaction-manager="transactionManager">  

        <!-- 定义事务传播属性 -->  

        <tx:attributes>  

            <tx:method name="insert*" propagation="REQUIRED" />  

            <tx:method name="update*" propagation="REQUIRED" />  

            <tx:method name="edit*" propagation="REQUIRED" />  

            <tx:method name="save*" propagation="REQUIRED" />  

            <tx:method name="add*" propagation="REQUIRED" />  

            <tx:method name="new*" propagation="REQUIRED" />  

            <tx:method name="set*" propagation="REQUIRED" />  

            <tx:method name="remove*" propagation="REQUIRED" />  

            <tx:method name="delete*" propagation="REQUIRED" />  

            <tx:method name="change*" propagation="REQUIRED" />  

            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  

            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  

            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  

            <tx:method name="*" propagation="REQUIRED" read-only="true" />  

        </tx:attributes>  

    </tx:advice>  

  

    <!-- 配置事务切面 -->  

    <aop:config>  

        <aop:pointcut id="serviceOperation"   expression="execution(* com.java1234.service.*.*(..))" />  

        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  

    </aop:config>  

</beans>

 

3.5.2.代码详解:

1)  自动扫描:自动扫描"com.java1234.dao"和"com.java1234.service"包下的所有类,作为dao层和service层的类

2) 配置数据源:就是连接数据库,包括driverClassNameurlusernamepassword这四项

3) 整合最重要

    ① 配置mybatis的sqlSessionFactory:作为整合的桥梁,以前我们要自己获取, 现在spring管理,简单方便

        "org.mybatis.spring.SqlSessionFactoryBean"lib目录下的mybatis-spring-1.2.0.jar包提供     

    ② 引入数据源"dataSource"

    ③ 自动扫描mappers.xml文件,即映射文件

    ④ mybatis配置文mybatis-config.xml下面会介绍

4) DAO接口所在包名,Spring会自动查找其下的类 注入"sqlSessionFactoryBeanName"

5) (事务管理)spring用到的,引入数据源"dataSource"

6) 配置事务通知属性

7) 配置事务切面

 

注:(关于spring事务管理配置:这里简单介绍了一下,具体介绍网上有很多,这里不详细介绍了,大家想深入了解就百度一下吧

    事务在系统服务启动的时候就加载了,一般的,我们把事务配在service层,利用service的业务逻辑借口统一的管理。为什么不用在dao层呢?因为一个service有可能调用多个dao,而这多个dao有可能相互联系,有时候一个操作需要调用多次数据库,但是这多次调用要么全提交,要么全回滚。 因此,在dao层调用事务理论上说不是一个很明智的选择。应该有业务逻辑层service层负责事务,统一处理。 

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。 

 

3.6.mybatis-config.xml

3.6.1.代码如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

        <!-- 别名 -->

        <typeAliases>

                <package name="com.java1234.entity"/>

        </typeAliases>

</configuration>

 

3.6.2.代码详解:

1) 别名<typeAliases>给类的完成限定名取别名,方便使用

2) "com.java1234.entity"把entity实体下的所有东西都导入别名

3) 例如:一般使用entity下的user时,要entity.user,导入别名后,可直接使用user


4.分层

 SpringMvc与Mybatis整合详解

简单介绍:

1) com.java1234.entity——实体层

2) com.java1234.dao——接口层(dao层)

3) com.java1234.mappers——接口实现层(这里是mybatis通过映射实现的)

4) com.java1234.service——事务层

5) com.java1234.service.impl——service实现层

6) com.java1234.controller——控制层

7) com.java1234.util——所需工具类(可选)


5.总结

   关于SpringMvc与Mybatis整合,就这么多,主要是web.xml、applicationContext.xml 的配置,另外我简单说了一下关于“分层”,希望对大家有所帮助,一起交流,一起进步!

注:项目文档可以去我的资源里自行下载!!!