iBatis2使用Properties文件配置
 
iBatis的xml配置文件对行的先后顺序有严格要求。假设要将在外部配置JDBC的连接属性,那么需要这样配置:
 
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.87.30.32:3306/tdmc_leizm
jdbc.user=root
jdbc.password=123456
 
sqlMapConfig.xml
xml version ="1.0" encoding ="GBK" ?>
                "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
< sqlMapConfig >
  
         < properties resource ="jdbc.properties" />

         < settings cacheModelsEnabled ="true" enhancementEnabled ="true"
                             lazyLoadingEnabled ="true" maxRequests ="512" maxSessions ="128"
                             maxTransactions ="32" useStatementNamespaces ="true" />
        
  
         < transactionManager type ="JDBC" >
                 < dataSource type ="SIMPLE" >
                         < property name ="JDBC.Driver" value ="${jdbc.driver}" />
                         < property name ="JDBC.ConnectionURL" value ="${jdbc.url}" />
                         < property name ="JDBC.Username" value ="${jdbc.user}" />
                         < property name ="JDBC.Password" value ="${jdbc.password}" />
                 dataSource >
         transactionManager >

         < sqlMap resource ="com/asiainfo/tdmc/entity/sqlmap/Cfg_code.xml" />

sqlMapConfig >
 
引入的配置文件必须写在第一行。
 
上面的配置与下面给出的配置效果是完全一样的,不使用外部配置文件的写法:
 
xml version ="1.0" encoding ="GBK" ?>
                "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
< sqlMapConfig >
         < settings cacheModelsEnabled ="true" enhancementEnabled ="true"
                             lazyLoadingEnabled ="true" maxRequests ="512" maxSessions ="128"
                             maxTransactions ="32" useStatementNamespaces ="true" />
        
         < transactionManager type ="JDBC" >
                 < dataSource type ="SIMPLE" >
                         < property name ="JDBC.Driver" value ="com.mysql.jdbc.Driver" />
                         < property name ="JDBC.ConnectionURL" value ="jdbc:mysql://10.87.30.32:3306/tdmc_leizm" />
                         < property name ="JDBC.Username" value ="root" />
                         < property name ="JDBC.Password" value ="123456" />
                 dataSource >
         transactionManager >

         < sqlMap resource ="com/asiainfo/tdmc/entity/sqlmap/Cfg_code.xml" />

sqlMapConfig >
 
有关settings的配置:
         < settings
                
                cacheModelsEnabled="true"
                
                enhancementEnabled="true"
                
                lazyLoadingEnabled="true"
                
                maxRequests="512"
                
                maxSessions="128"
                
                maxTransactions="32"
                
                useStatementNamespaces="true"/>