- 2023-11-16 13:25:00
- 1502 热度
- 0 评论
虽然项目中都夹杂了Hibernate的支持,但是团队开发中,很多人为了编写特殊查询的代码时都使用了JDBC进行查询。
JDBC查询后返回的是一个List集合,List中组装的是Map,一个Map就是一个对应的对象。
但是接口不能直接返回Map,都是返回的对象,以方便自己和其他人使用,为了转换这个Map,往往写这样的代码:
@SuppressWarnings("unchecked") public static MS_Mont analyzeMapToMS_Mont(Map map){ MS_Mont obj = new MS_Mont(); if(null != map.get("montNo")) obj.setMontNo(Integer.parseInt(map.get("montNo").toString())); if(null != map.get("montName")) obj.setMontName(map.get("montName").toString()); if(null != map.get("montType")) obj.setMontType(Integer.parseInt(map.get("montType").toString())); if(null != map.get("montLength")) obj.setMontLength(Integer.parseInt(map.get("montLength").toString())); if(null != map.get("montDesc")) obj.setMontDesc(map.get("montDesc").toString()); if(null != map.get("bigType")) obj.setBigType(Integer.parseInt(map.get("bigType").toString())); if(null != map.get("bigTypeName")) obj.setBigTypeName(map.get("bigTypeName").toString()); if(null != map.get("littleType")) obj.setLittleType(Integer.parseInt(map.get("littleType").toString())); if(null != map.get("littleTypeName")) obj.setLittleTypeName(map.get("littleTypeName").toString()); if(null != map.get("insertTime")) obj.setInsertTime(map.get("insertTime").toString()); if(null != map.get("updateTime")) obj.setUpdateTime(map.get("updateTime").toString()); if(null != map.get("userNoRe")) obj.setUserNoRe(Integer.parseInt(map.get("userNoRe").toString())); if(null != map.get("userNoLast")) obj.setUserNoLast(Integer.parseInt(map.get("userNoLast").toString())); return obj; }
很麻烦,很多,很枯燥。
为了解决这个问题,我列出一个解决方法,写一个方法,传入要赋值的对象和Map,然后根据列的属性名称从Map中获得响应的值,然后赋值给这个对象的属性。
例如,这里写了一个简单的查询:
public CM_Line getObjectBean(int lineNo) { try { String sql = "select * from cm_line where lineNo=?"; Object[] obj = new Object[]{ lineNo }; List rows = jdbcTemplate.queryForList( sql, obj ); if(null != rows && rows.size() > 0) { CM_Line line = new CM_Line(); return (CM_Line) line.analyzeMap((Map)rows.get(0)); } else { return null; } } catch (Exception e) { logger.error(e); } return null; }
然后我们调用了他的analyzeMap方法,这个方法把当前对象当作要赋值的对象,然后调用公用方法进行组装:
public Object analyzeMap(Map<String, Object> para){ Object obj = this; ObjectUtil.setValToObj(obj, para); return obj; }
公用方法:
public synchronized static void setValToObj(Object entityName, Map<String, Object> para){ try { Class c = entityName.getClass(); // 获得对象属性 Field field[] = c.getDeclaredFields(); for (Field f : field) { try { PropertyDescriptor pd = new PropertyDescriptor(f.getName(), c); Method writeMethod = pd.getWriteMethod(); if(!CommonCheck.isNullOrEmpty(para.get(f.getName()))) writeMethod.invoke(entityName, para.get(f.getName())); } catch (Exception e) { } } } catch (Exception e) { } }
下面就有人说了,那根据对象获得这个对象的Map怎么搞,这个之前已经写过了,不这里仍然把代码放一下:
/** * 返回一个对象的属性和属性值 */ public synchronized static LinkedHashMap<String,String> getProAndValMap(Object entityName) { LinkedHashMap<String,String> map = new LinkedHashMap<String, String>(); try { Class c = entityName.getClass(); // 获得对象属性 Field field[] = c.getDeclaredFields(); for (Field f : field) { Object v = invokeMethod(entityName, f.getName(), null); if(null != v) map.put(f.getName(), v.toString()); else map.put(f.getName(), ""); } } catch (Exception e) { map = null; } return map; } /** * 获得对象属性的值 */ private synchronized static Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { Class ownerClass = owner.getClass(); methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); Method method = null; try { method = ownerClass.getMethod("get" + methodName); } catch (Exception e) { } return method.invoke(owner); }
同样通过对象的反射,获得属性名称和值。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)