Spring将数据访问过程中固定的和可变的部分明确划分为两个不同的类:
①模板(template):管理过程中固定的部分(事务控制、管理资源、处理异常)
②回调(callback):处理自定义的数据访问代码(创建语句、绑定参数、整理结果集)
DBCP包含了多个提供连接池功能的数据源;其中BasicDataSource是最常用的;在org.apache.commons.dbcp包下面的BasicDataSource
spring提供了两种数据源对象;位于 org.springframework.jdbc.datasource包中 供选择
①DriverManagerDataSource:与dbcp的不同,此数据源提供的连接并没有进行池化管理
②SingleConnectionDataSource:每个连接请求返回同一个连接
JDBC访问数据;spring提供了三个模板类供使用:
①JdbcTemplate
②NamedParameterJdbcTemplate
③SimpleJdbcTemplate
现在使用SimpleJdbcTemplate模板类
使用SimpleJdbcTemplate访问数据;只需要设置DataSource就能够使用了: 在要使用的那个bean中加上SimpleJdbcTemplate类型的属性
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
datasource所引用的dataSource可以是javax.sql.DataSource的任意实现
<bean id="" class="" p:属性名-ref="jdbcTemplate"/>
也可以直接这样配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/usersystem"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
当在bean中配置了DataSource dataSource属性,就可以不再spring中配置SimpleJdbcTemplate了,因为它会在内部自动创建一个SimpleJdbcTemplate实例
public void test(){
ApplicationContext atc = new ClassPathXmlApplicationContext("com/tanghaibin/JdbcTemplate/aop.xml");
DataSource dataSource = (DataSource) atc.getBean("dataSource");
try {
System.out.println(dataSource.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ORM:对象关系映射
spring集成hibernate:
在配置Hibernate session工厂bean的时候,我们需要确定持久化域对象是通过xml文件还是通过注解来进行配置单,如果选择在xml中定义对象与数据库之间的映射,那么需要在spring中配置LocalSessionFactorybean
<bean id="sessionFactory" class"org.springframewor.orm.hibernate3.LocalSessionFactoryBean" >
<property name=dataSource"ref="dataSource"/>
<property name="mappingResources">
<list>
<value>hbm.xml文件</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">方言</prop>
</props>
</property>
</bean>
当使用那个注解的方式来定义持久化,就需要使用AnnotationSessionFactoryBean来代替LocalSesionFactoryBean:
<bean id="sessionFactory" class="org.springframewor.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name=dataSource"ref="dataSource"/>
<property name="packagesToScan" value="持久化对象的全限定包名"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">方言</prop>
</props>
</property>
</bean>
在dao中得到当前事务的:session:sessionFactory.getCurrentSession()
当dao类加上:@Repository注解的的作用:
当我们没有使用Hibernate模板那么dao类就没有异常转换功能,我们只需要在spring应用上下文中添加一个PersistenceExceptionTranslationPostProcessor的bean:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
PersistenceExceptionTranslationPostProcessor是一个bean的后置处理程序,它会在所有拥有@Repository注解的类上添加一个通知器,这样就会捕获任何平台相关的异常并以spring的非检查型数据访问异常形式重新抛出
JPA:java持久化API
配置容器管理的JPA:
<!-- 配置使用hibernate作为jpa的实现,将配置为一个HibernateJpaVendorAdapter -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="dataBasePlatform" value="org.hibernate.dialect.HSQLDialect"/>
</bean>
<bean id="emf" class="org.springframword.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- jpaVendorAdapter 属性用于指明所使用的是哪一个厂商的JPA实现;Spring提供了多个JPA厂商的适配器;比如hibernate厂商实现的-->
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
</bean>
①模板(template):管理过程中固定的部分(事务控制、管理资源、处理异常)
②回调(callback):处理自定义的数据访问代码(创建语句、绑定参数、整理结果集)
DBCP包含了多个提供连接池功能的数据源;其中BasicDataSource是最常用的;在org.apache.commons.dbcp包下面的BasicDataSource
spring提供了两种数据源对象;位于 org.springframework.jdbc.datasource包中 供选择
①DriverManagerDataSource:与dbcp的不同,此数据源提供的连接并没有进行池化管理
②SingleConnectionDataSource:每个连接请求返回同一个连接
JDBC访问数据;spring提供了三个模板类供使用:
①JdbcTemplate
②NamedParameterJdbcTemplate
③SimpleJdbcTemplate
现在使用SimpleJdbcTemplate模板类
使用SimpleJdbcTemplate访问数据;只需要设置DataSource就能够使用了: 在要使用的那个bean中加上SimpleJdbcTemplate类型的属性
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
datasource所引用的dataSource可以是javax.sql.DataSource的任意实现
<bean id="" class="" p:属性名-ref="jdbcTemplate"/>
也可以直接这样配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/usersystem"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
当在bean中配置了DataSource dataSource属性,就可以不再spring中配置SimpleJdbcTemplate了,因为它会在内部自动创建一个SimpleJdbcTemplate实例
public void test(){
ApplicationContext atc = new ClassPathXmlApplicationContext("com/tanghaibin/JdbcTemplate/aop.xml");
DataSource dataSource = (DataSource) atc.getBean("dataSource");
try {
System.out.println(dataSource.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ORM:对象关系映射
spring集成hibernate:
在配置Hibernate session工厂bean的时候,我们需要确定持久化域对象是通过xml文件还是通过注解来进行配置单,如果选择在xml中定义对象与数据库之间的映射,那么需要在spring中配置LocalSessionFactorybean
<bean id="sessionFactory" class"org.springframewor.orm.hibernate3.LocalSessionFactoryBean" >
<property name=dataSource"ref="dataSource"/>
<property name="mappingResources">
<list>
<value>hbm.xml文件</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">方言</prop>
</props>
</property>
</bean>
当使用那个注解的方式来定义持久化,就需要使用AnnotationSessionFactoryBean来代替LocalSesionFactoryBean:
<bean id="sessionFactory" class="org.springframewor.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name=dataSource"ref="dataSource"/>
<property name="packagesToScan" value="持久化对象的全限定包名"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">方言</prop>
</props>
</property>
</bean>
在dao中得到当前事务的:session:sessionFactory.getCurrentSession()
当dao类加上:@Repository注解的的作用:
当我们没有使用Hibernate模板那么dao类就没有异常转换功能,我们只需要在spring应用上下文中添加一个PersistenceExceptionTranslationPostProcessor的bean:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
PersistenceExceptionTranslationPostProcessor是一个bean的后置处理程序,它会在所有拥有@Repository注解的类上添加一个通知器,这样就会捕获任何平台相关的异常并以spring的非检查型数据访问异常形式重新抛出
JPA:java持久化API
配置容器管理的JPA:
<!-- 配置使用hibernate作为jpa的实现,将配置为一个HibernateJpaVendorAdapter -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="dataBasePlatform" value="org.hibernate.dialect.HSQLDialect"/>
</bean>
<bean id="emf" class="org.springframword.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- jpaVendorAdapter 属性用于指明所使用的是哪一个厂商的JPA实现;Spring提供了多个JPA厂商的适配器;比如hibernate厂商实现的-->
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
</bean>