zoukankan      html  css  js  c++  java
  • spring 中的两个DaoSupport类的使用对比

    可以利用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:	" ).append( getDeptNo() ).append( "	" );
            buf.append( "dName:	" ).append( getDName() ).append( "	" );
            buf.append( "loc:	" ).append( getLoc() ).append( "	" );
            
            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

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

  • 相关阅读:
    一次线上问题引发的对于C#中相等判断的思考
    Node中的模块引入机制
    Node 各个版本支持ES2015特性的网站
    使用Chrome 中的 ssh 插件登陆 linux 服务器
    vmWare 虚机文件不能启动的事故处理
    JaveScript 中使用 XSLT转换XML文档
    浏览器上的坐标体系相关概念(客户区,页面,屏幕)
    visual Studio 中使用正则表达式来进行查找替换
    JavaScript 执行环境及作用域
    Laravel save部分字段失效的bug问题解决
  • 原文地址:https://www.cnblogs.com/jinhuazhe2013/p/4314585.html
Copyright © 2011-2022 走看看