数据库(ORACLE)查出数据封装到实体

说明:

1、对查出的数据做相应的转换,使用视图实现转换

2、转换的目的是为了处理一些比较复杂的数据类型,比如DATA,TIMESTRAMP等,把这些通过类型转为varchar型,方便封装到实体

案例代码:

String sql1 = "select * from v_ab01_upload where aaz800='0' and aaz400="
                        + aaz400
                        + " and aaa322="
                        + aaa322
                        + " and aab001='"
                        + aab001 + "' order by xh";
                List listab01_upload = DbUtils.getList(sql1,
                        V_AB01_UPLOAD.class);
                V_AB01_UPLOAD jcxx = new V_AB01_UPLOAD();
                for (int p = 0; p < listab01_upload.size(); p++) {
                    jcxx = (V_AB01_UPLOAD) listab01_upload.get(p);
}

工具类:

public class DbUtils {
	private static DataSourceFactory dsf = (DataSourceFactory) IocBeanUtil.getBean("dataSourceTarget");

    private static String driverClass = "oracle.jdbc.driver.OracleDriver";
    //没什么好说的,获取数据库连接
    public static Connection getConn(){
        Connection conn = null;
        try {
            Class.forName(driverClass);
            conn = getTemplate().getDataSource().getConnection();;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    /*
     * 将rs结果转换成对象列表
     * @param rs jdbc结果集
     * @param clazz 对象的映射类
     * return 封装了对象的结果列表
     */
    public static List populate(ResultSet rs , Class clazz) throws NoSuchMethodException, SecurityException, Exception{
        //结果集的元素对象 
        ResultSetMetaData rsmd = rs.getMetaData();
        //获取结果集的元素个数
        int count = rsmd.getColumnCount();
        String[] colNames = new String[count];
        for (int i = 1; i <= count; i++) {
            colNames[i - 1] = rsmd.getColumnName(i);
        }

//         System.out.println("#");
//         for(int i = 1;i<=colCount;i++){
//             System.out.println(rsmd.getColumnName(i));
//             System.out.println(rsmd.getColumnClassName(i));
//             System.out.println("#");
//         }
         //返回结果的列表集合
         List list = new ArrayList();
         //业务对象的属性数组
         Field[] fields = clazz.getDeclaredFields();
         while(rs.next()){//对每一条记录进行操作
             Object obj = clazz.newInstance();//构造业务对象实体
             //将每一个字段取出进行赋值
//             for(int i = 1;i<=colNames.length;i++){
//            	 String colName =colNames[i];
//                 System.out.println("colName:="+colName);
                 //寻找该列对应的对象属性
                 for(int j=0;j<fields.length;j++){
                	 Object value =null;
                 
                     Field f = fields[j];
                     System.out.println("Name:="+f.getName());
                     for(int i = 0;i<colNames.length;i++){
                    	 String colName =colNames[i];
                         System.out.println("colName:="+colName);
                         System.out.println("Name:="+f.getName());
                        if(f.getName().equals(colName)){
                        	value=rs.getObject(f.getName());
                        	
                        	break;
                        } 
                     } 
                     	
                              

                     if(value==null){
                    	 value="";
                     }
     				System.out.println(f.getGenericType());//打印该类的所有属性类型
     				 
    				// 如果类型是String
    				if (f.getGenericType().toString().equals(
    						"class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
    					// 拿到该属性的gettet方法

     
    					value = (String)((value==null?"":value)).toString();// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("String type:" + value);
    					}
     
    				}
    				// 如果类型是Long
    				if (f.getGenericType().toString().equals(
    						"class java.lang.Long")) { // 如果type是类类型,则前面包含"class ",后面跟类名
    					// 拿到该属性的gettet方法

                         if(value==""){
                        	 value=null;
                         }else{
                        	 value = Long.valueOf(value.toString());// 调用getter方法获取属性值
                         }
    					
    					if (value != null) {
    						System.out.println("Long type:" + value);
    					}
     
    				}
    				// 如果类型是Integer
    				if (f.getGenericType().toString().equals(
    						"class java.lang.Integer")) {

    					value = (Integer)value;// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("Integer type:" + value);
    					}
    				}
     
    				// 如果类型是Double
    				if (f.getGenericType().toString().equals(
    						"class java.lang.Double")) {

    					value = (Double)value;// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("Double type:" + value);
    					}
    				}
     
    				// 如果类型是Boolean 是封装类
    				if (f.getGenericType().toString().equals(
    						"class java.lang.Boolean")) {

    					value = (Boolean)value;// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("Boolean type:" + value);
    					}
    				}
     
    				// 如果类型是boolean 基本数据类型不一样 这里有点说名如果定义名是 isXXX的 那就全都是isXXX的
    				// 反射找不到getter的具体名
    				if (f.getGenericType().toString().equals("boolean")) {

    					value = (Boolean)value;// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("boolean type:" + value);
    					}
    				}
    				// 如果类型是Date
    				if (f.getGenericType().toString().equals(
    						"class java.util.Date")) {

//    					value = (Date)value;// 调用getter方法获取属性值
    					value=rs.getTimestamp(f.getName());
    					if (value != null) {
    						System.out.println("Date type:" + value);
    					}
    				}
    				// 如果类型是Short
    				if (f.getGenericType().toString().equals(
    						"class java.lang.Short")) {
    					value = (Short)value;// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("Short type:" + value);
    					}
    				}
    				// 如果类型是BigDecimal
    				if (f.getGenericType().toString().equals(
    						"class java.math.BigDecimal")) {
    					value = (BigDecimal)value;// 调用getter方法获取属性值
    					if (value != null) {
    						System.out.println("BigDecimal type:" + value);
    					}
    				}
    				// 如果类型是Timestamp
    				if (f.getGenericType().toString().equals(
    						"class java.sql.Timestamp")) {

    					value=rs.getTimestamp(f.getName());
    					if (value != null) {
    						System.out.println("Timestamp type:" + value);
    					}
    				} 
                     
                     
                     //如果匹配进行赋值
                    	 boolean flag = f.isAccessible();
                         f.setAccessible(true);
                         f.set(obj, value);
                         f.setAccessible(flag); 
                     
                 }
