Spring整合Mybatis

顾名思义,就是将Mybatis的功能交由Spring托管,因此需要在Spring核心配置中完成相应的配置。

Mybatis框架主要是简化了对数据库的访问,也就是数据处理层的业务实现,所以在使用Spring时可以进行分层开发。

也就是将Mybatis的核心功能实现交由applicationContext_mapper实现。

在配置核心文件之前,需要导入重要的依赖:mybatis,mysql-connector-java,spring-context,mybatis-spring整合的框架,以及德鲁伊druid.

下面进行applicationContext_mapper的配置:

<?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"
       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">
    <!-- Spring 接管 MyBatis 的核心配置-->
<!--    读取属性文件jdbc.properties-->
    <context:property-placeholder location="jdbc.properties" />
<!--    创建数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
<!--    设置sqlSessionFactoryBean类-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource" />
        <!--配置Mybatis核心配置文件-->
        <property name="configLocation" value="SqlMapConfig.xml" />
        <!--注册实体类别名-->
        <property name="typeAliasesPackage" value="实体类所在包的完全限定名" />
    </bean>
<!--    注册mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="接口所在包的完全限定名称" />
    </bean>
</beans>

接下来进行业务逻辑层applicationContext.xml的核心配置啦~因为是基于注解的开发,所以一定要添加包扫描,除此之外还有事务等.

需要注意的是,分层开发完毕之后一定要整合在一起。

只是用于测试的话,可以在service配置中import  mapper配置信息。

比如这样:

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

    <import resource="applicationContext_mapper.xml"></import>
    <!--service 业务逻辑层的配置,添加包扫描-->
    <context:component-scan base-package="com.zxf.service.impl"></context:component-scan>
</beans>

虽然Mybatis的业务都交由Spring托管,但是mybatis的核心配置仍需要保留,以便在Spring不能全部托管时,由自身来完成业务的处理。

另外记录一下如何添加文件代码模板:

设置(setting)-->  编辑器(editor)--> 文件和代码模板,添加名称,扩展名以及核心的代码就可以添加自己需要的模板了~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值