Spring+Mybatis整合+部分源码解析

目录

Spring+mybatis的整合步骤

ApplicationContext.xml配置文件图文解析

使用mybatis+spring实现增删改查

整合需要用到的配置文件

代码展示

实体类

mapper接口

service层

SqlSessionFactoryBean

MapperScannerConfigure

BasicDataSource


Spring+mybatis的整合步骤

1.引入依赖:在项目的构建管理工具中引入Spring和MyBatis的依赖

2.在ApplicationContext.xml配置dataSource数据源 比如连接驱动,连接数据源的url和连接数据库的用户名和密码

3.在ApplicationContext.xml配置文件中创建一个sqlSessionFactory对象,并将数据源和MyBatis的配置文件注入到该对象中

4.配置中使用MapperScannerConfigurer配置MyBatis的扫描器

5.定义Mapper接口,并在XML文件中编写对应的SQL语句

6.在Service层中注入Mapper接口的实例,并调用其方法进行数据库操作

ApplicationContext.xml配置文件图文解析

使用mybatis+spring实现增删改查

整合需要用到的配置文件

ApplicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">


   <!-- 1.引入properties文件-->
    <context:property-placeholder location="连接数据的配置文件(jdbc.proerities)"></context:property-placeholder>
   <!-- 2.配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <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>

    <!--3.创建sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/> <!--name此处的name是指上面数据源的id-->
        <property name="configLoc
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值