spring
来源:互联网 发布:如何查看网络稳定性 编辑:程序博客网 时间:2024/04/30 00:18
一.JDBCTemplate
导入的jar
//创建连接池
DriverManagerDataSource dmd=new DriverManagerDataSource();
//往连接池里面设置4大参数
dmd.setDriverClassName("com.mysql.jdbc.Driver");
dmd.setUrl("jdbc:mysql:///myspring03");
dmd.setUsername("root");
dmd.setPassword("root");
//创建jdbcTemplate模板
JdbcTemplate template =new JdbcTemplate();
//将连接池设置到模板中
template.setDataSource(dmd);
=====================
<bean id="ds3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///myspring03"/>
<property name="user" value="root"/>
<property name="password" value="root"></property>
</bean>
<bean id=" jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"ref="ds3"/>
</bean> -->
引入外部文件:
<!--外部属性文件的导入 ====================================================== -->
<!--在xml的文件中引入db.properties文件-->
<!-- <context:property-placeholder location="classpath:db.properties"/> -->
<!-- 在配置文件中使用配置文件中的信息 -->
<!-- <bean id="ds4" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"ref="ds4"/>
</bean> -->
<!--外部属性文件的导入 ====================================================== end-->
==============
==================
1.1.5Spring的事务管理:
声明事务管理在xml中的配置:(基于AOPxml的配置)
<!--在xml的文件中引入db.properties文件-->
<context:property-placeholderlocation="classpath:db.properties"/>
<!-- 在配置文件中使用配置文件中的信息 -->
<bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"value="${jdbc.driver}"/>
<property name="jdbcUrl"value="${jdbc.url}"/>
<property name="user"value="${jdbc.user}"/>
<property name="password"value="${jdbc.password}"></property>
</bean >
<bean id="accountService"class="cn.itcast.demo3.AccountServiceImpl">
<property name="accountDao"ref="accountDao"></property>
</bean >
<bean id="accountDao"class="cn.itcast.demo3.AccountDaoImpl">
<!-- 直接注入我们的数据源 -->
<property name="dataSource"ref="dataSource"></property>
</bean >
<!--注册事务管理器 -->
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 事务管理器得需要引入我们的连接池即数据源,因为得获得连接,开启事务 -->
<property name="dataSource"ref="dataSource"></property>
</bean >
<!--基于 AOP的声明事务管理 -->
<!--定义一个增强,事务的通知 -->
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<!--事务里面的属性 -->
<tx:attributes >
<tx:method name= "transf" />
</tx:attributes >
</tx:advice >
<!--定义AOP的配置(通知和切点的组合) -->
<aop:config >
<aop:pointcut expression="execution(* cn.itcast.demo3.AccountService+.*(..))"id="mypointcut"/>
<aop:advisor advice-ref="txAdvice"pointcut-ref="mypointcut"/>
</aop:config >
步骤:
1>导包 aop tx
2>在xml中配置
- 配置数据源
- 声明目标类,持久层的类。并且在持久层中注入数据源,目的是Dao类继承JdbcDaoSupport。此类帮我们拿到模板,直接注入数据源即可
- 注册事务管理器。事务管理器中注入数据源!目的是获得连接,开启事务
- 定义增强。事务管理的 : <tx:Advicer id="" transcation-manager="事务管理器"> 事务里面的属性
- 定义Aop的设置
<!--声明目标类和dao 类。在dao类中注入数据源 -->
<!--注册事务管理器 -->
<!--定义增强 -->
<!--声明一个切面 通知和切点的组合 -->
基于注解的事务管理:
第一步:事务管理器:
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
第二步:注解事务:
<!-- 开启注解的事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
第三步:在Service上使用注解
@Transactional
* 注解中有属性值:
* isolation
* propagation
* readOnly
=====================
<!--在xml的文件中引入db.properties文件-->
<context:property-placeholderlocation="classpath:db.properties"/>
<!-- 在配置文件中使用配置文件中的信息 -->
<bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"value="${jdbc.driver}"/>
<property name="jdbcUrl"value="${jdbc.url}"/>
<property name="user"value="${jdbc.user}"/>
<property name="password"value="${jdbc.password}"></property>
</bean >
<bean id="accountService"class="cn.itcast.demo4.AccountServiceImpl">
<property name="accountDao"ref="accountDao"></property>
</bean >
<bean id="accountDao"class="cn.itcast.demo4.AccountDaoImpl">
<!-- 直接注入我们的数据源 -->
<property name="dataSource"ref="dataSource"></property>
</bean >
<!--注册事务管理器 -->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 事务管理器得需要引入我们的连接池即数据源,因为得获得连接,开启事务 -->
<propertyname="dataSource"ref="dataSource"></property>
</bean>
<!--基于注解的方式来完成事务的管理 -->
<tx:annotation-driventransaction-manager="transactionManager"/>
在目标类上加上
@Transactional
publicclassAccountServiceImpl implements AccountService {
0 0
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Spring
- Spring
- spring
- spring
- spring
- Spring
- bootstrap 改变模态弹出框的样式
- 深入浅出ES6(四):模板字符串
- 欢迎使用CSDN-markdown编辑器
- SSH(基于注解)
- java观察者模式
- spring
- POJ 1861 Network 并查集和krusral算法
- 【BZOJ3942】[Usaco2015 Feb]Censoring【AC自动机 / KMP】
- Android Studio导入eclipse工程出现“app:mergeDebugResources“
- jsp/java代码中用jdbc驱动代替jdbc-odbc桥连接SQL Server数据库
- 利用逐次平方法快速求幂
- 网络编程
- Spring面向切面编程
- 深入浅出ES6(五):不定参数和默认参数