1.DataSource的接口
这是一个spring接口,可以获取数据库的Connection。是标准化的,取得连接的一种方式。

默认市面上有两个数据库连接池实现了spring的datasource接口,

分别是apache的dbcp数据库连接池和c3p0连接池。

2.spring对java jdk的jdbc做了深层次的封装,叫jdbctemplate,在org.springframework.jdbc包下

org.springframework.jdbc.core.JdbcTemplate包下,

JdbcTemplate主要提供以下五类方法:

  • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

  • update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

  • query方法及queryForXXX方法:用于执行查询相关语句;

  • call方法:用于执行存储过程、函数相关语句。

3.DataSource是数据源,jdbctemplate是操控sql语句的,所以datasource要注入到jdbc之中

DataSource要注入到JdbcTemplate之中。

DataSource要注入到JdbcTemplate之中。

DataSource要注入到JdbcTemplate之中。

JdbcTemplate简介

  Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。

  JdbcTemplate位于中。其全限定命名为org.springframework.jdbc.core.JdbcTemplate。要使用JdbcTemlate还需一个这个包包含了一下事务和异常控制

配置Spring配置文件applicationContext.xml

 1 <context:property-placeholder location="classpath:db.properties"/>
2 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
3 <property name="user" value="${jdbc.user}"></property>
4 <property name="password" value="${jdbc.password}"></property>
5 <property name="driverClass" value="${jdbc.driverClass}"></property>
6 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
7 </bean>
8
9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
10 <property name="dataSource" ref="dataSource"></property>
11 </bean>

  第一行代码:用来读取db.properties文件中的数据。

  第二行代码:用来配置一个数据源,这里数据实现类来自C3P0中的一个属性类。其中属性的值就是来自于db.properties

  第九行代码:配置一个JdbcTemplate实例,并注入一个dataSource数据源

 

二。spring操控数据库的几种方法

1.spring的自带jdbctemplate

2.mybatis的sqlsessionFactoryBean或者sqlsessionTemplate

Spring+JdbcTemplate/Mybatis

1.dao组件继承org.springframework.jdbc.core.support.JdbcDaoSupport 
applicationContext.xml文件中配置

    <util:properties id = "jdbcProperties" location = "classpath:db.properties"></util:properties>

    <bean id = "myDataSource2" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close">
<property name="driverClassName" value ="#{jdbcProperties.driver}"></property>
<property name="url" value="#{jdbcProperties.url}"></property>
<property name="username" value="#{jdbcProperties.user}"></property>
<property name="password" value="#{jdbcProperties.pwd}"></property>
</bean> <bean id = "empDao01" class = "hateapple.dao.EmpDao01">
<property name="dataSource" ref = "myDataSource2"></property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

empDao01通过setter注入dataSource,set方法继承自JdbcDaoSupport,且不能被覆盖重写

//set方法签名
public final void setDataSource(DataSource dataSource)
  • 1
  • 2
  • 1
  • 2

其实就是注入的dataSource被父类JdbcDaoSupport拿去初始化自己的成员变量jdbcTemplate了,empDao01想要使用jdbcTemplate只能通过getJdbcTemplate。

2.不继承 
org.springframework.jdbc.core.support.JdbcDaoSupport,为empDao02注入jdbcTemplate,empDao02通过jdbcTemplate操作数据库; 
applicationContext.xml文件配置

    <bean id = "myDataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="em"></property>
<property name="password" value="em"></property>
</bean> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref = "myDataSource"></property>
</bean>
<bean id = "empDao02" class = "hateapple.dao.EmpDao02">
<property name="jdbcTemplate" ref = "jdbcTemplate"></property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

对比两种方式,第一种是把钥匙交给门卫用的时候向门卫拿,第二种自己带身上。

Spring+myBatis

1.spring整合mybatis 的核心是 SqlSessionFactoryBean、MapperFactoryBean(单一接口)

org.mybatis.spring.SqlSessionFactoryBean包含了dataSource(数据源)、mapperLocations(接口的mapper映射文件路径);

org.mybatis.spring.mapper.MapperFactoryBean包含了sqlSessionFactory(上面的bean),mapperInterface(接口的完整名称);
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref = "myDataSource"></property>
<!-- 加载多个可以改为*.xml -->
<property name="mapperLocations" value = "classpath:hateapple/mapper/StudentMapperMyBatis.xml"></property>
</bean> <!-- hateapple.dao.BaseDao是包含了一个findAll()方法的接口 -->
<bean id="studentMapper" class= "org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="hateapple.dao.BaseDao"></property>
<property name="sqlSessionFactory" ref ="sqlSessionFactory"></property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

测试代码

        @Test
