博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring的关于数据源的datasource接口的深入理解
阅读量:7070 次
发布时间:2019-06-28

本文共 4106 字,大约阅读时间需要 13 分钟。

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 
2
3
4
5
6
7
8 9
10
11

  第一行代码:用来读取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文件中配置

  • 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文件配置

  • 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.整合mybatis 的核心是 SqlSessionFactoryBean、MapperFactoryBean(单一接口)

org.mybatis.spring.SqlSessionFactoryBean包含了dataSource(数据源)、mapperLocations(接口的mapper映射文件路径);org.mybatis.spring.mapper.MapperFactoryBean包含了sqlSessionFactory(上面的bean),mapperInterface(接口的完整名称);
  • 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
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的实例

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

MyMapperAnnotation .

public @interface MyMapperAnnotation {}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

BaseDao.java

@MyMapperAnnotationpublic interface BaseDao {    public List
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
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

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
//sqlSessionTemplate是被注入进来的    @Override    public List
findAll() { List
studentList = sqlSessionTemplate.selectList("findAll"); for (Student student : studentList) { System.out.println(student.getName()); } return studentList; } 本文部分转自http://blog.csdn.net/zhaohuijiadelu/article/details/51899080
你可能感兴趣的文章
Oracle Spool教程
查看>>
通过jQuery实现的百叶窗效果
查看>>
python-day1
查看>>
Android架构图
查看>>
xtrabackup 安装
查看>>
黑客必备技能
查看>>
设置文本显示为2行,溢出隐藏后以省略号结尾
查看>>
Sequelize-nodejs-13-Working with legacy tables
查看>>
virtualbox+vagrant学习-2(command cli)-1-vagrant box命令
查看>>
数据库相关
查看>>
转:推荐一个包:Hashids,用来把整数生成唯一字符串(比如:通过加密解密id来隐藏真实id)...
查看>>
参考学习
查看>>
Cocos暂停和重新开始游戏
查看>>
知识发现过程
查看>>
mysql分组取每组大的记录
查看>>
Windows Server 2012 R2怎么配置域控制器?(转)
查看>>
matlab global
查看>>
php构造函数和析构函数
查看>>
javascript 坑
查看>>
Java并发常见问题
查看>>