java通过反射批量生成set设置值

对于大批量属性的类,里面有有很多属性,这个时候一个个写还是很痛苦的,通过java反射来获取这些类里面的方法,从而得到自己想要的set属性的字符串再好不过了。

下面是一个JDOM设置值的,直接反射生成即可 

project.setSeatPicUrl(node.getChildText("seatPicUrl"));  
project.setId(node.getChildText("id"));  
project.setCategoryID(node.getChildText("categoryID"));  
project.setSonCategoryID(node.getChildText("sonCategoryID"));  
project.setSiteID(node.getChildText("siteID"));  
project.setSiteName(node.getChildText("siteName"));  
project.setSubHead(node.getChildText("subHead"));  
project.setIsETicket(node.getChildText("isETicket"));  
project.setMinPrice(node.getChildText("minPrice"));  
project.setMaxPrice(node.getChildText("maxPrice"));  
project.setPriceStr(node.getChildText("priceStr"));  
project.setSellEndTime(node.getChildText("sellEndTime"));  
project.setShowTime(node.getChildText("showTime"));  
project.setCanSell(node.getChildText("canSell"));  
project.setIsTest(node.getChildText("isTest"));  
project.setVenueName(node.getChildText("venueName"));  
project.setVenueID(node.getChildText("venueID"));  
project.setIsXuanZuo(node.getChildText("isXuanZuo"));  
project.setUpdateTime(node.getChildText("updateTime"));  
project.setPerformIdList(node.getChildText("performIdList"));  
project.setName(node.getChildText("name"));  
project.setDescription(node.getChildText("description"));  
project.setStatus(node.getChildText("status"));

 

 

Java代码

package com.proxy.reflect;  
  
/** 
 * @author: (le.qiao) 
 * @e-mail: qiaolevip@gmail.com 
 * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a> 
 * @date: 2012-11-30 
 *  
 */  
import java.lang.reflect.Field;  
import java.lang.reflect.Method;  
  
import com.proxy.bean.Project;  
  
public class ReflectUser {  
  
    public static void main(String[] args) throws Exception {  
        /* 
         * 实列化类 方法2 
         */  
        Project bean = new Project();  
        // bean.setAge(100);  
        // bean.setBirthday(new Date());  
        // bean.setName("武汉");  
  
        // 得到类对象  
        @SuppressWarnings("rawtypes")  
        Class cls = (Class) bean.getClass();  
  
        /* 
         * 得到类中的所有属性集合 
         */  
        Field[] fs = cls.getDeclaredFields();  
        for (int i = 0; i < fs.length; i++) {  
            Field f = fs[i];  
            f.setAccessible(true); // 设置些属性是可以访问的  
            // Object val = f.get(bean);// 得到此属性的值  
  
            // System.out.println("name:" + f.getName() + "\t value = " + val);  
  
            String type = f.getType().toString();// 得到此属性的类型  
            if (type.endsWith("String")) {  
                // System.out.println(f.getType() + "\t是String");  
                f.set(bean, "12");        // 给属性设值  
            } else if (type.endsWith("int") || type.endsWith("Integer")) {  
                // System.out.println(f.getType() + "\t是int");  
                f.set(bean, 12);       // 给属性设值  
            } else {  
                // System.out.println(f.getType() + "\t");  
            }  
  
        }  
  
        /* 
         * 得到类中的方法 
         */  
        Method[] methods = cls.getMethods();  
        for (int i = 0; i < methods.length; i++) {  
            Method method = methods[i];  
            // if (method.getName().startsWith("get")) {  
            // System.out.print("methodName:" + method.getName() + "\t");  
            // System.out.println("value:" + method.invoke(bean));// 得到get 方法的值  
            // }  
  
            if (method.getName().startsWith("set")) {  
                System.out.print(firstToLower(cls.getSimpleName()) + "." + method.getName() + "(node.getChildText(\""  
                        + firstToLower(method.getName().substring(3)) + "\"));\n");  
            }  
        }  
    }  
  
    /** 
     * @param val 
     * @return 
     */  
    public static String firstToLower(String val) {  
        return val.substring(0, 1).toLowerCase() + val.substring(1);  
    }  
  
}  

 

 

java实体类

package com.proxy.bean;  
  
import java.util.ArrayList;  
  
/** 
 * @author: (le.qiao) 
 * @e-mail: qiaolevip@gmail.com 
 * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a> 
 * @date: 2012-8-13 
 *  
 */  
public class Project {  
  
    // 项目ID  
    private String id;  
  
    // 项目名称  
    private String name;  
  
    // 所属父类别  
    private String categoryID;  
  
    // 所属子类别  
    private String sonCategoryID;  
  
    // 城市ID  
    private String siteID;  
  
    // 城市名称  
    private String siteName;  
  
    // 项目详情描述  
    private String description;  
  
    // 一句话描述  
    private String subHead;  
  
    // 是否支持电子票(1-支持;0-不支持)  
    private String isETicket;  
  
    // 最低价格  
    private String minPrice;  
  
    // 最高价格  
    private String maxPrice;  
  
    // 价格快照  
    private String priceStr;  
  
    // 销售结束时间  
    private String sellEndTime;  
  
    // 演出时间  
    private String showTime;  
  
    // 是否可以购买(1-可以购买;0-不允许购买)  
    private String canSell;  
  
    // 是否是测试项目(1-是,0-不是)  
    private String isTest;  
  
    // 场馆名称  
    private String venueName;  
  
    // 场馆ID  
    private String venueID;  
  
