日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

JdbcTemplate使用小结

發(fā)布時(shí)間:2025/3/8 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JdbcTemplate使用小结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  org.springframework.jdbc.core.JdbcTemplate.query(String sql, Object[] args, RowMapper<StaffUnionVO> rowMapper) throws DataAccessException

1.自定義rowMapper

public class StaffUnionVO implements RowMapper<StaffUnionVO>, Serializable {private static final long serialVersionUID = 1544023434308856628L;public StaffUnionVO() {}private String code;// 員工編碼private String name;// 員工姓名public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}private boolean isExistColumn(ResultSet rs, String columnName) {try {if (rs.findColumn(columnName) > 0) {return true;}} catch (SQLException e) {return false;}return false;}@Overridepublic StaffUnionVO mapRow(ResultSet rs, int row) throws SQLException {StaffUnionVO vo = new StaffUnionVO();
 
    if (isExistColumn(rs, "code"))vo.setCode(rs.getString("code"));
    if (isExistColumn(rs, "name"))vo.setName(rs.getString("name"));
  
    return vo;} }

  示例: List<StaffUnionVO> vos =?JdbcTemplate.query(sql, new Object[0], new?StaffUnionVO()?);

?

2.使用BeanPropertyRowMapper

public class StaffUnionVO implements Serializable {private static final long serialVersionUID = 1544023434308856628L;public StaffUnionVO() {}private String code;// 員工編碼private String name;// 員工姓名public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;} }

  示例:List<StaffUnionVO> vos =?JdbcTemplate.query(sql, new Object[0], BeanPropertyRowMapper.newInstance(StaffUnionVO.class));

?