public void testMybatis(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
BaseDao baseDao = (BaseDao)ac.getBean("studentMapper");
List<Student> studentList = baseDao.findAll();
for (Student student : studentList) {
System.out.println(student.getName());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
如果需要多个org.mybatis.spring.mapper.MapperFactoryBean,一个一个配置肯定不现实

2. MapperScannerConfigurer批量扫描接口,并为每个接口生成一个 MapperFactoryBean的实例

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描的接口的包 -->
<property name="basePackage" value ="hateapple.dao"></property>
<!-- 会话工厂 -->
<property name="sqlSessionFactory" ref ="sqlSessionFactory"></property>
<!-- 自定义注解,只有被自定义的注解标记的接口才会被扫描 -->
<property name="annotationClass" value="hateapple.annotation.MyMapperAnnotation"></property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

MyMapperAnnotation .Java

public @interface MyMapperAnnotation {

}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

BaseDao.java

@MyMapperAnnotation
public interface BaseDao {
public List<Student> findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

测试代码:

        @Test
public void testMybatis(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
BaseDao baseDao = (BaseDao)ac.getBean("baseDao");
List<Student> studentList = baseDao.findAll();
for (Student student : studentList) {
System.out.println(student.getName());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

spring+SqlSessionTemplate

其实org.mybatis.spring.mapper.MapperFactoryBean就是封装了一个SqlSessionTemplate操作数据库,我们调用baseDao.findAll()最终的操作还是sqlSessionTemplate.selectList(“findAll”)

1.直接为操作数据库类注入sqlSessionTemplate

<bean id ="sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index = "0" ref="sqlSessionFactory"></constructor-arg>
</bean>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
    //sqlSessionTemplate是被注入进来的
@Override
public List<Student> findAll() {
List<Student> studentList = sqlSessionTemplate.selectList("findAll");
for (Student student : studentList) {
System.out.println(student.getName());
}
return studentList;
}

本文部分转自http://blog.csdn.net/zhaohuijiadelu/article/details/51899080

spring的关于数据源的datasource接口的深入理解的更多相关文章

  1. Spring 的动态数据源实现

    1. 配置多个数据源 这里以两个c3p0数据库连接池的数据源作为实例.在Spring框架下使用c3p0的数据库需要加入c3p0-0.9.1.2.jar(现在最新的)这个支持包.这里以数据同步项目为例: ...

  2. Spring+MyBatis多数据源配置实现

    最近用到了MyBatis配置多数据源,原以为简单配置下就行了,实际操作后发现还是要费些事的,这里记录下,以作备忘 不多废话,直接上代码,后面会有简单的实现介绍 jdbc和log4j的配置 #定义输出格 ...

  3. 四、Spring Boot 多数据源 自动切换

    实现案例场景: 某系统除了需要从自己的主要数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库,要求可以在任何方法上可以灵活指定具体要操作的数据库.为了在开发中以最简单的方法使用,本文基于注解 ...

  4. 三、Spring Boot 多数据源配置

    下面一个Java类是已经写好的根据配置文件动态创建多dataSource的代码,其原理也很简单,就是读取配置文件,根据配置文件中配置的数据源数量,动态创建dataSource并注册到Spring中. ...

  5. spring切换多数据源

    应用场景:在一个项目需要用到两个或两个以上的数据库时,要进行切换数据库,来操作相应的表. 框架:用的是spring 的org.springframework.jdbc.datasource.looku ...

  6. Spring配置动态数据源-读写分离和多数据源

    在现在互联网系统中,随着用户量的增长,单数据源通常无法满足系统的负载要求.因此为了解决用户量增长带来的压力,在数据库层面会采用读写分离技术和数据库拆分等技术.读写分离就是就是一个Master数据库,多 ...

  7. 通过spring抽象路由数据源+MyBatis拦截器实现数据库自动读写分离

    前言 之前使用的读写分离的方案是在mybatis中配置两个数据源,然后生成两个不同的SqlSessionTemplate然后手动去识别执行sql语句是操作主库还是从库.如下图所示: 好处是,你可以人为 ...

  8. Spring Boot的数据访问:CrudRepository接口的使用

    示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...

  9. Spring实现动态数据源,支持动态加入、删除和设置权重及读写分离

    当项目慢慢变大,訪问量也慢慢变大的时候.就难免的要使用多个数据源和设置读写分离了. 在开题之前先说明下,由于项目多是使用Spring,因此下面说到某些操作可能会依赖于Spring. 在我经历过的项目中 ...

随机推荐

  1. UVA - 11077 Find the Permutations (置换)

    Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...

  2. 对JVM还有什么不懂的?一文章带你深入浅出JVM!

    本文跟大家聊聊JVM的内部结构,从组件中的多线程处理,JVM系统线程,局部变量数组等方面进行解析 JVM JVM = 类加载器(classloader) + 执行引擎(execution engine ...

  3. .NET微服务架构及API网关

    一.MSA简介 1.1.MSA是什么 微服务架构MSA是Microservice Architecture的简称,它是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相通讯.互相配合, ...

  4. Gitlab 灾备措施

    Gitlab创建备份 使用Gitlab一键安装包安装Gitlab非常简单,同样的备份恢复与迁移也非常简单.使用一条命令即可创建完整的Gitlab备份: gitlab-rake    gitlab:ba ...

  5. 了解jQuery的$符号

    $是什么? 可以使用typeof关键字来观察$的本质. console.log(type of $); //输出结果为function 因此可以得出结论,$其实就是一个函数.$(); 只是根据所给参数 ...

  6. 部署Seafile服务

    介绍 官网:https://www.seafile.com 客户端/服务端下载:https://www.seafile.com/download/ 中文安装教程(MySQL版):http://manu ...

  7. Junit使用第二弹

    实例总结 1. 参数化测试 有时一个测试方法,不同的参数值会产生不同的结果,那么我们为了测试全面,会把多个参数值都写出来并一一断言测试,这样有时难免费时费力,这是我们便可以采用参数化测试来解决这个问题 ...

  8. C# 3.0新加特性

    类型推断 申明变量的时候,可以不用直指定类型: var i = 5; var s = "Hello"; //两种写法是一样的 int i = 5; string s = " ...

  9. 理解题意后的UVa340

    之前理解题意错误,应该是每一次game,只输入一组答案序列,输入多组测试序列,而之前的错误理解是每一次输入都对应一组答案序列和一组测试序列,下面是理解题意后的代码,但是还是WA,待修改 #includ ...

  10. ubuntu 搭建简易的https网站

    ubuntu 搭建简易的https网站 环境:ubuntu 12.04.5 openssl (1)创建一个ssl的保存路径 sudo mkdir /opt/nginx/ssl (2)生存密钥sudo ...