当前位置:   article > 正文

Spring-jdbc-注解_spring jdbc注解有哪些

spring jdbc注解有哪些

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  13. <context:component-scan base-package="cn.itcast.spring0909.jdbc.transaction.annotation"></context:component-scan>
  14. <!--
  15. dataSource
  16. -->
  17. <bean
  18. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  19. <property name="locations">
  20. <value>classpath:jdbc.properties</value>
  21. </property>
  22. </bean>
  23. <bean id="dataSource" destroy-method="close"
  24. class="org.apache.commons.dbcp.BasicDataSource">
  25. <property name="driverClassName" value="${jdbc.driverClassName}" />
  26. <property name="url" value="${jdbc.url}" />
  27. <property name="username" value="${jdbc.username}" />
  28. <!-- <property name="password" value="${jdbc.password}" /> -->
  29. </bean>
  30. <!--
  31. jdbcTemplate
  32. -->
  33. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  34. <property name="dataSource">
  35. <ref bean="dataSource"/>
  36. </property>
  37. </bean>
  38. <!--
  39. 事务管理器
  40. -->
  41. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  42. <property name="dataSource">
  43. <ref bean="dataSource"/>
  44. </property>
  45. </bean>
  46. <!--
  47. 启动事务的注解解析器
  48. -->
  49. <tx:annotation-driven transaction-manager="transactionManager"/>
  50. </beans>

  1. public interface PersonDao {
  2. public void savePerson();
  3. }

  1. @Repository("personDao")
  2. public class PersonDaoImpl implements PersonDao{
  3. @Resource(name="jdbcTemplate")
  4. //这里这能保存jdbcTemplate变量的方式不能继承jdbcSupport
  5. private JdbcTemplate jdbcTemplate;
  6. @Override
  7. public void savePerson() {
  8. // TODO Auto-generated method stub
  9. this.jdbcTemplate.execute("insert into person(pname) values('aa')");
  10. int a = 1/0;
  11. this.jdbcTemplate.execute("insert into person(pname) values('aa')");
  12. }
  13. }

  1. public interface PersonService {
  2. public void savePerson();
  3. }

  1. @Service("personService")
  2. public class PersonServiceImpl implements PersonService{
  3. @Resource(name="personDao")
  4. private PersonDao personDao;
  5. /**
  6. * 注解的粒度比xml要细
  7. */
  8. @Transactional(readOnly=false)//这才是重点
  9. public void savePerson() {
  10. // TODO Auto-generated method stub
  11. this.personDao.savePerson();
  12. }
  13. }

上面的代码才是重点

  1. public class PersonTest extends SpringHelper{
  2. static{
  3. path = "cn/itcast/spring0909/jdbc/transaction/annotation/applicationContext.xml";
  4. }
  5. @Test
  6. public void test(){
  7. PersonService personService = (PersonService)context.getBean("personService");
  8. personService.savePerson();
  9. }
  10. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/576771
推荐阅读
相关标签
  

闽ICP备14008679号