1.新疆测试表stu(使用oracle数据库)
-- Create table create table STU ( id NUMBER not null, name VARCHAR2(20), age NUMBER ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table STU add constraint PJ primary key (ID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );
2.创建实体类Student
package com.baosight.model; /** * <p>Title: </p> * <p>Description:Student </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午12:32:46*/ public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.HashMap; import java.util.Map; import com.baosight.model.Student; /** * <p>Title: </p> * <p>Description:SessionIn </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午6:33:43*/ public class SessionIn { private String tableName = "stu";//表名 private Map<String,String> map;//存放实体类属性名和对应的表里字段名的对应关系 private String MethodName[];//存放实体类属性的get方法 /** * @Title: 构造方法 * @Description: TODO * @param * @return * @throws */ public SessionIn() { map = new HashMap<String, String>(); map.put("id", "id"); map.put("name", "name"); map.put("age", "age"); MethodName = new String[map.size()]; } public void save(Student s) throws Exception{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger"); String sql = createSql(); PreparedStatement ps = con.prepareStatement(sql); for(int i=0;i<MethodName.length;i++){ Method m = s.getClass().getMethod(MethodName[i]); Class returnType = m.getReturnType(); if(returnType.getName().equals("java.lang.String")){ String v = (String) m.invoke(s); ps.setString(i+1, v); } if(returnType.getName().equals("int")){ Integer v = (Integer) m.invoke(s); ps.setInt(i+1, v); } } // ps.setInt(1, s.getId()); ps.executeUpdate(); ps.close(); con.close(); } /** 拼接sql * @Title: createSql * @Description: TODO * @return * @return String * @throws */ private String createSql() { // TODO Auto-generated method stub String str1 = ""; String str2 = ""; int index = 0; for(String s:map.keySet()){ String m = map.get(s); m = "get"+Character.toUpperCase(m.charAt(0))+m.substring(1); MethodName[index] = m; str1+=s+","; str2+="?,"; index++; } str1 = str1.substring(0,str1.length()-1); str2 = str2.substring(0,str2.length()-1); String sql = "insert into "+tableName+"("+str1+") values("+str2+")"; System.out.println(sql); return sql; } }4.新建测试类TestStudent,进行插入测试
import com.baosight.model.Student; /** * <p>Title: </p> * <p>Description:TestStu </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午3:16:01*/ public class TestStudent { /** * @throws Exception * @Title: main * @Description: TODO * @param args * @return void * @throws */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //学生测试类 Student s = new Student(); s.setId(2); s.setName("s2"); s.setAge(40); SessionIn session = new SessionIn(); session.save(s); } }
结果:1 2 s2 40
上例模拟了hibernate内部实现的对sql的封装、传参和执行的过程,主要用到了反射机制。