Ibatis在项目中怎么使用的

 Spring中配置:
  
  <!--定义dataSource -->
  	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@10.101.2.12:1521:appora</value></property>
        <property name="username"><value>authority</value></property>
        <property name="password"><value>authority</value></property>
    </bean>
    
    <!--定义sqlMap配置文件及数据源使用 -->
     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <!--  <property name="configLocation" value="com/eclink/mapping/sqlMapConfig.xml"/>-->
        <property name="configLocation" value="classpath:/com/eclink/mapping/sqlMapConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <!-- <property name="lobHandler" ref="lobHandler"/> -->
    </bean>
    
		<!--dao注入sqlMapClient -->
     <bean id="firmDao"
          class="com.eclink.dao.authentication.FirmDAO"
          abstract="false"  lazy-init="default"
          autowire="default" dependency-check="default">
        <property name="sqlMapClient">
            <ref bean="sqlMapClient"></ref>
        </property>
    </bean> 
    
    <!--service注入dao -->
    <bean id="firmService"
   	class="com.eclink.service.authentication.impl.FirmServiceImpl"
   	abstract="false" lazy-init="default"
   	autowire="default" dependency-check="default">
		   	<property name="firmDao">
		   		<ref bean="firmDao"></ref>
		   	</property>
		   	 <property name="roleDao">
		   		<ref bean="roleDao"></ref>
		   	</property>
		   	<property name="userService">
		   		<ref bean="userService"></ref>
		   	</property>
		    <property name="roleService">
		   		<ref bean="roleService"></ref>
		   	</property> 
		    <property name="employeeDao">
		   		<ref bean="employeeDao"></ref>
		   	</property> 		
   </bean>
   
   使用:
   在FirmService中定义FirmDao属性并有方法
   public Organization getOrgById(int id)throws Exception {
		Organization firm =null;
		try{
			firm =firmDao.getOrgById(id);
		}catch(Exception e){
			log.error("Error in getOrgById",e);
			throw e ;
		}
		return firm;
	}
	
	在FirmDao中继承Spring.jar中的SqlMapClientDaoSupport,实际上通过getSqlMapClient()方法返回sqlMapClient.
	public class FirmDAO  extends SqlMapClientDaoSupport{
	
		public Organization getOrgById(Integer firmId)throws Exception{
			try{
				return (Organization)getSqlMapClient().queryForObject("authentication.getOrgById",firmId);
			}catch(SQLException e){
				log.error("Error occur in the method getOrgById:"+e) ;
				throw e ;
			}
		}
	}
   
   
   注意:sqlMap配置文件如下:引入authentication_sqlmap.xml了
   <sqlMapConfig>	    
	    <sqlMap resource="com/eclink/mapping/authentication_sqlmap.xml"/>
	 </sqlMapConfig>
   
   在authentication_sqlmap.xml中定义authentication.getOrgById的语句如下:
   <select id="authentication.getOrgById" resultMap="OrgInfo" parameterClass="int">
	   SELECT ID, PARENT_ORG, ORG_NAME, ORG_TYPE, NAME_CN, NAME_EN, LINKMAN, TELEPHONE, FAX, 
	   ADDRESS, REGISTER_DATE, ORG_CODE,IS_SYSTEM FROM UT_ORGANIZATION where id=#value#
  </select>	 
  
  在ibatis配置文件中:
  定义:
  <typeAlias alias="Organization" type="com.eclink.vo.authentication.Organization"/>
  和
   <resultMap id="OrgInfo" class="Organization">
		 <result property="id" column="id"/>
		 <result property="orgCode" column="ORG_CODE"/>
		 <result property="nameEn" column="NAME_EN"/>
		 <result property="nameCn" column="NAME_CN"/>
		 <result property="orgName" column="ORG_NAME"/> 
		 <result property="linkMan" column="LINKMAN"/>
		 <result property="telephone" column="TELEPHONE"/>
		 <result property="fax" column="FAX"/>
		 <result property="address" column="ADDRESS"/>
		 <result property="registerDate" column="REGISTER_DATE" javaType="date"/>
		 <result property="parentId" column="PARENT_ORG"/>
		 <result property="orgType" column="ORG_TYPE"/>
		 <result property="isSystem" column="IS_SYSTEM"/>
	</resultMap>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值