国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

spring對jdbc事務控制

2019-11-06 06:21:55
字體:
來源:轉載
供稿:網友

pojo類

package com.pactera.pojo;public class User { public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } PRivate String name; private int age; @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; }}

IUserDao

package com.pactera.dao;import com.pactera.pojo.User;public interface IUserDao { void save(User user);}

UserDao

package com.pactera.daoImpl;import javax.annotation.Resource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import com.pactera.dao.IUserDao;import com.pactera.pojo.User;@Repositorypublic class UserDao implements IUserDao{ @Resource private JdbcTemplate jdbcTemplate; @Override public void save(User user) { String sql = "insert into user(name,age) values(?,?)"; jdbcTemplate.update(sql,user.getName(),user.getAge()); }}

IUserService

package com.pactera.service;import com.pactera.pojo.User;public interface IUserService { void save(User user);}

UserService

package com.pactera.serviceImpl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.pactera.dao.IUserDao;import com.pactera.pojo.User;import com.pactera.service.IUserService;@Servicepublic class UserService implements IUserService{ @Resource private IUserDao userDao; @Override public void save(User user) { userDao.save(user); int i=1/0; userDao.save(user); }}

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:MySQL://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8"> </property> <property name="user" value="root"></property> <property name="passWord" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="50"></property> <property name="acquireIncrement" value="5"></property> </bean> <!-- 創建jdbcTemplate對象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟注解掃描 --> <context:component-scan base-package="com.pactera"></context:component-scan> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 聲明式事務 --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="query" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- aop事務切面 --> <aop:config> <aop:pointcut id="myPointcut" expression="execution(* com.pactera.service.*.*(..))" /> <!--將定義好的事務處理策略應用到上述的切入點 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> </aop:config></beans>

測試

private IUserService userService; @Before public void init(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); this.userService = ac.getBean("userService",IUserService.class); } @Test public void test(){ User user = new User(); user.setName("hello"); user.setAge(15); userService.save(user); }

配 有jdbc事務控制的時候service層中出現異常,再拋出異常之前對數據庫的操作會回滾


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚顺县| 汨罗市| 淄博市| 攀枝花市| 凤凰县| 克东县| 犍为县| 汪清县| 讷河市| 和田市| 田阳县| 汝州市| 鄢陵县| 久治县| 蓝山县| 澳门| 高邑县| 五指山市| 民权县| 岑巩县| 磴口县| 黎城县| 金门县| 息烽县| 长垣县| 滦平县| 吉木萨尔县| 清远市| 富民县| 通许县| 沛县| 京山县| 韩城市| 临沭县| 饶河县| 秀山| 安龙县| 沿河| 若尔盖县| 吴旗县| 竹山县|