自己动手写ORM框架-java
Posted 狼王不乖s
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己动手写ORM框架-java相关的知识,希望对你有一定的参考价值。
利用xml做配置文件,可以动态生成sql:
Java代码- package com.landray.kmss.util;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- import org.mira.lucene.analysis.c;
- import org.springframework.jdbc.core.JdbcTemplate;
- import com.landray.kmss.sys.transport.model.Exam;
- /**
- * createBy Zhang Yanan
- *
- * createTime 2012-6-14 下午03:53:19
- *
- * desc 类和表的映射工具
- *
- */
- public class MappingUtil
- private JdbcTemplate jdbcTemplate;
- private JdbcTemplate jdbcTemplateAssist;
- public MappingUtil(JdbcTemplate jdbcTemplate,JdbcTemplate Assist)
- this.jdbcTemplate = jdbcTemplate;
- this.jdbcTemplateAssist = jdbcTemplateAssist;
- public MappingUtil()
- public JdbcTemplate getJdbcTemplate()
- return jdbcTemplate;
- public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
- this.jdbcTemplate = jdbcTemplate;
- public JdbcTemplate getJdbcTemplateAssist()
- return jdbcTemplateAssist;
- public void setJdbcTemplateAssist(JdbcTemplate jdbcTemplateAssist)
- this.jdbcTemplateAssist = jdbcTemplateAssist;
- //从Object[] 转为Bean
- public Object fromObject2Bean(Class c ,Object[] o) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
- Object ob = c.newInstance();
- Method[] f = c.getMethods();
- String path = this.getXmlFileName(c);
- Map map = this.readXml(path);
- for(int i=0;i<f.length;i++)
- Method method = f[i];
- String methodName = method.getName();
- if(methodName.startsWith("set"))
- String fieldName = methodName.replace("set", "");
- String upChar = fieldName.charAt(0)+"";
- fieldName=fieldName.replace(upChar,upChar.toLowerCase());
- String order = (String)map.get(fieldName);
- method.invoke(ob, o[Integer.valueOf(order)-1]);
- return ob;
- //获取class获取映射文件位置
- private String getXmlFileName(Class c)
- String path = "";
- String xml = c.getName().substring((c.getName().lastIndexOf("."))+1)+".xml";
- path = c.getResource(xml).toString();
- return path;
- //读取xml,把属性,顺序放入map
- private Map readXml(String path)
- Map map = new HashMap();
- SAXBuilder sb = new SAXBuilder();
- try
- Document myDocument = sb.build(path);
- Element root = myDocument.getRootElement();
- Element table = root.getChild("table");
- List<Element> list = table.getChildren("property");
- for(Element e:list)
- String order = e.getAttributeValue("order");
- String name = e.getAttributeValue("name");
- map.put(name, order);
- catch (JDOMException e)
- e.printStackTrace();
- catch (IOException e)
- e.printStackTrace();
- return map;
- //读取xml,把属性名,表字段放入map
- private Map readXmlForsql(String path)
- Map map = new HashMap();
- SAXBuilder sb = new SAXBuilder();
- try
- Document myDocument = sb.build(path);
- if(myDocument==null)
- throw new RuntimeException("请检查xml的路径");
- Element root = myDocument.getRootElement();
- Element table = root.getChild("table");
- List<Element> list = table.getChildren("property");
- String tableName = table.getAttributeValue("name");
- map.put("table_name", tableName);
- for(Element e:list)
- String column = e.getAttributeValue("column");
- String name = e.getAttributeValue("name");
- map.put(name, column);
- catch (JDOMException e)
- e.printStackTrace();
- catch (IOException e)
- e.printStackTrace();
- return map;
- //根据对象转换插入sql语句
- public String fromBean2InsertSql(Object o)
- StringBuilder sb = new StringBuilder();
- // System.out.println(o.getClass());
- String path = this.getXmlFileName(o.getClass());
- Map map = this.readXmlForsql(path);
- sb.append("insert into "+map.get("table_name"));
- map.remove("table_name");
- Set set = map.entrySet();
- Iterator it = set.iterator();
- StringBuilder columns = new StringBuilder();
- StringBuilder values = new StringBuilder();
- columns.append("(");
- values.append("(");
- while(it.hasNext())
- Map.Entry e = (Map.Entry)it.next();
- columns.append(e.getValue()+",");
- Object ob = this.getValueOfObject(o, e.getKey().toString());
- if(null == ob)
- values.append("null,");
- else
- values.append("'"+ob+"',");
- String cStr = columns.toString().substring(0, columns.toString().length()-1);
- String vStr = values.toString().substring(0,values.toString().length()-1);
- sb.append(cStr+")");
- sb.append(" values ");
- sb.append(vStr+")");
- return sb.toString();
- //根据属性名称获取对象该属性的值
- public Object getValueOfObject(Object ob,String field)
- Object o = new Object();
- String s = field.substring(0,1);
- String fieldNew = s.toUpperCase()+field.substring(1,field.length());
- String method = "get"+fieldNew;
- try
- try
- Method m = ob.getClass().getMethod(method);
- o = m.invoke(ob);
- catch (IllegalArgumentException e)
- e.printStackTrace();
- catch (IllegalAccessException e)
- e.printStackTrace();
- catch (InvocationTargetException e)
- e.printStackTrace();
- 自己动手写ORM的感受