    // 是否支持选座(1-支持选座;0-不支持选座)  
    private String isXuanZuo;  
  
    // 相关艺人信息节点  
    // private String artistinfo;  
  
    // 最后更新时间  
    private String updateTime;  
  
    // 商品状态(3,销售中;4,结束;7,预定;8,预售;10,测试;6,禁止显示)  
    private String status;  
  
    /** 座位表图片路径 */  
    private String seatPicUrl;  
  
    private List<String> performIdList = new ArrayList<String>();  
  
    public String getSeatPicUrl() {  
        return seatPicUrl;  
    }  
  
    public void setSeatPicUrl(String seatPicUrl) {  
        this.seatPicUrl = seatPicUrl;  
    }  
  
    public String getId() {  
        return id;  
    }  
  
    public void setId(String id) {  
        this.id = id;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getCategoryID() {  
        return categoryID;  
    }  
  
    public void setCategoryID(String categoryID) {  
        this.categoryID = categoryID;  
    }  
  
    public String getSonCategoryID() {  
        return sonCategoryID;  
    }  
  
    public void setSonCategoryID(String sonCategoryID) {  
        this.sonCategoryID = sonCategoryID;  
    }  
  
    public String getSiteID() {  
        return siteID;  
    }  
  
    public void setSiteID(String siteID) {  
        this.siteID = siteID;  
    }  
  
    public String getSiteName() {  
        return siteName;  
    }  
  
    public void setSiteName(String siteName) {  
        this.siteName = siteName;  
    }  
  
    public String getDescription() {  
        return description;  
    }  
  
    public void setDescription(String description) {  
        this.description = description;  
    }  
  
    public String getSubHead() {  
        return subHead;  
    }  
  
    public void setSubHead(String subHead) {  
        this.subHead = subHead;  
    }  
  
    public String getIsETicket() {  
        return isETicket;  
    }  
  
    public void setIsETicket(String isETicket) {  
        this.isETicket = isETicket;  
    }  
  
    public String getMinPrice() {  
        return minPrice;  
    }  
  
    public void setMinPrice(String minPrice) {  
        this.minPrice = minPrice;  
    }  
  
    public String getMaxPrice() {  
        return maxPrice;  
    }  
  
    public void setMaxPrice(String maxPrice) {  
        this.maxPrice = maxPrice;  
    }  
  
    public String getPriceStr() {  
        return priceStr;  
    }  
  
    public void setPriceStr(String priceStr) {  
        this.priceStr = priceStr;  
    }  
  
    public String getSellEndTime() {  
        return sellEndTime;  
    }  
  
    public void setSellEndTime(String sellEndTime) {  
        this.sellEndTime = sellEndTime;  
    }  
  
    public String getShowTime() {  
        return showTime;  
    }  
  
    public void setShowTime(String showTime) {  
        this.showTime = showTime;  
    }  
  
    public String getCanSell() {  
        return canSell;  
    }  
  
    public void setCanSell(String canSell) {  
        this.canSell = canSell;  
    }  
  
    public String getIsTest() {  
        return isTest;  
    }  
  
    public void setIsTest(String isTest) {  
        this.isTest = isTest;  
    }  
  
    public String getVenueName() {  
        return venueName;  
    }  
  
    public void setVenueName(String venueName) {  
        this.venueName = venueName;  
    }  
  
    public String getVenueID() {  
        return venueID;  
    }  
  
    public void setVenueID(String venueID) {  
        this.venueID = venueID;  
    }  
  
    public String getIsXuanZuo() {  
        return isXuanZuo;  
    }  
  
    public void setIsXuanZuo(String isXuanZuo) {  
        this.isXuanZuo = isXuanZuo;  
    }  
  
    // public String getArtistinfo() {  
    // return artistinfo;  
    // }  
    //  
    // public void setArtistinfo(String artistinfo) {  
    // this.artistinfo = artistinfo;  
    // }  
  
    public String getUpdateTime() {  
        return updateTime;  
    }  
  
    public void setUpdateTime(String updateTime) {  
        this.updateTime = updateTime;  
    }  
  
    public String getStatus() {  
        return status;  
    }  
  
    public void setStatus(String status) {  
        this.status = status;  
    }  
  
    public List<String> getPerformIdList() {  
        return performIdList;  
    }  
  
    public void setPerformIdList(List<String> performIdList) {  
        this.performIdList = performIdList;  
    }  
  
}  

 

 

麦田应用:(思路:从数据库查询列名称进行拼接)

 public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@172.16.6.111:1521/mtdb";
            String user = "BSS_KANYU_BEIJING_20180814_89";
            String pass = "BSS_KANYU_BEIJING_20180814_89";
            conn = DriverManager.getConnection(url, user, pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
public static void getColoms(String tableName) throws SQLException {
        Connection conn = getConnection();
        String sql = "select * from " + tableName + " where rownum = 1 ";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData data = rs.getMetaData();
            for (int i = 1; i <= data.getColumnCount(); i++) {
                // 获得指定列的列名
                String columnName = data.getColumnName(i);
                System.out.println("dmHouseBase.set"+toUpperCaseFirstOne(ReportUtils.lineToHump(columnName))+"(rs.getObject(\""+columnName+"\")!=null?rs.getObject(\""+columnName+"\").toString():\"\");");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt!=null) {
                stmt.close();
            }
            if (conn!=null) {
                conn.close();
            }
        }
    }
public static void main(String[] args) throws SQLException {
        for (String table:tables) {
            getColoms(table);
        }
    }

 

转载于:https://www.cnblogs.com/pypua/articles/9876968.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值