java查询转对象_ResultSet转成java类对象
在做web开发时遇到⼀个事情:
需要从mysql数据表中查询数据并遍历查询结果
这样最简单的⽅式是:查询到结果根据表中字段列表的顺序来⼀个个获取字段,但这样需要记住字段的顺序,操作起来不是那么⽅便。因此便想可不可以把查询结果
ResultSet转化成java的实例对象,然后利⽤类对象的get⽅法,这样会⽅便清晰很多。
查了下,有很多可以参考,于是参考着有了⾃⼰的代码,如下:
1.mapper函数:
package com.alibaba.search.offline.oddiff.service;
import flect.Field;
import flect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.apachemons.beanutils.BeanUtils;
import org.apachemons.logging.Log;
import org.apachemons.logging.LogFactory;
import com.jcraft.jsch.Logger;
//import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement.Column;
public class OddiffResultSetMapper {
protected static Log logger = Log(OddiffResultSetMapper.class);
@SuppressWarnings("unchecked")
public List mapRersultSetToObject(ResultSet rs, Class outputClass) {
List outputList = null;
try {
// make sure resultset is not null
if (rs != null) {
logger.info("rs:"+rs);
// check if outputClass has 'Entity' annotation
if (outputClass.isAnnotationPresent(Entity.class)) {
/
/ get the resultset metadata
ResultSetMetaData rsmd = rs.getMetaData();
// get all the attributes of outputClass
Field[] fields = DeclaredFields();
while (rs.next()) {
T bean = (T) wInstance();
for (int _iterator = 0; _iterator < ColumnCount(); _iterator++) {
// getting the SQL column name
String columnName = ColumnName(_iterator + 1);
// reading the value of the SQL column
Object columnValue = rs.getObject(_iterator + 1);
logger.info("columnName:"+columnName+",value:"+columnValue);
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
Column column = Annotation(Column.class);
if (column.name().equalsIgnoreCase(columnName) && columnValue != null) { BeanUtils.setProperty(bean, Name(), columnValue);
break;
}
}
}
}
if (outputList == null) {
outputList = new ArrayList();
}
outputList.add(bean);
}
} else {
// throw some error
}
} else {
logger.info("rs is null");
return null;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
resultset 遍历} catch (InvocationTargetException e) {
e.printStackTrace();
}
return outputList;
}
}
2.类对象的声明:
package com.alibaba.search.offline.oddiff.service; import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class OddiffEntity {
@Column(name="id")
private Integer Id;
@Column(name="user_name")
private String userName;
@Column(name="app_name")
private String appName;
@Column(name="online_version")
private String onlineVersion;
@Column(name="oa_version")
private String oaVersion;
@Column(name="data_name")
private String dataName;
@Column(name="start_time")
private String startTime;
@Column(name="end_time")
private String endTime;
@Column(name="commit_time")
private String commitTime;
@Column(name="duration_time")
private String durationTime;
@Column(name="status")
private String status;
@Column(name="log_file")
private String logFile;
public void setId(Integer id) {
this.Id = id;
}
public Integer getId() {
return Id;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getAppName() {
return appName;
}
public void setOnlineVersion(String onlineVersion) { lineVersion = onlineVersion;
}
public String getOnlineVersion() {
return onlineVersion;
}
public void setOaVersion(String oaVersion) {
this.oaVersion = oaVersion;
}
public String getOaVersion() {
return this.oaVersion;
}
public void setDataName(String dataName) {
this.dataName = dataName;
}
public String getDataName() {
return dataName;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getStartTime() {
return startTime;
}
public void setEndTime(String endTime) {
}
public String getEndTime() {
return endTime;
}
public void setCommitTime(String commitTime) { thismitTime = commitTime;
}
public String getCommitTime() {
return commitTime;
}
public void setDurationTime(String durationTime) { this.durationTime = durationTime;
}
public String getDurationTime() {
return durationTime;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论