模拟Hibernate动态生成SQL语句
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了模拟Hibernate动态生成SQL语句,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9817字,纯文字阅读大概需要15分钟。
内容图文
![模拟Hibernate动态生成SQL语句](/upload/InfoBanner/zyjiaocheng/1242/f155d38204924d7390a4a17807a0b37e.jpg)
这里有一个xml配置文件,也就是Hibernate框架中会用到的POJO和数据库的映射文件
1 <? xml version="1.0" encoding="utf-8" ?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > 4 <!-- 5 Mapping file autogenerated by MyEclipse Persistence Tools 6 --> 7 < hibernate-mapping > 8 < class name ="hp.pojo.Member" table ="member" catalog ="hedb" > 9 < id name ="mid" type ="java.lang.String" > 10 < column name ="mid" length ="50" /> 11 < generator class ="assigned" ></ generator > 12 </ id > 13 < property name ="name" type ="java.lang.String" > 14 < column name ="name" length ="50" /> 15 </ property > 16 < property name ="age" type ="java.lang.Integer" > 17 < column name ="age" /> 18 </ property > 19 < property name ="salary" type ="java.lang.Double" > 20 < column name ="salary" precision ="22" scale ="0" /> 21 </ property > 22 < property name ="birthday" type ="java.util.Date" > 23 < column name ="birthday" length ="0" /> 24 </ property > 25 < property name ="note" type ="java.lang.String" > 26 < column name ="note" length ="65535" /> 27 </ property > 28 </ class > 29 </ hibernate-mapping >
POJO类如下:
1 package hp.pojo; 2 3 import java.util.Date; 4 5 /** 6 * Member entity. @author MyEclipse Persistence Tools 7 */ 8 9 @SuppressWarnings("serial") 10publicclass Member implements java.io.Serializable 11{ 12 13// Fields 14 15private String mid; 16private String name; 17private Integer age; 18private Double salary; 19private Date birthday; 20private String note; 21 22// Constructors 23 24/** default constructor */ 25public Member() 26 { 27 } 28 29/** minimal constructor */ 30public Member(String mid) 31 { 32this.mid = mid; 33 } 34 35/** full constructor */ 36public Member(String mid, String name, Integer age, Double salary, Date birthday, String note) 37 { 38this.mid = mid; 39this.name = name; 40this.age = age; 41this.salary = salary; 42this.birthday = birthday; 43this.note = note; 44 } 45 46// Property accessors 47 48public String getMid() 49 { 50returnthis.mid; 51 } 52 53publicvoid setMid(String mid) 54 { 55this.mid = mid; 56 } 57 58public String getName() 59 { 60returnthis.name; 61 } 62 63publicvoid setName(String name) 64 { 65this.name = name; 66 } 67 68public Integer getAge() 69 { 70returnthis.age; 71 } 72 73publicvoid setAge(Integer age) 74 { 75this.age = age; 76 } 77 78public Double getSalary() 79 { 80returnthis.salary; 81 } 82 83publicvoid setSalary(Double salary) 84 { 85this.salary = salary; 86 } 87 88public Date getBirthday() 89 { 90returnthis.birthday; 91 } 92 93publicvoid setBirthday(Date birthday) 94 { 95this.birthday = birthday; 96 } 97 98public String getNote() 99 { 100returnthis.note; 101 } 102103publicvoid setNote(String note) 104 { 105this.note = note; 106 } 107108 }
下面代码演示了利用反射以及XML解析,动态生成SQL语句,并且利用JDBC技术执行SQL语句的过程:
1 package hp.test; 2 3 import java.io.File; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method; 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.util.ArrayList; 9 import java.util.Date; 10 import java.util.List; 11 12 13 14 15 16 import org.dom4j.Document; 17 import org.dom4j.Element; 18 import org.dom4j.io.SAXReader; 19 20 import com.mysql.jdbc.PreparedStatement; 21 22 public class MyHibernate 23 { 24 private class Column 25 { 26 private String name; 27 private String length; 28 } 29 30 private class Generator 31 { 32 private String type; 33 } 34 35 private class Id 36 { 37 private String name; 38 private String type; 39 40 private Column column; 41 private Generator generator; 42 } 43 private class Property 44 { 45 private String name; 46 private String type; 47 private Column column; 48 } 49 50 private class Class 51 { 52 private String name; 53 private String table; 54 private String catalog; 55 56 private Id id; 57 private List<Property> allProperties; 58 } 59privateclass KeyValuePair 60 { 61private String type; 62private Object value; 63 64private KeyValuePair(String type,Object value) 65 { 66this.type = type; 67this.value = value; 68 } 69 } 70 71private String configPath ; 72private Object obj = null; 73private Document document; 74private Class voClass ; 75private String sql; 76private List<KeyValuePair> allValues; 77 78 79public MyHibernate(String configPath) 80 { 81this.configPath = configPath; 82 } 83 84publicint save(Object obj) throws Exception 85 { 86this.obj = obj; 87this.load(); 88this.format(); 89this.generateSql(); 90returnthis.saveToDb(); 91 } 92 93publicvoid load() throws Exception 94 { 95 SAXReader saxReader = new SAXReader(); 96this.document = saxReader.read(new File(this.configPath)); 97 } 98publicvoid format() throws Exception 99 { 100 Class _class = new Class(); 101 org.dom4j.Element root = this.document.getRootElement(); 102 Element classElement = root.element("class"); 103 _class.name = classElement.attributeValue("name"); 104 _class.table = classElement.attributeValue("table"); 105 _class.catalog = classElement.attributeValue("catalog"); 106107 Element idElement = classElement.element("id"); 108 _class.id = new Id(); 109 _class.id.name = idElement.attributeValue("name"); 110 _class.id.type = idElement.attributeValue("type"); 111112 Element columnElementInId = idElement.element("column"); 113 _class.id.column = new Column(); 114 _class.id.column.name = columnElementInId.attributeValue("name"); 115 _class.id.column.length = columnElementInId.attributeValue("length"); 116117 Element generatorElement = idElement.element("generator"); 118 _class.id.generator = new Generator(); 119 _class.id.generator.type = generatorElement.attributeValue("class"); 120121 List<Property> allProperties = new ArrayList<MyHibernate.Property>(); 122 _class.allProperties = allProperties; 123124 List<Element> allPropertiesElements = classElement.elements("property"); 125for(Element item : allPropertiesElements) 126 { 127 Property property = new Property(); 128 property.name = item.attributeValue("name"); 129 property.type = item.attributeValue("type"); 130131 Element columnElement = item.element("column"); 132 property.column = new Column(); 133 property.column.name = columnElement.attributeValue("name"); 134 property.column.length = columnElement.attributeValue("length"); 135 allProperties.add(property); 136//System.out.println("name: " + property.name);137 } 138this.voClass = _class; 139 } 140141publicvoid generateSql() throws Exception 142 { 143this.allValues = new ArrayList<MyHibernate.KeyValuePair>(); 144 StringBuffer columns = new StringBuffer(); 145 StringBuffer values = new StringBuffer(); 146 StringBuffer sql = new StringBuffer(" INSERT INTO "); 147 sql.append(this.voClass.table).append("( "); 148149if("assigned".equals(this.voClass.id.generator.type)) 150 { 151//需要用户提供主键列152 columns.append(this.voClass.id.column.name).append(","); 153 values.append("?,"); 154155 Field field = this.obj.getClass().getDeclaredField(this.voClass.id.name); 156 field.setAccessible(true); 157this.allValues.add(new KeyValuePair(this.voClass.id.type, field.get(this.obj))); 158 } 159160for(Property property : this.voClass.allProperties) 161 { 162 columns.append(property.column.name).append(","); 163 values.append("?,"); 164165 Field field = this.obj.getClass().getDeclaredField(property.name); 166 field.setAccessible(true); 167this.allValues.add(new KeyValuePair(property.type, field.get(this.obj))); 168 } 169170 columns.delete(columns.length()-1, columns.length()); 171 values.delete(values.length()-1, columns.length()); 172173 sql.append(columns.toString()).append(") VALUES ("); 174 sql.append(values.toString()).append(")"); 175this.sql = sql.toString(); 176 System.out.println(this.sql); 177 } 178179publicint saveToDb() throws Exception 180 { 181 java.lang.Class.forName("org.gjt.mm.mysql.Driver"); 182 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hedb", "root", "admin"); 183 java.sql.PreparedStatement ps = conn.prepareStatement(this.sql); 184185for(int i = 0; i < this.allValues.size(); i++) 186 { 187//System.out.println("***type:" + this.allValues.get(i).type);188if(String.class.getName().toString().equals(this.allValues.get(i).type)) 189 { 190 ps.setString(i+1, (String)this.allValues.get(i).value); 191 } 192elseif(Integer.class.getName().toString().equals(this.allValues.get(i).type)) 193 { 194 ps.setInt(i+1, (Integer)this.allValues.get(i).value); 195 }elseif(Double.class.getName().toString().equals(this.allValues.get(i).type)) 196 { 197 ps.setDouble(i+1, (Double)this.allValues.get(i).value); 198 }elseif(Date.class.getName().toString().equals(this.allValues.get(i).type)) 199 { 200 ps.setDate(i+1, new java.sql.Date(((Date)this.allValues.get(i).value).getTime())); 201 } 202203//System.out.println("设置第" + (i+1) + "个值("+this.allValues.get(i).type+","+this.allValues.get(i).value+")");204 } 205return ps.executeUpdate(); 206207 } 208 }
测试代码:
1 package hp.test; 2 3 import java.util.Date; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 8 import hp.pojo.Member; 9 import hp.utils.HibernateHelper; 10 11 public class Main 12 { 13 14 public static void main(String[] args) 15 { 16 Member vo = new Member(); 17 vo.setMid("admin4"); 18 vo.setAge(24); 19 vo.setBirthday(new Date()); 20 vo.setName("sheldon4"); 21 vo.setNote("a good person4"); 22 vo.setSalary(44444.44); 2324try25 { 26 MyHibernate mh = new MyHibernate("C:\\D\\code\\resource\\mapping.xml"); 27int count = mh.save(vo); 28 System.out.println("成功插入" + count + "行数据"); 29 } 30catch(Exception e) 31 { 32 e.printStackTrace(); 3334 } 35 System.out.println("main done//~"); 36 } 3738 }
原文:http://www.cnblogs.com/kuillldan/p/6266344.html
内容总结
以上是互联网集市为您收集整理的模拟Hibernate动态生成SQL语句全部内容,希望文章能够帮你解决模拟Hibernate动态生成SQL语句所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。