环保部网站建设项目验收方案,php程序员网站开发招聘,室内设计联盟官网论坛,云南建设银行官方网站第一种方式
jdbc整合了:加载数据库驱动,创建连接,写原生语句,执行,关闭这些东西.
第二种方式
mybatis对jdbc进行封装,他允许你通过配置的形式,配置数据库参数,并且允许你通过xml来写动态sql语句.if:test让你可以把sql变得灵活起来.并且还能将你的查询结果直接映射到你想要的…第一种方式
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进行将结果进行映射,返回数据.
整合SpringSpringMVCMybatis
1、修改mybatis-config.xml文件将连接池等配置移除在spring中配置
?xml version1.0 encodingUTF-8?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://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 --/configuration2、在applicationContext.xml中配置druid连接池
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:phttp://www.springframework.org/schema/pxmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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 locationclasspath:jdbc.properties/!-- 2.配置druid连接池 id是固定值class是druid连接池类的全路径 --bean iddataSource classcom.alibaba.druid.pool.DruidDataSource!-- 配置连接数据库的基本信息 --property namedriverClassName value${db.driverClassName}/propertyproperty nameurl value${db.url}/propertyproperty nameusername value${db.username}/propertyproperty namepassword value${db.password}/property/bean!-- 3.整合spring和mybatis框架将SqlSession等对象的创建交给Spring容器id值(sqlSessionFactory)是固定值--bean idsqlSessionFactoryclassorg.mybatis.spring.SqlSessionFactoryBean!-- 3.1.指定mybatis核心配置文件的位置 --property nameconfigLocationvalueclasspath:mybatis/mybatis-config.xml/property!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 --property namedataSource refdataSource/property!-- 3.3、扫描所有的 XxxMapper.xml映射文件读取其中配置的SQL语句 --property namemapperLocations valueclasspath:mybatis/mapper/*.xml//bean!-- 4、定义mapper接口扫描器 --bean classorg.mybatis.spring.mapper.MapperScannerConfigurer!-- 扫描所有XxxMapper接口将接口实例的创建交给spring容器 --property namebasePackage valuecom.mq.dao//bean!-- 5.配置需要扫描的包(service层)spring自动去扫描 base-package下的类如果扫描到的类上有 Controller、Service、Component等注解将会自动将类注册为bean即由spring创建实例--context:component-scan base-packagecom.mq.service//beans3、 在resources目录下创建jdbc.properties文件
db.driverClassNamecom.mysql.cj.jdbc.Driver
db.url jdbc:mysql://localhost:3306/ssm_demo?useSSLfalseserverTimezoneUTC
db.usernameroot
db.passwordmorongrui