//             }
             list.add(obj);
         }
        return list;
    }
    public static JdbcTemplate getTemplate() {

		return dsf.getYbJdbcTemplate();
	}
 // 把一个字符串的第一个字母大写、效率是最高的、
	private static String getMethodName(String fildeName) throws Exception{
		byte[] items = fildeName.getBytes();
		items[0] = (byte) ((char) items[0] - 'a' + 'A');
		return new String(items);
	
	}
	
	public static List  getList(String sql, Class clazz){
		Connection conn = DbUtils.getConn();
        ResultSet rs = null;
        PreparedStatement psmt = null;
        System.out.println(conn);
        List list=null;
        try {
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
			try {
				list = DbUtils.populate(rs, clazz);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs=null;
            }
            if(psmt!=null){
                try {
                    psmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                psmt=null;
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn=null;
            }
        }
	        
     return list;
	}
	
}

实体类:

@Entity
@Table(name = "V_AB01_UPLOAD")
public class V_AB01_UPLOAD implements Serializable {

	 /**
     * 社保业务系统编码
     */
    private java.lang.String AAZ400;


    @Column(name = "AAZ400", length = 10)
    public java.lang.String getAAZ400() {
        return this.AAZ400;
    }


    public void setAAZ400(final java.lang.String AAZ400) {
        this.AAZ400 = AAZ400;
    }

    /**
     * UUID
     */
    private java.lang.String SBUUID;


    @Column(name = "SBUUID", length = 32)
    public java.lang.String getSBUUID() {
        return this.SBUUID;
    }


    public void setSBUUID(final java.lang.String SBUUID) {
        this.SBUUID = SBUUID;
    }
    /**
     * 数据传输类型
     */
    private java.lang.String AAA321;


    @Column(name = "AAA321", length = 1)
    public java.lang.String getAAA321() {
        return this.AAA321;
    }


    public void setAAA321(final java.lang.String AAA321) {
        this.AAA321 = AAA321;
    }


    /**
     * 传输批次号
     */
    private java.lang.String AAA322;


