首页 > 数据库技术 > 详细

Day3:Spring-JDBC、事务管理

时间:2014-01-20 19:37:35      阅读:437      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
jdbc的编程:
 *  获取链接  

 *   Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,username,password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeUpdate(sql)/executeQuery(sql); List<Person> persoList = new ArrayList<Person>(); while(rs.next()){ Person person = new Person(); person.setPid(rs.getLong("pid")); person.setPname(rs.getString("pname")); personList.add(person); } * jdbc的编程特点: 固定代码+可变参数=模板模式
bubuko.com,布布扣
bubuko.com,布布扣
Spring配置数据库的连接池有两种方式
第一种方式
<bean id="dataSource" destroy-method="close"                
    class="org.apache.commons.dbcp.BasicDataSource">   <property name="driverClassName" value="com.mysql.jdbc.Driver" />   <property name="url" value="jdbc:mysql://localhost:3306/vijay" />   <property name="username" value="root" />   <property name="password" value="root" /> </bean> 第二种方式 * 导入属性文件   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     <property name="locations">
      <value>classpath:com/foo/jdbc.properties</value>     </property>   </bean> * <bean id="dataSource1" destroy-method="close"
      class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName" value="${driverClassName}" />     <property name="url" value="${url}" />     <property name="username" value="${username}"/>     <property name="password" value="${password}"/>   </bean>
bubuko.com,布布扣
bubuko.com,布布扣
spring声明式事务处理配置文件的思路
<aop:config>
  //切入点表达式
      表达式应该把目标类包括进去
  //通知
  <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
bubuko.com,布布扣
通知:
<tx:advice id="tx" transaction-manager="transactionManager"
  >
   <tx:attributes>
      <tx:method name="save*" propagation="REQUIRED"/>
   </tx:attributes>
</tx>
bubuko.com,布布扣
bubuko.com,布布扣
事务管理器:
<bean id="transactionManager"
   class="DataSourceTransactionManager">
    <property name="dataSource">
       <ref bean="dataSource"/>
      </property>
</bean>
bubuko.com,布布扣
bubuko.com,布布扣

bubuko.com,布布扣
程序员做的事情
bubuko.com,布布扣
注入service
 <bean id="personService" class="PersonServiceImpl">
     <property name="personDao">
        <ref bean="personDao"/>
     </property>

 </bean>
bubuko.com,布布扣
注入dao
<bean id="personDao" class="PersonDaoImpl">
   <property name="dataSource">
        <ref bean="dataSource"/>
    </property>
</bean>
bubuko.com,布布扣
dataSource:
  *  导入properties文件
   <bean
  class="PropertyPlaceholderConfigurer">
  <property name="locations">
   <value>
classpath:cn/itcast/spring0401/jdbc/transaction/xml/jdbc.properties
   </value>
  </property>
 </bean>
 *  <bean id="dataSource"..>
  
bubuko.com,布布扣

 

bubuko.com,布布扣

Day3:Spring-JDBC、事务管理

原文:http://www.cnblogs.com/vijay/p/3526780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!