首先配置config.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mytest1</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean = "dataSource"/>
</property>
</bean>
<bean id="helloDao" class="com.gc.action.HelloDao">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
<property name="transactionManager">
<ref bean = "transactionManager"/>
</property>
<property name="sql">
<value>insert into tb1 values(101,'gf','nihao')</value>
</property>
</bean>
再是实现的helloDao类
package com.gc.action;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
public class HelloDao {
//private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
private PlatformTransactionManager transactionManager;
private String sql;
public void setSql(String sql) {
this.sql = sql;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void create(String name){
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
try {
jdbcTemplate.update(this.sql);
} catch (DataAccessException ex) {
transactionManager.rollback(status);
throw ex;
}finally{
transactionManager.commit(status);
}
System.out.println("*************************");
}
}
最后测试类:
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.action.HelloDao;
import com.gc.impl.FinanceInterface;
import com.gc.impl.TimeBookInterface;
public class testExample {
public static void main(String[] args) throws InstantiationException,IllegalAccessException,
ClassNotFoundException{
String path = "D:\\tianWorkPlace\\5_2\\WebContent\\config.xml";
ApplicationContext actx = new FileSystemXmlApplicationContext(path);
// FinanceInterface financeProxy = (FinanceInterface) actx.getBean("logProxy");
// financeProxy.doCheck("李四");
HelloDao hello = (HelloDao) actx.getBean("helloDao");
//timeBookProxy.doCheck("zhang San");
hello.create("zhang san");
}
}