    @Column(name = "AAA322", length = 30)
    public java.lang.String getAAA322() {
        return this.AAA322;
    }


    public void setAAA322(final java.lang.String AAA322) {
        this.AAA322 = AAA322;
    }


    /**
     * 单位编号
     */
    private java.lang.String AAB001;


    @Column(name = "AAB001", length = 30)
    public java.lang.String getAAB001() {
        return this.AAB001;
    }

    public void setAAB001(final java.lang.String AAB001) {
        this.AAB001 = AAB001;
    }


    /**
     * 组织机构代码
     */
    private java.lang.String AAB003;

    @Column(name = "AAB003", length = 9)
    public java.lang.String getAAB003() {
        return this.AAB003;
    }


    public void setAAB003(final java.lang.String AAB003) {
        this.AAB003 = AAB003;
    }

    /**
     * 单位名称
     */
    private java.lang.String AAB004;


    @Column(name = "AAB004", length = 300)
    public java.lang.String getAAB004() {
        return this.AAB004;
    }


    public void setAAB004(final java.lang.String AAB004) {
        this.AAB004 = AAB004;
    }

    /**
     * 特殊单位类别代码
     */
    private java.lang.String AAB065;


    @Column(name = "AAB065", length = 4)
    public java.lang.String getAAB065() {
        return this.AAB065;
    }


    public void setAAB065(final java.lang.String AAB065) {
        this.AAB065 = AAB065;
    }


    /**
     * 登记注册地所在行政区划代码
     */
    private java.lang.String AAB301;


    @Column(name = "AAB301", length = 6)
    public java.lang.String getAAB301() {
        return this.AAB301;
    }


    public void setAAB301(final java.lang.String AAB301) {
        this.AAB301 = AAB301;
    }


    /**
     * 统一社会信用
     */
    private java.lang.String AAB998;


    @Column(name = "AAB998", length = 18)
    public java.lang.String getAAB998() {
        return this.AAB998;
    }


    public void setAAB998(final java.lang.String AAB998) {
        this.AAB998 = AAB998;
    }


    /**
     * 联系人姓名
     */
    private java.lang.String AAE004;


    @Column(name = "AAE004", length = 75)
    public java.lang.String getAAE004() {
        return this.AAE004;
    }

    public void setAAE004(final java.lang.String AAE004) {
        this.AAE004 = AAE004;
    }


    /**
     * 联系电话
     */
    private java.lang.String AAE005;


    @Column(name = "AAE005", length = 60)
    public java.lang.String getAAE005() {
        return this.AAE005;
    }


    public void setAAE005(final java.lang.String AAE005) {
        this.AAE005 = AAE005;
    }

    /**
     * 联系地址
     */
    private java.lang.String AAE006;


    @Column(name = "AAE006", length = 300)
    public java.lang.String getAAE006() {
        return this.AAE006;
    }

    public void setAAE006(final java.lang.String AAE006) {
        this.AAE006 = AAE006;
    }


    /**
     * 邮政编码
     */
    private java.lang.String AAE007;


    @Column(name = "AAE007", length = 6)
    public java.lang.String getAAE007() {
        return this.AAE007;
    }

    public void setAAE007(final java.lang.String AAE007) {
        this.AAE007 = AAE007;
    }

    /**
     * 经办时间
     */
    private java.lang.String AAE036;


    @Column(name = "AAE036", length = 7)
    public java.lang.String getAAE036() {
        return this.AAE036;
    }


    public void setAAE036(final java.lang.String AAE036) {
        this.AAE036 = AAE036;
    }
    /**
     * 传输时间戳
     */
    private java.lang.String AAE418;


    @Column(name = "AAE418", length = 11)
    public java.lang.String getAAE418() {
        return this.AAE418;
    }


    public void setAAE418(final java.lang.String AAE418) {
        this.AAE418 = AAE418;
    }

    /**
     * XH
     */
    private java.lang.String XH;


    @Column(name = "XH", length = 30)
    public java.lang.String getXH() {
        return this.XH;
    }


    public void setXH(final java.lang.String XH) {
        this.XH = XH;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值