1:用到的jar(下面的目录是相对于spring2.5解压后的目录)
/dist/spring.jar
/lib/jakarta-comons/commons-logging.jar
/lib/jakarta-comons/common-pools.jar
/lib/jakarta-comons/commons-dbcp.jar
/lib/aspectj/aspectjrt.jar
/lib/aspectj/jweaver.jar
/lib/cjlib/cglib-nodep-z.1_3.jar
/lib/j2ee/common-annotations.jar
外加上mysql的数据库驱动
2:新建工程
业务接口:
import java.util.List;
import bean.Su;
public interface SuService {
public void save(Su s);
public void update(Su s);
public Su querySuById(Integer id);
public List<Su> queryAll();
public void deleteSu(Integer id);
}
业务接口实现:
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import bean.Su;
import service.SuService;
//下面两条为spring事务管理的默认情况
//当方法开始时开始事务,方法结束时事务结束。【unchecked】运行期例外事务回滚。
//1:【checked】方法里面thorws new Exception,方法抛出。外面调用时捕获,不回滚。
@Transactional
public class SuServiceImpl implements SuService {
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedJdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
//配置该属性后,出现上面1checked情况时也会回滚
@Transactional(rollbackFor=RuntimeException.class)
//@Transactional(noRollbackFor=RuntimeException.class)//不会回滚
public void deleteSu(Integer id) {
jdbcTemplate.update("delete su where id=? ",
new Object[] {id},
new int[] { java.sql.Types.INTEGER });
}
@SuppressWarnings("unchecked")
public List<Su> queryAll() {
System.out.println(this.simpleJdbcTemplate+"::::::::::::::::");
System.out.println(this.namedJdbcTemplate+"::::::::::::::::");
this.simpleJdbcTemplate.update("update su set message=? where id=?", "heshaohui",2);
throw new RuntimeException();
// return (List<Su>)jdbcTemplate.query("select * from su",new SuRowMapper());
}
public Su querySuById(Integer id) {
return (Su)jdbcTemplate.queryForObject("select * from su where id=?", new Object[]{id},new SuRowMapper());
}
//配置该属性后,不开启事务。因为开启事务对性能会有一定的影响
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public void save(Su s) {
jdbcTemplate.update("insert into su(message) value(?)",
new Object[] { s.getMessage() },
new int[] { java.sql.Types.VARCHAR });
}
public void update(Su s) {
jdbcTemplate.update("upate su set message=? where id=?",
new Object[] { s.getMessage() ,s.getId()},
new int[] { java.sql.Types.VARCHAR,java.sql.Types.INTEGER });
}
public void setNamedJdbcTemplate(NamedParameterJdbcTemplate namedJdbcTemplate) {
this.namedJdbcTemplate = namedJdbcTemplate;
}
public void setSimpleJdbcTemplate(SimpleJdbcTemplate simpleJdbcTemplate) {
this.simpleJdbcTemplate = simpleJdbcTemplate;
}
}
bean类:
public class Su {
private Integer id;
private String message;
public Su(){}
public Su(String message){this.message=message;}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
测试:
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Su;
import service.SuService;
public class SuTest {
private static SuService suService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext cx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
suService=(SuService) cx.getBean("suService");
}
@Test public void save(){
suService.save(new Su("贺少辉"));
System.out.println(suService.queryAll().size());
}
}
配置文件:
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 加载数据库的属性文件:两种方式,1:直接把value的值写在dataSource中对应的属性处
2:写在jdbc.properties文件中,加上下面这句代码 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 连接池启动时的初始值,一般两三条就够了 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将用不到的连接释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最小空闲值,当空闲的连接数小于阀值时,连接池就会预申请一些连接,以免峰值到时来不及申请-->
<property name="minIdle" value="${jdbc.minIdle}"></property>
</bean>
<!-- 配置事务bean -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 封装了JdbcTemplate,在SQL语句中增加了命名参数的支持。 -->
<bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<!-- JdbcTemplate的包装类,利用了JAVA5的一些特性,如泛型。 -->
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<!-- 将事务交与spring容器管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="suService" class="service.imp.SuServiceImpl">
<!--下面这个配的是jdbcTemplate,注意下的set方法的写法。。。 -->
<property name="dataSource" ref="dataSource"></property>
<property name="simpleJdbcTemplate" ref="simpleJdbcTemplate"></property>
<property name="namedJdbcTemplate" ref="namedJdbcTemplate"></property>
</bean>
</beans>
到此结束。