spring中以jdbc实现查询

BaseDao中 设置JdbcTemplate  属性 。

首先看下BaseDao的java代码

/*    */ import java.io.Serializable;
/*    */ import java.util.List;
/*    */ import java.util.Map;
/*    */ import javax.sql.DataSource;
/*    */ import org.springframework.jdbc.core.JdbcTemplate;
/*    */ 
/*    */ public abstract class BaseDao
/*    */   implements   Serializable
/*    */ {
/*    */   protected DataSource dataSource;
/*    */   protected JdbcTemplate jt;
/*    */ 
/*    */   public Object fetchObject(List list, int row, String fieldName)
/*    */   {
/* 27 */     int n = list.size();
/* 28 */     if ((n < 1) || (row >= n))
/* 29 */       return null;
/* 30 */     Map map = (Map)list.get(row);
/* 31 */     return ((map == null) ? null : map.get(fieldName));
/*    */   }
/*    */ 
/*    */   public Object fetchObject(List list, String fieldName)
/*    */   {
/* 36 */     return fetchObject(list, 0, fieldName);
/*    */   }
/*    */ 
/*    */   public DataSource getDataSource()
/*    */   {
/* 41 */     return this.dataSource;
/*    */   }
/*    */ 
/*    */   public JdbcTemplate getJt()
/*    */   {
/* 50 */     return this.jt;
/*    */   }
/*    */ 
/*    */   public void setDataSource(DataSource dataSource)
/*    */   {
/* 55 */     this.dataSource = dataSource;
/* 56 */     this.jt = new JDBCTemplate(this.dataSource);
/*    */   }
/*    */ }

 然后看下在spring配置文件中如何配置BaseDao

 <bean id="baseDao"
        class="com.nn.BaseDao">
     <property name="dataSource">
      <ref bean="dataSource.mm" />
    </property>       
  </bean>

下面看使用jdbcTemplate的java类如何书写:

public class BizCaseManagerImpl   extends  BaseDao{
	/**
	 * 放款前检查合同是否设置好合同属性 zm 
	 * @param bid
	 * @return true:设置完毕   false:未设置或没有设置完整
	 */
	public  boolean   checkPropSet(int  bid){
	 StringBuffer   sql = new  StringBuffer();
	 sql.append(" select *  from  table(IS_SETACCOUNT(  ")
	 .append(bid)
	 .append("))");
	 List  list = oraNativeSQL.getJdbcTemplate().query(sql.toString(),new RowMapper(){

		public Object mapRow(ResultSet  rs, int arg1) throws SQLException {
			Integer  num = new Integer(rs.getInt(1));
			return num;
		}
		 
	 });
	Integer  i = (Integer)list.get(0);
	 if(i.intValue() >0){
		 return  true;
	 }
	 return  false;
		
	}


}

需要使用JdbcTemplate  可通过两种方式使用。

方法1:

applicationContext.xml配置文件

  <bean id="nativesql"
        class="com.fbms.server.BizCaseManagerImpl">
    <property name="dataSource">
      <ref local="dataSource.mm" />
    </property>
  </bean>

 方法2:

 <bean  id="nativesql"  class="com.fbms.server.BizCaseManagerImpl"  parent="baseDao"></bean>

猜你喜欢

转载自salouhim.iteye.com/blog/1133215