日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring jdbc Template和Spring 事务管理

發(fā)布時(shí)間:2025/3/15 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring jdbc Template和Spring 事务管理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用jdbcTemplate完成增刪改查操作(重點(diǎn))

package com.it.jdbctemplate;import java.util.List;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.it.domain.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class JdbcTemplateTest3 {@Autowiredprivate JdbcTemplate jdbcTemplate;//修改 @Testpublic void test1(){jdbcTemplate.execute("update t_user set sex='男' where id=2");}//添加操作 @Testpublic void test2(){jdbcTemplate.execute("insert into t_user values(null,'趙六',20,'女')");}//刪除操作 @Testpublic void test3(){jdbcTemplate.execute("delete from t_user where id =5");}//測(cè)試返回簡(jiǎn)單數(shù)據(jù)類型 @Testpublic void test4(){String name = jdbcTemplate.queryForObject("select name from t_user where id=?", String.class,1);System.out.println(name);}//測(cè)試返回簡(jiǎn)單數(shù)據(jù)類型 @Testpublic void test5(){Integer count = jdbcTemplate.queryForObject("select count(*) from t_user",Integer.class);System.out.println(count);}//使用BeanPropertyRowMapper @Testpublic void test6(){ // User user = jdbcTemplate.queryForObject("select * from t_user where id=?", // new BeanPropertyRowMapper<User>(User.class),2); List<User> user = jdbcTemplate.query("select * from t_user", new BeanPropertyRowMapper<User>(User.class));System.out.println(user);}}

?


掌握數(shù)據(jù)庫(kù)連接池的使用和配置(重點(diǎn))

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--引入外部的properties文件 --><context:property-placeholder location="classpath:db.properties"/><!-- <bean id="driveManageDatoSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///springtest"></property><property name="username" value="root"></property><property name="password" value="123"></property></bean> --><!-- 設(shè)置c3p0連接池 --><bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="c3p0DataSource"></property></bean></beans> jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///springtest jdbc.username=root jdbc.password=123

  


3.掌握事務(wù)的傳播行為(重點(diǎn))

Spring事務(wù)管理機(jī)制

3.1PlatformTransactionManager(平臺(tái)事務(wù)管理器)

3.2TransactionDefinition(事務(wù)的定義信息)

傳播 :它解決的是兩個(gè)被事務(wù)管理的方法互相調(diào)用問題。它與數(shù)據(jù)庫(kù)沒有關(guān)系,是程序內(nèi)部維護(hù)的問題

propagation required(傳播請(qǐng)求) : ?默認(rèn)值 兩個(gè)操作處于同一個(gè)事務(wù),如果之前沒有事務(wù),新建一個(gè)事務(wù)

propagation requires new (傳播新請(qǐng)求) : 兩個(gè)操作處于不同的事務(wù)

propagtion nested ?: 它是一種嵌套事務(wù),它是使用SavePoint來實(shí)現(xiàn)的。事務(wù)回滾時(shí)可以回滾到指定的 savepoint

注意:它只對(duì)DataSourceTransactionManager有作用

3.2TransactionStatus(事務(wù)狀態(tài)信息)

  

4.了解基于xml配置聲明式事務(wù)管理(了解)

<!--創(chuàng)建事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0DataSource"></property></bean><!--配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="account" /></tx:attributes></tx:advice><!--配置切面 --><aop:config><aop:pointcut expression="execution(* com.it.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>

?


5.掌握基于annotation聲明式事務(wù)管理(重點(diǎn))

@Transactional()public void account(String outname, String inname, double money) {//轉(zhuǎn)出操作 accountDao.accountOut(outname, money);System.out.println(10/0);//一定會(huì)出現(xiàn)異常//轉(zhuǎn)入操作 accountDao.accountIn(inname, money);}<tx:annotation-driven transaction-manager="transactionManager"/>

?



? ??

轉(zhuǎn)載于:https://www.cnblogs.com/weihaiyang/p/7113546.html

總結(jié)

以上是生活随笔為你收集整理的Spring jdbc Template和Spring 事务管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。