本篇文章主要介绍了"Spring与JDBC连接实现对数据库的增删改查",主要涉及到方面的内容,对于Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
今天主要总结的是关于Spring中与jdbc的连接例子还是昨天的相同例子首先,我们要先开始配备bean3.xml中的配置文件在配置过程当中,首先需要引入的jdb...
今天主要总结的是关于Spring中与jdbc的连接
例子还是昨天的相同例子
首先,我们要先开始配备bean3.xml中的配置文件
在配置过程当中,首先需要引入的jdbc的包
在这个配置中的具体思想为:
1、JdbcTemplate操作数据库
Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。同时,为了支持对properties文件的支持,spring提供了类似于EL表达式的方式,把dataSource.properties的文件参数引入到参数配置之中,。
2,Spring 为DAO的编写带来的好处:
1)优化了的异常类型体系:
细化了数据访问异常,丰富了异常类型
(都是 Unchecked Exception,这种异常不会变动,采用同一种异常,表示同一种现象,与使用的持久化技术无关)
2)使用模板回调模式,开发者不再写模式化代码,简化编程:
不变:资源的获取,资源的释放,异常转化(Spring提供了模板类对此负责)
变化:SQL,变量,结果集的提取
基于JDBC的DAO实现:
使用 JdbcTemplate:(线程安全的)
1)意义:简化对JDBC的操作
模板负责:JDBC对象的获取释放,异常类型的转化
开发者负责:提供SQL,设置SQL中的变量,提取ResultSet
2)应用:
核心方法:
query();
update();
核心回调接口:
PreparedStatementCreator
PreparedStatementSetter
ResultSetExtractor
RowMapper
3)在DAO中获得JdbcTemplate的两种方式:
A)给DAO注入JdbcTempate:
Bean配置:DataSource->JdbcTemplate(需要bean工厂控制)->DAO
B)使DAO类继承 JdbcDaoSupport :
继承的方法:getJdbcTemplate()
Bean配置:DataSource->DAO
DAO需要一个接口和一个接口的实现类。因为Spring注入的时候只能用接口注入
dao是数据库接入层,这个是一个接口,implementsDao去实现这个接口,也就是按照接口中定义的方法取数据,Service也是一个接口,这个接口可以将不同的Dao放在同一个服务中,implementService实现Service。例如我们有个JavaBeanUser类,Course类,然后想在数据库中存取这个user相关的数据.对数据库的操作无非是增删改查,所以就对User类属性的增删改查,做一个接口,为什么要做接口呢,因为我们想与具体的实现脱离耦合关系。因为具体数据库的增删改查,我们既可以用Hibernate也可以ibitas这个dao中只是定义了我要增删改查,具体实现可以用不同的方法,对于user,和Courese他们分别有自己的增删改查,但是我们有的时候又想同时操作他们,所以也就有了Service接口。可以在这个接口中将User和Course的Dao当作成员变量。然后具体操作的时候传进来的是实现dao的imp就行了。然后在action中调用service
现在将bean3.xml文件配置进行上传
default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c//www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
*Dao*
transactionInterceptor
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了Spring与JDBC连接实现对数据库的增删改查,包括了方面的内容,希望对Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_161103.html
相关图片
相关文章