• hibernate的运行原理


    1. /*hibernate实际是对数据的封装将其封装成持久化对象适当我们不在关心数据库的具体结构用面向对象思想去编写程序hibernate将为我们自动生成sql语句*/  
    2. //下面是一个模拟hibernate具体实现  
    3. //session类的save方法实现其核心是用来Java里面的反射机制  
    4. import java.lang.reflect.Method;  
    5. import java.sql.Connection;  
    6. import java.sql.DriverManager;  
    7. import java.sql.PreparedStatement;  
    8. import java.sql.SQLException;  
    9. import java.util.HashMap;  
    10. import java.util.Map;  
    11.   
    12. public class session {  
    13.     // 本来是从配置文件中读出  
    14.     String tableName = "_tableName";  
    15.     Map<String, String> cfs = new HashMap<String, String>();  
    16.     String[] methedName;  
    17.   
    18.     public session() {  
    19.   
    20.         cfs.put("_id""id");  
    21.         cfs.put("_name""name");  
    22.         cfs.put("_age""age");  
    23.         methedName=new String[cfs.size()];  
    24.   
    25.     }  
    26.   
    27.     public void save(student s) throws Exception {  
    28.   
    29.         String sql = createSQL();  
    30.         Class.forName("com.jdbc.mysql.Driver");  
    31.         Connection con=DriverManager.getConnection("jdbc:mysql://localhost/hibernate","lingkai","818927");  
    32.         PreparedStatement ps=con.prepareStatement(sql);  
    33.         //  
    34.         for(int i=0;i<methedName.length;i++){  
    35.             Method m=s.getClass().getMethod(methedName[i]);//得到是s的一个对象调用getMethod()返回一个方法方法名为数组methedName[i]存的类容  
    36.             Class r=m.getReturnType();//得到该方法的返回的值类型  
    37.             //r.getName()的到该方法的名字  
    38.             if(r.getName().equals("int")){  
    39.                 Integer returnv=(Integer)m.invoke(s);//取出该方法返回的值  
    40.                 ps.setInt(i+1, returnv);  
    41.                   
    42.                   
    43.                   
    44.             }  
    45.             if(r.getName().equals("java.lang.String")){  
    46.                 String returnv=(String)m.invoke(s);  
    47.                 ps.setString(i+1, returnv);  
    48.                 }  
    49.               
    50.             System.out.println(m.getName()+"|"+m.getReturnType());  
    51.               
    52.         }  
    53.         //ps.executeUpdate();  
    54.         ps.close();  
    55.         con.close();  
    56.   
    57.     }  
    58.   
    59.     private String createSQL() {  
    60.         String str1 = "";  
    61.         int index=0;  
    62.         for (String s : cfs.keySet()) {  
    63.             String v=cfs.get(s);  
    64.             v=Character.toUpperCase(v.charAt(0))+v.substring(1);//将第一个字符串转换成大写的字母  
    65.             methedName[index]="get"+v;  
    66.             index++;  
    67.             str1 += s + ",";  
    68.         }  
    69.         str1 = str1.substring(0, str1.length() - 1);  
    70.         System.out.println(str1);  
    71.         String str2 = "";  
    72.         for (int i = 0; i < cfs.size(); i++) {  
    73.             str2 += "?,";  
    74.         }  
    75.         str2 = str2.substring(0, str2.length() - 1);// 截取字符串丢掉最后一个问号  
    76.         System.out.println(str2);  
    77.   
    78.         String sql = "insert into" + tableName + "(" + str1 + ")" + "values"  
    79.                 + "(" + str2 + ")";  
    80.         System.out.println(sql);  
    81.   
    82.         return null;  
    83.     }  
    84.   
    85. }  
    86.   
    87. //测试类的代码只需直接调用session的save方法  
    88. import java.sql.SQLException;  
    89.   
    90.   
    91. public class test {  
    92.       
    93.     public static void main(String[] args) throws Exception {  
    94.         student stu=new student();  
    95.         stu.setId(1);  
    96.         stu.setName("lingkai");  
    97.         stu.setAge(21);  
    98.           
    99.         session s=new session();  
    100.         s.save(stu);  
    101.           
    102.           
    103.     }  
    104.       
    105.       
    106.   
    107. }  
  • 相关阅读:
    cout的输出格式初探
    CVPR 2015 papers
    C语言的32个保留字
    读取siftgeo格式文件的matlab程序
    (转)各类排序算法总结
    被除数、除数、商、余数的正负号规律二
    被除数、除数、商、余数的正负号规律一
    FCKEditor上传图片word
    CKEditor上传图片word
    在线编辑器上传图片word
  • 原文地址:https://www.cnblogs.com/xulzong/p/3144401.html
Copyright © 2020-2023  润新知