Mybatis的resultMap返回map问题

网友投稿 542 2022-08-30


Mybatis的resultMap返回map问题

目录resultMap返回map问题简单封装resultMap返回对象为map

resultMap返回map问题

select

bank_name,

maintain_time_interval

from

fp_channel_prd_bank

where

channel_prd_id=7

and

maintain_time_interval

简单封装resultMap返回对象为map

public class DbUtils {

private static String host = "47.93.******";

private static String port = "3306";

private static String username = "*****";

private static String password = "******";

private static String database = "******";

static {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (Exception e) {

}

}

private static Connection getConn() {

Connection conn = null;

try {

String url = "jdbcqybGP:mysql://" + host + ":" + port + "/" + database;

conn = DriverManager.getConnection(url, username, password);

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

public static List execQuery(String sql, Object[] args) throws Exception {

Connection conn = getConn();

PreparedStatement ps = conn.prepareCall(sql);

ResultSet rs = null;

int count = StringUtils.countMatches(sql, "?");

//变量赋值。。。。。。

for (int i = 0; i < count; i++) {

ps.setObject(i, args[i]);

}

List> list = new ArrayList>();

rs = ps.executeQuery();

ResultSetMetaData metaData = rs.getMetaData();

      //注意。。metaData.getColumnName 获取字段名,rs.getObject 获取属性 是从 1 开始的,而不是从0 开始

while (rs.next()) {

int rowSize = metaData.getColumnCount();

Map map = new HashMap<>();

for (int i = 1; i <= rowSize; i++) {

String labelName = metaData.getColumnName(i);

Object obj = rs.getObject(labelName);

map.put(labelName, obj);

}

list.add(map);

}

close(conn, ps, rs);

return list;

}

/**

* @param conn

* @param ps

* @param rs

* @throws Exception

*/

private static void close(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception {

rs.close();

ps.close();

conn.close();

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python与matlab一些常用函数互转(matlab转成python)
下一篇:内存屏障由来及实现思路
相关文章

 发表评论

暂时没有评论,来抢沙发吧~