黑马-Spring与数据库
Spring与数据库
Spring与jdbc
引入dataSource
在客户端
模板编程
类的结构图, 真正干活的是JdbcTemplate(底层实现,操作 excute方法)
JdbcTemplate 构造函数 有DataSource参数 继承JdbcAccessor抽象类 抽象类有方法setDataSource(DataSource)
JdbcDaoSupport 抽象类 (不能被实例化 只能用来继承) 有setDataSource ,setDataSource创建了JdbcTemplate对象,DataSource最终赋值给了 JdbcTemplate
有setTemplate方法
JdbcAccessor抽象类 方法setDataSource(DataSource)
方案一
继承JdbcDaoSupport 两种选择 1.注入DataSource 2.注入JdbcTemplate
1.注入DataSource(setDataSource), 执行 利用JdbcDaoSupport getJdbcTemplate 执行JdbcTemplate的excute方法
2注入JdbcTemplate(JdbcSupport有setDatasource方法) 建立jdbcTemplate bean,注入DataSource(继承自JdbcAccesor的setDataSource方法)
方案二
继承Jdbctemplate 注入(1.JdbcTemplate继承JdbcAccessor 的 setDataSourc 2.利用JdbcTemplate本身构造器) , 执行利用 JdbcTemplate excute方法
1.
2.
方案三
私有属性 JdbcTemplate ,提供set方法 注入,执行 this.jdbTemplate.excute
spring与jdbc的查询
原Jdbc查询操作
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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-2.5.xsd">
- <!--
- 按照指定的路径加载配置文件
- -->
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" destroy-method="close"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <bean id="studentDao1_1" class="com.itheima09.spring.jdbc.dao.StudentDao1">
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <bean id="studentDao1_2" class="com.itheima09.spring.jdbc.dao.StudentDao1">
- <property name="jdbcTemplate">
- <ref bean="jdbcTemplate"/>
- </property>
- </bean>
- <bean id="studentDao2_1" class="com.itheima09.spring.jdbc.dao.StudentDao2">
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <bean id="studentDao2_2" class="com.itheima09.spring.jdbc.dao.StudentDao2">
- <constructor-arg index="0" ref="dataSource"></constructor-arg>
- </bean>
- <bean id="studentDao3" class="com.itheima09.spring.jdbc.dao.StudentDao3">
- <property name="jdbcTemplate">
- <ref bean="jdbcTemplate"/>
- </property>
- </bean>
- </beans>
jdbc-xml
- @Test
- public void testStuent() throws Exception{
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
- StudentDao3 studentDao3=(StudentDao3)context.getBean("studentDao3");
- studentDao3.queryStudent();
- }
test
Spring的声明式事务处理
概念
程序员不再负责处理事务,事务处理交给spring容器来做。
具体的详解
程序员负责两个内容:
1、 对表的crud操作:目标类的目标方法
2、 告诉spring容器什么样的目标方法采用什么样的事务策略
Spring容器负责:(切面)
负责事务的处理
实现原理
采用了aop技术来实现的。
事务的架构
PlatFormTransactionManager 顶级接口
AbstractPlatformTransactionManager 抽象接口 实现commit 和rollback方法 getTransaction
DataSourceTransactionManager 继承AbstractPlatformTransactionManager 实现doGetTransaction 方法
事务的定义
事务的状态
说明:通过spring的事务处理架构,再通过配置文件具体的实现事务的类,就可以让spring容器知道是什么样的技术来操作数据库,通过对事务状态的判断,通过事务的定义就可以知道具体的目标方法采用什么样的事务策略来处理了。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!--
- 引入dataSource
- 把dao层和service层的类导入进来
- -->
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" destroy-method="close"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="studentDao" class="com.itheima09.spring.jdbc.transaction.dao.StudentDaoImpl">
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <bean id="studentService" class="com.itheima09.spring.jdbc.transaction.service.StudentServiceImpl">
- <property name="studentDao">
- <ref bean="studentDao"/>
- </property>
- </bean>
- <!--
- 事务管理器
- 告诉spring容器要采用什么样的技术处理事务
- -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <!--
- 配置声明的事务策略
- id 唯一标示
- transaction-manager 事务管理器
- -->
- <tx:advice id="tx" transaction-manager="transactionManager">
- <tx:attributes>
- <!--
- 以save开头的方法,采用的传播属性是默认值,隔离机制是默认值,是读写事务
- -->
- <tx:method
- name="save*"
- propagation="REQUIRED"
- isolation="DEFAULT"
- read-only="false"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut
- expression="execution(* com.itheima09.spring.jdbc.transaction.service.*.*(..))"
- id="perform"/>
- <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
- </aop:config>
- </beans>
applicationContext.xml
Spring与hibernate
组成
1、 hibernate的配置文件
2、 持久化类和映射文件
3、 Dao层和service层所有的类
4、 Spring的配置文件
5、 客户端
Spring的配置文件
seessionFactory 产生session 进行crud操作 同时,hibernate 需要sessionFactory 通过session 开启事务
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 引入dataSource 把dao层和service层的类导入进来 -->
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" destroy-method="close"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <!-- 导入映射文件所在的路径 -->
- <property name="mappingDirectoryLocations">
- <list>
- <value>classpath:com/itheima09/spring/hibernate/transaction/domain
- </value>
- </list>
- </property>
- <!-- 其他配置 -->
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
- <prop key="hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
- <bean id="sessionFactory2"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <bean id="personDao"
- class="com.itheima09.spring.hibernate.transaction.dao.PersonDaoImpl">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <bean id="personService"
- class="com.itheima09.spring.hibernate.transaction.service.PersonServiceImpl">
- <property name="personDao">
- <ref bean="personDao" />
- </property>
- </bean>
- <!-- 配置事务管理器 -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/>
- </property>
- </bean>
- <tx:advice id="tx" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*"
- isolation="DEFAULT"
- propagation="REQUIRED"
- read-only="false"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut
- expression="execution(* com.itheima09.spring.hibernate.transaction.service.*.*(..))"
- id="perform" />
- <aop:advisor advice-ref="tx" pointcut-ref="perform" />
- </aop:config>
- </beans>
applicationContext.xml
关于session
从上图可以看出,程序员使用sessionFactory产生session,从而进行crud的操作
Spring容器利用sessionFactory产生session,进行事务的操作,所以spring容器产生
的session和程序员用的session肯定是同一个session,所以在spring声明式事务处理的
时候,session必须由当前线程产生
hibernate 回调函数
- package com.heima.spring.hibernate.callback;
- import org.hibernate.Session;
- public interface HibernateCallback {
- public Object doInHibernate(Session session);
- }
HibernateCallback
- package com.heima.spring.hibernate.callback;
- import java.util.List;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- public class SpringHibernateCore {
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- public Object doExecute(HibernateCallback action){
- Object obj=null;
- Session session = sessionFactory.openSession();
- Transaction transaction = session.beginTransaction();
- obj=action.doInHibernate(session);
- transaction.commit();
- return obj;
- }
- public List find(final String hql){
- return (List)this.doExecute(new
- HibernateCallback() {
- public Object doInHibernate(Session session)
- {
- return session.createQuery(hql).list();
- }
- });
- }
- }
SpringHibernateCore
- package com.heima.spring.hibernate.callback;
- import java.util.List;
- import com.heima.spring.hibernate.transaction.domain.Person;
- public class PersonDaoCallback extends SpringHibernateCore{
- public void querPerson(){
- List<Person> pList=this.find("from Person");
- System.out.println(pList.size());
- }
- }
PersonDaoCallback
黑马-Spring与数据库的更多相关文章
- Spring 链接数据库
一.前言 Spring 现在是我们在做 JavaWeb 开发中,用的最主流的框架.以后是不是我们暂时不知道,但现在是.废话不多我就介绍 Spring 中.链接数据库的三种方式: git源码地址 需要的 ...
- 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理
Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...
- Spring的数据库编程浅入浅出——不吹牛逼不装逼
Spring的数据库编程浅入浅出——不吹牛逼不装逼 前言 上文书我写了Spring的核心部分控制反转和依赖注入,后来又衔接了注解,在这后面本来是应该写Spring AOP的,但我觉得对于初学者来说,这 ...
- Spring(四)Spring与数据库编程
Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了Transact ...
- 【Spring】Spring的数据库开发 - 2、Spring JdbcTemplate的常用方法(execute、update、query)
Spring JdbcTemplate的常用方法 文章目录 Spring JdbcTemplate的常用方法 execute() update() query() 简单记录-Java EE企业级应用开 ...
- 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析
Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...
- Spring的数据库开发
Spring JDBC框架操作mysql数据库 Spring中的JDBC为我们省去连接和关闭数据库的代码,我们着重关注对数据库的操作.Sprin ...
- Spring 实现数据库读写分离
随着互联网的大型网站系统访问量的增高,数据库访问压力方面不断的显现而出,所以许多公司在数据库层面采用读写分离技术,也就是一个master,多个slave.master负责数据的实时更新或实时查询,而s ...
- Spring 操作数据库
试了一下spring的JdbcTemplate觉得很好用.首先增加一个连接到mysql数据库的dataSource <bean id="dataSource2" class= ...
随机推荐
- 一些做vue前端的经验
1.先赋值,后渲染 场景:表格渲染中,一般都是这样把json的东西传给table的 this.tableData = json.data.rows 然后的话我们一般会在渲染前对json中的数据做一些转 ...
- 简单选择排序(Simple Selection Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- TBody scrollbar 设置
由于scrollbar自身有宽度 对于tbody来说可能会挤压与thead不对齐下面办法能够解决大致问题 1.设置tbody display:block : overflow-y:auto:(并且修 ...
- MATLAB中mexFunction函数的接口规范(转)
reference:https://www.cnblogs.com/haoyul/p/5606720.html https://www.cnblogs.com/zeakey/p/3984733.htm ...
- FPGA低温不能启动分析(转)
FPGA低温不能启动分析 现象描述:在给medium板光端机做低温试验时,分别给发送版.接收板断电重新启动,发现有的板子在-40°可以启动,而有些板子在-20°都不能启动,需要升高温度到0°以上才能启 ...
- 1005 继续(3n+1)猜想
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对 n=3 进行验证的时 ...
- jdbc中Class.forName(driverName)的作用
上次面试别人问我jdbc的过程: 我是这样回答的: Class.forName加载驱动 DriverManager.connect(url,username, password)获取连接对象 conn ...
- 人生苦短,我用python,为什么选择python,python简介
认识 Python 人生苦短,我用 Python —— Life is short, you need Python  目标 Python 的起源 为什么要用 Python? Python 的特点 ...
- SpringMVC学习三
实现有点用处的增删改查,并利用了AJAX(javascript)动态修改,还有json的返回读取,以及文件上传和下载. 配置基础Employee类以及Dao类 package com.springmv ...
- 解决jsp表达式不能解析的问题
在jsp页面用了表达式,但是出现了表达式不能解析的问题 出现的页面如下 原因:web.xml的版本过低,maven自动生成的web.xml版本为2.3,只有2.3以上的版本才支持表达式 解决方法:改w ...