  看一下?BeanPropertyRowMapper.java 的源碼,可以學(xué)到不少東西。

/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/ package org.springframework.jdbc.core;import java.beans.PropertyDescriptor; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanWrapper; import org.springframework.beans.NotWritablePropertyException; import org.springframework.beans.PropertyAccessorFactory; import org.springframework.beans.TypeMismatchException; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.support.JdbcUtils; import org.springframework.util.Assert; import org.springframework.util.StringUtils;public class BeanPropertyRowMapper<T> implements RowMapper<T> {protected final Log logger = LogFactory.getLog(super.getClass());private Class<T> mappedClass;private boolean checkFullyPopulated = false;private boolean primitivesDefaultedForNullValue = false;private Map<String, PropertyDescriptor> mappedFields;private Set<String> mappedProperties;public BeanPropertyRowMapper() {}public BeanPropertyRowMapper(Class<T> mappedClass) {initialize(mappedClass);}public BeanPropertyRowMapper(Class<T> mappedClass, boolean checkFullyPopulated) {initialize(mappedClass);this.checkFullyPopulated = checkFullyPopulated;}public void setMappedClass(Class<T> mappedClass) {if (this.mappedClass == null) {initialize(mappedClass);} else if (!(this.mappedClass.equals(mappedClass)))throw new InvalidDataAccessApiUsageException(new StringBuilder().append("The mapped class can not be reassigned to map to ").append(mappedClass).append(" since it is already providing mapping for ").append(this.mappedClass).toString());}protected void initialize(Class<T> mappedClass) {this.mappedClass = mappedClass;this.mappedFields = new HashMap();this.mappedProperties = new HashSet();PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);for (PropertyDescriptor pd : pds)if (pd.getWriteMethod() != null) {this.mappedFields.put(pd.getName().toLowerCase(), pd);String underscoredName = underscoreName(pd.getName());if (!(pd.getName().toLowerCase().equals(underscoredName))) {this.mappedFields.put(underscoredName, pd);}this.mappedProperties.add(pd.getName());}}//在大寫(xiě)的字符前加入下滑線private String underscoreName(String name) {if (!(StringUtils.hasLength(name))) {return "";}StringBuilder result = new StringBuilder();result.append(name.substring(0, 1).toLowerCase());for (int i = 1; i < name.length(); ++i) {String s = name.substring(i, i + 1);String slc = s.toLowerCase();if (!(s.equals(slc))) {result.append("_").append(slc);} else {result.append(s);}}return result.toString();}public final Class<T> getMappedClass() {return this.mappedClass;}public void setCheckFullyPopulated(boolean checkFullyPopulated) {this.checkFullyPopulated = checkFullyPopulated;}public boolean isCheckFullyPopulated() {return this.checkFullyPopulated;}public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) {this.primitivesDefaultedForNullValue = primitivesDefaultedForNullValue;}public boolean isPrimitivesDefaultedForNullValue() {return this.primitivesDefaultedForNullValue;}public T mapRow(ResultSet rs, int rowNumber) throws SQLException {Assert.state(this.mappedClass != null, "Mapped class was not specified");Object mappedObject = BeanUtils.instantiate(this.mappedClass);BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);initBeanWrapper(bw);ResultSetMetaData rsmd = rs.getMetaData();int columnCount = rsmd.getColumnCount();Set populatedProperties = (isCheckFullyPopulated()) ? new HashSet() : null;//稀少的特性for (int index = 1; index <= columnCount; ++index) {String column = JdbcUtils.lookupColumnName(rsmd, index);PropertyDescriptor pd = (PropertyDescriptor) this.mappedFields.get(column.replaceAll(" ", "").toLowerCase());if (pd == null)continue;try {Object value = getColumnValue(rs, index, pd);if ((this.logger.isDebugEnabled()) && (rowNumber == 0))this.logger.debug(new StringBuilder().append("Mapping column '").append(column).append("' to property '").append(pd.getName()).append("' of type ").append(pd.getPropertyType()).toString());try {bw.setPropertyValue(pd.getName(), value);} catch (TypeMismatchException e) {if ((value == null) && (this.primitivesDefaultedForNullValue)) {this.logger.debug(new StringBuilder().append("Intercepted TypeMismatchException for row ").append(rowNumber).append(" and column '").append(column).append("' with value ").append(value).append(" when setting property '").append(pd.getName()).append("' of type ").append(pd.getPropertyType()).append(" on object: ").append(mappedObject).toString());} else {throw e;}}if (populatedProperties != null) {populatedProperties.add(pd.getName());}} catch (NotWritablePropertyException ex) {throw new DataRetrievalFailureException(new StringBuilder().append("Unable to map column ").append(column).append(" to property ").append(pd.getName()).toString(), ex);}}if ((populatedProperties != null) && (!(populatedProperties.equals(this.mappedProperties)))) {throw new InvalidDataAccessApiUsageException(new StringBuilder().append("Given ResultSet does not contain all fields necessary to populate object of class [").append(this.mappedClass).append("]: ").append(this.mappedProperties).toString());}return mappedObject;}protected void initBeanWrapper(BeanWrapper bw) {}protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());}public static <T> BeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {BeanPropertyRowMapper newInstance = new BeanPropertyRowMapper();newInstance.setMappedClass(mappedClass);return newInstance;} }

  1.通過(guò)PropertyDescriptor反映射調(diào)用set和get方法

  2.HashSet、TreeSet?equals方法

  AbstractSet.java

  public
boolean equals(Object o) {if (o == this)return true;if (!(o instanceof Set))return false;Collection<?> c = (Collection<?>) o;if (c.size() != size())return false;try {return containsAll(c);} catch (ClassCastException unused) {return false;} catch (NullPointerException unused) {return false;}}

?

  3.BeanPropertyRowMapper checkFullyPopulated 默認(rèn)是false,這樣的話如果 sql結(jié)果集中的字段 和 DTO 字段不匹配,就會(huì)拋異常。可以手動(dòng)設(shè)置這個(gè)值。

  4.PO BO VO DTO POJO DAO概念及其作用(附轉(zhuǎn)換圖)

?

轉(zhuǎn)載于:https://www.cnblogs.com/hujunzheng/p/6306769.html

總結(jié)

以上是生活随笔為你收集整理的JdbcTemplate使用小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。