jPetStore 源码分析

本文对jPetStore项目进行了深入的源码分析,重点探讨了DAO层的初始化,如何通过`DaoManager`获取数据库操作对象,并详细介绍了接口`AccountDao`的使用。在DAO层,接口的实现依赖于iBatis的模板方法,如`queryForObject`,配合映射文件中的SQL语句进行数据操作。配置文件如`dao.xml`和`sql-map-config.xml`对于理解jPetStore的数据访问逻辑至关重要,其中事务管理和数据库连接池配置也进行了说明。
摘要由CSDN通过智能技术生成

jPetStore 源码分析

jPetStore 作为ibatis的一个成功实例,此刻我们就来分析下它的结构
下面这个类是dao的初始化类,开始的静态块负责把数据库建立
...

public   class  DaoConfig {

  
private   static   final  String resource  =   " com/ibatis/jpetstore/persistence/dao.xml " ;
  
private   static   final  DaoManager daoManager;

  
static  {
    
try  {
      daoManager 
=  newDaoManager( null );
      Properties props 
=  Resources.getResourceAsProperties( " properties/database.properties " );
      String url 
=  props.getProperty( " url " );
      String driver 
=  props.getProperty( " driver " );
      String username 
=  props.getProperty( " username " );
      String password 
=  props.getProperty( " password " );
      
if  (url.equals( " jdbc:hsqldb:mem:jpetstore " )) {
        Class.forName(driver).newInstance();
        Connection conn 
=  DriverManager.getConnection(url, username, password);
        
try  {
          ScriptRunner runner 
=   new  ScriptRunner(conn,  false false );
          runner.setErrorLogWriter(
null );
          runner.setLogWriter(
null );
          runner.runScript(Resources.getResourceAsReader(
" ddl/hsql/jpetstore-hsqldb-schema.sql " ));
          runner.runScript(Resources.getResourceAsReader(
" ddl/hsql/jpetstore-hsqldb-dataload.sql " ));
        } 
finally  {
          conn.close();
        }
      }
    } 
catch  (Exception e) {
      
throw   new  RuntimeException( " Description.  Cause:  "   +  e, e);
    }

  }

  
public   static  DaoManager getDaoManager() {
    
return  daoManager;
  }

  
public   static  DaoManager newDaoManager(Properties props) {
    
try  {
      Reader reader 
=  Resources.getResourceAsReader(resource);
      
return  DaoManagerBuilder.buildDaoManager(reader, props);
    } 
catch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值