spring NamedParameterJdbcDaoSupport query相对于JdbcDaoSupport更加方便

可以利用NamedParameterJdbcDaoSupport 已经封装的NamedParameterJdbcTemplate方便的进行sql中参数的初始化工作。

相对于JdbcDaoSupport中封装的JdbcTemplate更加好用。

1.Dept

package cn.hxex.springcore.jdbc;

public class Dept {

    private Integer deptNo;
    private String dName;
    private String loc;
    
    public Integer getDeptNo() {
        return deptNo;
    }
    public void setDeptNo(Integer deptNo) {
        this.deptNo = deptNo;
    }
    public String getDName() {
        return dName;
    }
    public void setDName(String name) {
        dName = name;
    }
    public String getLoc() {
        return loc;
    }
    public void setLoc(String loc) {
        this.loc = loc;
    }
    
    public String toString() {
        StringBuffer buf = new StringBuffer();
        
        buf.append( "deptNo:\t" ).append( getDeptNo() ).append( "\t" );
        buf.append( "dName:\t" ).append( getDName() ).append( "\t" );
        buf.append( "loc:\t" ).append( getLoc() ).append( "\t" );
        
        return buf.toString();
    }
}

2.IDeptDao

package cn.hxex.springcore.jdbc;

import java.util.List;

public interface IDeptDao {

    public List getDepts();
    
    public List getDeptsByNo(int deptno);
}

3.DeptNamedDaoImp

package cn.hxex.springcore.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

public class DeptNamedDaoImp extends NamedParameterJdbcDaoSupport implements IDeptDao{

    @Override
    public List getDepts() {
        String sql = "SELECT deptno, dname, loc FROM dept";
        return super.getNamedParameterJdbcTemplate().query( sql, new HashMap(),  new DeptRowMapper() );
    }

    @Override
    public List getDeptsByNo(final int deptno) {
        final String sql = "SELECT deptno, dname, loc FROM dept where deptno=:deptno";
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("deptno", deptno);
        return super.getNamedParameterJdbcTemplate().query( sql, paramMap,  new DeptRowMapper() );
    }
    
    class DeptRowMapper implements RowMapper {

        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            Dept dept = new Dept();
            dept.setDeptNo( new Integer( rs.getString(1) ) );
            dept.setDName( rs.getString( 2 ) );
            dept.setLoc( rs.getString( 3 ) );
            return dept;
        }
        
    }

}

4.JdbcNamedDaoSupportMain

package cn.hxex.springcore.jdbc;

import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class JdbcNamedDaoSupportMain {

    public static void main(String[] args) {
        
        ClassPathResource resource = new ClassPathResource("cn/hxex/springcore/jdbc/JdbcNamedDaoSupportBeans.xml");
        BeanFactory factory = new XmlBeanFactory(resource);

        IDeptDao dao = (IDeptDao)factory.getBean( "deptDao" );
        //List depts = dao.getDepts();
        List depts = dao.getDeptsByNo(10);
        
        for( int i=0; i<depts.size(); i++ ) {
            System.out.println( depts.get( i ) );
        }
    }
}

5.JdbcNamedDaoSupportBeans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.0.xsd">

    <!--  初始化数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
      <property name="url">
        <value>jdbc:oracle:thin:@localhost:1521:XE</value>
      </property>
      <property name="username"><value>jeffrey</value></property>
      <property name="password"><value>jeffrey</value></property>
    </bean>
    
    <bean id="deptDao" class="cn.hxex.springcore.jdbc.DeptNamedDaoImp">
        <property name="dataSource" ref="dataSource" />    
    </bean>
</beans>

 

spring参考文档:http://docs.spring.io/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html

备注:以上部分代码来自陈天河编著的某书

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。