java jdbc 注解_使用注解将JDBC结果集映射到Java对象

http://www.codeproject.com/Tips/372152/Mapping-JDBC-ResultSet-to-Object-using-Annotations

1.[代码]SamplePojo.java

import javax.persistence.Column;

import javax.persistence.Entity;

@Entity

public class SamplePojo {

@Column(name="User_Id")

private int id;

@Column(name="User_Name")

private String name;

@Column(name="Address")

private String address;

@Column(name="Gender")

private boolean gender;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public boolean isGender() {

return gender;

}

public void setGender(boolean gender) {

this.gender = gender;

}

@Override

public String toString() {

return "id: " + id + "\n" +

"name: " + name + "\n"+

"address: " + address + "\n" +

"gender: " + (gender ? "Male" : "Female") + "\n\n";

}

}

2.[代码]ResultSetMapper.java

import java.lang.reflect.Field;

import java.lang.reflect.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.apache.commons.beanutils.BeanUtils;

public class ResultSetMapper {

@SuppressWarnings("unchecked")

public List mapRersultSetToObject(ResultSet rs, Class outputClass) {

List outputList = null;

try {

// make sure resultset is not null

if (rs != null) {

// 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 = outputClass.getDeclaredFields();

while (rs.next()) {

T bean = (T) outputClass.newInstance();

for (int _iterator = 0; _iterator < rsmd

.getColumnCount(); _iterator++) {

// getting the SQL column name

String columnName = rsmd

.getColumnName(_iterator + 1);

// reading the value of the SQL column

Object columnValue = rs.getObject(_iterator + 1);

// iterating over outputClass attributes to check if any attribute has 'Column' annotation with matching 'name' value

for (Field field : fields) {

if (field.isAnnotationPresent(Column.class)) {

Column column = field

.getAnnotation(Column.class);

if (column.name().equalsIgnoreCase(

columnName)

&& columnValue != null) {

BeanUtils.setProperty(bean, field

.getName(), columnValue);

break;

}

}

}

}

if (outputList == null) {

outputList = new ArrayList();

}

outputList.add(bean);

}

} else {

// throw some error

}

} else {

return null;

}

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

return outputList;

}

}

3.[代码]使用方法

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.List;

public class SampleMain {

public static void main(String ...args){

try {

ResultSetMapper resultSetMapper = new ResultSetMapper();

ResultSet resultSet = null;

// simple JDBC code to run SQL query and populate resultSet - START

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String database = "jdbc:odbc:AkDb";

Connection connection = DriverManager.getConnection( database ,"","");

PreparedStatement statement = connection.prepareStatement("SELECT * FROM UsersSample");

resultSet = statement.executeQuery();

// simple JDBC code to run SQL query and populate resultSet - END

List pojoList = resultSetMapper.mapRersultSetToObject(resultSet, SamplePojo.class);

// print out the list retrieved from database

if(pojoList != null){

for(SamplePojo pojo : pojoList){

System.out.println(pojo);

}

}else{

System.out.println("ResultSet is empty. Please check if database table is empty");

}

connection.close();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值