有谁能给个封装结果集的例子mysql的并注解一下,学习java的
发布网友
发布时间:2022-05-01 15:37
我来回答
共2个回答
热心网友
时间:2022-05-01 17:07
package com.golden.;
import java.lang.reflect.Method;
import java.sql.ResultSet;
/**
* 行映射器
*
* @author pine
*
*/
public class RowMapper {
/**
* 将结果集中一行映射为对象
* @param resultSet
* @param clazz
* @return T
*/
public <T> T mappingRow(ResultSet resultSet, Class<T> clazz) {
class StringHelper{
/**
* 首字母大写
* @param str
* @return String
*/
private String toUpperCase(String str){
String firstLetter = str.substring(0, 1).toUpperCase();
if (str.length()==1) {
return firstLetter;
}
return firstLetter.concat(str.substring(1));
}
/**
* 获取方法名
* @param strs
* @return String
*/
private String getMethodName(String[] strs){
StringBuffer methodName = new StringBuffer("set");
for (String temp : strs) {
if (temp.equals("")) {
continue;
}
methodName.append(this.toUpperCase(temp));
}
return methodName.toString();
}
/**
* 获取方法名
* @param field
* @return String
*/
public String getMethodName(String field){
return this.getMethodName(field.split("_"));
}
}
StringHelper helper = new StringHelper();
try {
T t = clazz.newInstance();
int columnCount = resultSet.getMetaData().getColumnCount();
Method[] methods = clazz.getMethods();
outer:for (int i = 0; i < columnCount; i++) {
String field = resultSet.getMetaData().getColumnLabel(i+1);
String methodName = helper.getMethodName(field);
for (Method method : methods) {
if (method.getName().equals(methodName)) {
method.invoke(t, resultSet.getObject(i+1));
continue outer;
}
}
}
return t;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
使用的时候:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.golden..BankDao;
import com.golden..RowMapper;
import com.golden.util.DbcpUtils;
import com.golden.vo.Bank;
//继承RowMapper
public class BankDaoImpl extends RowMapper implements BankDao {
public List<Bank> findBanks(String query, int start, int limit) {
Connection connection = DbcpUtils.getConnection();
String sql = "select * from bank t where (t.bank_no like concat('%',?,'%') or t.bank_name like concat('%',?,'%')) order by t.id asc limit ?,?;";
DbcpUtils.printlnSQL(sql);
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, query);
preparedStatement.setString(2, query);
preparedStatement.setInt(3, start);
preparedStatement.setInt(4,limit);
resultSet =preparedStatement.executeQuery();
List<Bank> banks = new ArrayList<Bank>();
while (resultSet.next()) {
banks.add(this.mappingRow(resultSet, Bank.class));//调用封装方法
}
return banks;
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
DbcpUtils.closeConnection(connection, preparedStatement, resultSet);
}
}
}
热心网友
时间:2022-05-01 18:25
select * from (
(SELECT uid,je,starttime,stoptime,1 as type from (SELECT id from members WHERE referer='4') as m JOIN (SELECT uid,je,starttime,stoptime from `cz` WHERE `status`>0) as c on c.uid=m.id)
union
(SELECT uid,je,starttime,stoptime,2 as type from (SELECT id from members WHERE referer='4') as m JOIN (SELECT uid,je,starttime,stoptime from `withdrawh` WHERE `state`=1) as w on w.uid=m.id)
) order by starttime