jdbc和druid和mybatis之间的关系

第一种方式

jdbc整合了:加载数据库驱动,创建连接,写原生语句,执行,关闭这些东西.

第二种方式

mybatis对jdbc进行封装,他允许你通过配置的形式,配置数据库参数,并且允许你通过xml来写动态sql语句.if:test让你可以把sql变得灵活起来.并且还能将你的查询结果直接映射到你想要的实体上面.
然后你就去配置你的用户名,密码,连接超时,等等.
等你下次使用mybatis时,他后面会根据你的配置,帮你加载数据库驱动,创建连接,写原生语句,执行,关闭.

第三种方式

但目前每次访问数据库都要重新创建关闭一个新的连接,会浪费时间和性能,所以mybatis需要再配一个连接池,比如druid.c3p0
mybatis让你指定连接池是谁,如druid.之后将原来的东西都交给druid.什么账号了,密码了.都给他,让druid帮你创建一批连接,在你需要用的时候,mybatis可以从druid连接池中取一个连接

一次简单的访问流程:

controller->service->dao->mapper
1.首先项目启动时druid就已经使用jdbc创建好一堆连接了,留待后用.
2.当请求到mapper时,mybatis框架创建临时类.
3.然后将动态sql进行替换重写,变成原始的native sql.
4.从druid拿到一个连接.
5.将sql通过连接交给数据库执行.
6.然后获取执行结果.
7.mybatis进行将结果进行映射,返回数据.

整合Spring+SpringMVC+Mybatis

1、修改mybatis-config.xml文件,将连接池等配置移除,在spring中配置

<?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">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 1.配置开发环境 -->
	<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
	<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
	<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
	
</configuration>

2、在applicationContext.xml中配置druid连接池

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 1.加载jdbc.properties文件的位置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 配置连接数据库的基本信息 -->
        <property name="driverClassName" value="${db.driverClassName}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>

    <!-- 3.整合spring和mybatis框架
        将SqlSession等对象的创建交给Spring容器
        id值(sqlSessionFactory)是固定值
     -->
    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 3.1.指定mybatis核心配置文件的位置 -->
        <property name="configLocation"
                  value="classpath:mybatis/mybatis-config.xml"></property>
        <!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 3.3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 -->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
    </bean>

    <!-- 4、定义mapper接口扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 -->
        <property name="basePackage" value="com.mq.dao"/>
    </bean>

    <!-- 5.配置需要扫描的包(service层):spring自动去扫描 base-package下的类,
        如果扫描到的类上有 @Controller、@Service、@Component等注解,
        将会自动将类注册为bean(即由spring创建实例)
     -->
    <context:component-scan base-package="com.mq.service"/>

</beans>

3、 在resources目录下创建jdbc.properties文件

db.driverClassName=com.mysql.cj.jdbc.Driver
db.url= jdbc:mysql://localhost:3306/ssm_demo?useSSL=false&serverTimezone=UTC
db.username=root
db.password=morongrui

猜你喜欢

转载自blog.csdn.net/weixin_44313315/article/details/130464337