首页 > 其他分享 >SSM整合03 - Spring整合MyBatis

SSM整合03 - Spring整合MyBatis

时间:2023-02-18 21:36:15浏览次数:39  
标签:xml 03 配置文件 -- Spring SSM mybatis 整合 MyBatis

MyBatis核心配置文件 mybatis-config.xml

Spring整合MyBatis的核心点:将MyBatis核心配置文件中的配置尽可能写入Spring的配置文件中

原MyBatis核心配置文件 mybatis-config.xml

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

    <properties resource="jdbc.properties"/>

    <settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!--配置分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

     <!--实体类所在的包 name="com.zzz.mybatis.pojo"-->
    <typeAliases>
        <package name=""/>
    </typeAliases>

     <!--引入mybatis映射文件所在的包,name="com.zzz.mybatis.mapper"--> 
    <mappers>
        <package name=""/>
    </mappers>
</configuration>

也可以配置留在 mybatis-config.xml中

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

    <!--<properties resource="jdbc.properties"/>-->

    <settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!--配置分页插件-->
    <!--<plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>-->

    <!-- 实体类所在的包 name="com.zzz.mybatis.pojo"-->
    <!--<typeAliases>
        <package name=""/>
    </typeAliases>-->

    <!-- 引入mybatis映射文件所在的包,name="com.zzz.mybatis.mapper" -->
    <!--<mappers>
        <package name=""/>
    </mappers>-->
</configuration>

Spring配置文件 spring.xml

Spring的配置文件中不仅仅是将MyBatis的核心文件的配置搬过来,而是进一步优化了 SqlSession的初始化,在配置中获取 SqlSessionFactory对象,进而创建所有mapper接口的代理实现类对象,并交给IoC容器管理,从而实现在service中可以自动装配(通过注解@Autowired)mapper接口对象

spring.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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--扫描组件(除控制层)-->
    <context:component-scan base-package="com.zzz.ssm">
        <!--排除所有有 @Controller注解的组件-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--引入jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

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

    <!--
        开启事务的注解驱动
        将使用注解 @Transactional 标识的方法或类中所有的方法进行事务管理
    -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <!--配置SqlSessionFactoryBean,可以直接在Spring的IoC中获取 SqlSessionFactory对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--设置MyBatis的核心配置文件的路径-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--加载数据源,在Spring中配置数据源则MyBatis核心配置文件就不需要配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--设置类型别名所对应的包,在Spring中配置完成,MyBatis核心文件就无需配置-->
        <property name="typeAliasesPackage" value="com.zzz.ssm.pojo"></property>
        <!--全局配置,目前只有一个 将下划线映射为驼峰-->
        <!--<property name="mapUnderscoreToCamelClass" value="true"></property>-->
        <!--配置映射文件的路径,只有映射文件的包和mapper接口的包不一致时才需要设置-->
        <!--<property name="mapperLocations" value="classpath:mappers/*.xml"></property>-->
        <!--配置分页插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor"></bean>
            </array>
        </property>
    </bean>

    <!--
        通过上面配置的SqlSession创建路径下所有mapper接口的代理实现类对象,
        并将对象交个IoC容纳管理,至此,在service中可以自动装配mapper接口对象
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--
            同时设置mapper接口和映射文件所在的包路径,
            如果两者路径不一致,则需要在 SqlSessionFactoryBean中设置 mapperLocations
        -->
        <property name="basePackage" value="com.zzz.ssm.mapper"></property>
    </bean>

</beans>

EmployeeServiceImpl.java

public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    ...
}

标签:xml,03,配置文件,--,Spring,SSM,mybatis,整合,MyBatis
From: https://www.cnblogs.com/Ashen-/p/17133665.html

相关文章