Spring整合Mybatis

一、Spring的配置

1、web.xml 中配置监听器ContextLoaderListener。

  ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6 
 7     <!-- 上下文配置 -->
 8     <context-param>
 9         <param-name>contextConfigLocation</param-name>
10         <param-value>classpath:application-context.xml</param-value>
11     </context-param>
12     
13     <!-- 监听器配置 -->
14     <listener>
15         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16     </listener>
17  
18 </web-app>

2、配置在加载时需要的application-context.xml文件,如上面所示。(上下文的配置)

3、创建 application-context.xml 文件,并配置需要读取的xml文件。

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 只配置一个导入 -->
    <import resource="config/*.xml" />

</beans>

4、在application-context.xml文件中配置自动扫描包。

<!-- 配置自动扫描 -->
<context:component-scan base-package="cn.itcast"/>

二、数据源及事务的配置

1、在classpath路径下添加jdbc.properties配置文件。

1 driverClassName=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
3 username=root
4 password=123456

2、配置读取jdbc.properties。(property.xml文件)

<!-- 读取多个properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

3、配置数据源,使用druid数据源。(jdbc.xml文件)

<!-- 配置Druid -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <!-- 驱动类 -->
    <property name="driverClassName" value="${driverClassName}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
</bean>

4、配置事务。(transation.xml文件)

<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 开启事务的注解配置方式  @Transation-->
<tx:annotation-driven transaction-manager="transactionManager"/>

三、整合Mybatis

1、Mybatis的工厂配置。(mybatis.xml文件)

<!-- Mybatis整合 -->
<!-- 配置工厂 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 配置数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 全局配置文件 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
    
<!-- 扫描接口及Mapper文件 
   1、接口和Mapper发布后在同一个文件夹下;
   2、接口和Mapper文件名相同.
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 基本包 -->
    <property name="basePackage" value="cn.itcast.core.dao"/>
</bean>

2、在classpath的路径下面创建 mybatis-config.xml 文件,以下都不需要配置。

二级缓存存在一定的问题;可以使用反向生成工具配置为全类名,所以不需要别名;使用了Mapper和接口的扫描,所以不需要配置。

<?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>
    <!-- 设置二级缓存, 延时加载 -->
    <!-- 别名 -->
    <!-- Mapper的位置,整合了spring不用配置 -->
</configuration>

3、项目的配置文件结构,如下所示:

原文地址:https://www.cnblogs.com/yufeng218/p/6512771.html