• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
土上方方
博客园    首页    新随笔    联系   管理    订阅  订阅

Spring jdbcTemplate

spring JdbcTemplate 事务管理 

spring的JDBC框架能够承担资源管理和异常处理的工作,从而简化我们的JDBC代码, 让我们只需编写从数据库读写数据所必需的代码。Spring把数据访问的样板代码隐藏到模板类之下, 
结合Spring的事务管理,可以大大简化我们的代码.

Spring提供了对事务的声明式事务管理,只需要在配置文件中做一些配置,即可把操作纳入到事务管理当中,解除了和代码的耦合。

  Spring声明式事务管理,核心实现就是基于Aop。

  Spring声明式事务管理是粗粒度的事务控制,只能给整个应用方法事务,不可以对方法的某几行应用事务。

     Spring声明式事务管理器类:

               Jdbc技术:DataSourceTransactionManager

               hibernate技术:HibernateTransactionManager

先创建一个实体类的包,并在该包中创建Account实体类,写上属性并将其进行get,set封装
public class Account {
    private int  aid;
    private  String aname;
    private  Double balance;
同样在该包中创建Stock实体类,写上该类的属性值,并将其进行get,set封装
public class Stock {
        private  int  sid;
        private  String sname;
        private  int count;
创建StoreException类,并继承异常类,并将该类的名称进行有参无参构造
public class StoreException extends  Exception {

    public StoreException() {
        super();
    }

    public StoreException(String message){
        super(message);
    }
}
创建dao包,并在该包中创建AccountDao接口,并写上接口的实现方法
public interface AccountDao {
    //要进行购买操作  银行账户
public  boolean  updateAccount(int aid,double money,boolean isBuy);
}
在dao层中创建AccountDaoImpl接口实现类,并且继承JdbcDaoSupport父类,并实现接口方法 
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    public boolean updateAccount(int aid, double money, boolean isBuy) {
        boolean flg=false;
        String sql=null;
        if(isBuy){
               //购买股票
            sql="update account set balance=balance-? where aid=?";
        }else {
            sql="update account set balance=balance+? where aid=?";
        }
           //对数据进行增删改,都用update()
         int  count=this.getJdbcTemplate().update(sql,money,aid);
           if(count>0){
               flg=true;
           }
        return flg;
    }
}
同样在dao层中创建SackDao接口,并写上接口的实现方法
public interface SaockDao {
    //要进行购买操作  银行账户
public  boolean  updateSock(int sid,double count,boolean isBuy);
}
如上述所示,在dao层中创建SaockDaoImpl接口实现类,并且继承JdbcDaoSupport父类,并实现接口方法 
public class SaockDaoImpl extends JdbcDaoSupport implements SaockDao {
    public boolean updateSock(int sid, double count, boolean isBuy) {
        boolean   fl=false;
        String  sql=null;
        if(isBuy){
            sql="update stock set count=count+? where sid=?";
        }else {
            sql="update stock set count=count-? where sid=?";
        }
        int  pl=this.getJdbcTemplate().update(sql,count,sid);
        if(pl>0){
            fl=true;
        }
        return fl;
    }
}
之后创建service层,并在该层中创建AccountService接口,并写上接口的实现方法
public interface AccountService {
      public  void  buySock(int sid,int count,int aid,double money);
}
同样在service包中,创建接口实现类并继承AccountService接口,实现该接口中的方法,并将dao包中的接口重新植入,将其进行封装
public class AccountServiceImpl implements  AccountService{
    private AccountDao  accountDao;
    private SaockDao saockDao;
    public void buySock(int sid, int count, int aid, double money){
        boolean  isBuy=true;  //购买股票
accountDao.updateAccount(aid,money,isBuy);
        //异常
if(1==1){
           try {
               throw new cn.happy.SpringJDBCTemplateSW.entity.StoreException();
           } catch (cn.happy.SpringJDBCTemplateSW.entity.StoreException e) {
               e.printStackTrace();
           }
       }

        saockDao.updateSock(sid,count,isBuy);
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public SaockDao getSaockDao() {
        return saockDao;
    }

    public void setSaockDao(SaockDao saockDao) {
        this.saockDao = saockDao;
    }
}
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///bookstok
jdbc.user=sha
jdbc.password=sha
applicationContestSpringSW.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
      ">

    <!--识别jdbc.properties文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <!--创建数据源 Spring -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>    <!--dao配置-->
<bean id="AccountDaoImpl" class="cn.happy.SpringJDBCTemplateSW.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="SaockDaoImpl" class="cn.happy.SpringJDBCTemplateSW.dao.SaockDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
     <!--service accuntservice-->
<bean id="accuntservice" class="cn.happy.SpringJDBCTemplateSW.service.AccountServiceImpl">
         <property name="accountDao" ref="AccountDaoImpl"></property>
         <property name="saockDao" ref="SaockDaoImpl"></property>
     </bean>
    <!--事务管理器-->
<bean id="tranasationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务,拦截业务方法-->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!--目标类型-->
<property name="target" ref="accuntservice"></property>
        <property name="transactionManager" ref="tranasationManager"></property>
        <!--增强-->
<property name="transactionAttributes">
            <props>
                <prop key="buy*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-StockException</prop>
            </props>
        </property>
    </bean>
</beans>
测试类:
public class SpringJDBCTemplateSWTest {
    @Test
    public  void  testTx(){
        ApplicationContext  appl=new ClassPathXmlApplicationContext("applicationContestSpringSW.xml");
        AccountService  acc= (AccountService) appl.getBean("accountServiceProxy");
      try {
          acc.buySock(1,12,1,200);
      }catch (StoreException e){
          e.printStackTrace();
      }
    }
}
posted @ 2017-08-20 09:03  土上方方  阅读(271)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3