当前位置: 首页 > news >正文

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 configurationPUBLIC "-//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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://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.xsdhttp://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
http://www.lryc.cn/news/68180.html

相关文章:

  • 云原生Istio案例实战
  • 解读赛力斯年报:华为智选车的B面
  • 互联网内卷严重?你咋不看看其他行业呢?无非是三十晚上无月亮,大家都一样
  • CompletableFuture异步任务编排使用
  • Scala的高级用法
  • 【31.在排序数组中查找元素的第一个和最后一个位置】
  • 如何构建“Buy Me a Coffee”DeFi dApp
  • Redis 实战篇:巧用 Bitmap 实现亿级海量数据统计
  • 3 天,入门 TAURI 并开发一个跨平台 ChatGPT 客户端
  • 14个最佳创业企业WordPress主题
  • MySQL基础(三十)PowerDesigner的使用
  • nginx 服务器总结
  • 基于Hebb学习的深度学习方法总结
  • 思科模拟器 | 访问控制列表ACL实现网段精准隔绝
  • Python os模块详解
  • Oracle PL/SQL基础语法学习13:比较运算符
  • 金仓数据库适配记录
  • ElasticSearch 学习 ==ELK== 进阶
  • 【数据结构 -- C语言】 双向带头循环链表的实现
  • 自然语言处理与其Mix-up数据增强方法报告
  • Vue(组件化编程:非单文件组件、单文件组件)
  • 【MATLAB数据处理实用案例详解(22)】——基于BP神经网络的PID参数整定
  • 第11章 项目人力资源管理
  • 07-Vue技术栈之(组件之间的通信方式)
  • 度量学习Metirc Learning和基于负例的对比学习Contrastive Learning的异同点思考
  • 3.编写油猴脚本之-helloword
  • openwrt的openclash提示【更新失败,请确认设备闪存空间足够后再试】
  • torch.nn.Module
  • 论文解析-基于 Unity3D 游戏人工智能的研究与应用
  • 6、Flutterr聊天界面网络请求