MyBatis和Spring框架整合

本文详细介绍了如何将Spring框架与MyBatis进行整合,包括配置数据库连接、创建Spring配置文件、设置事务管理器、定义MyBatis工厂以及实现持久层、Dao层和测试。整合过程涉及db.properties配置、applicationContext.xml、CustomerMapper.xml映射文件以及Dao层接口与实现类的创建。
摘要由CSDN通过智能技术生成

关于Sping与MyBatis的整合,通过Spring实例化Bean,然后调用实例对象中的查询方法来执行MyBatis映射文件中的SQL语句,如果能够正确查询出数据库中的数据,我们就认为已经整合好,具体实现流程见下图

第一步,src目录下创建db.properties文件,配置数据库的基本四项

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10     <!--最大空闲数-->
jdbc.initialSize=5   <!--初始化连接数-->

第二步,src目录下创建Spring的配置文件 ,applicationContext.xml

  1. 读取db.properties文件的配置
  2. 配置数据源
  3. 配置事务管理器并开启了事务注解
  4. 配置MyBatis工厂与Spring整合
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">


    <!--读取db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>


    <!
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值