该程序为尚学堂马士兵老师讲解,模拟了hibernate的原理,主要应用了字符串拼接,反射知识。

step1,新建数据库

use jd;
create table _student(
_id int(11),
_nage varchar(20),
_age int(11));

step 2 student实体类,再次略过

step3,编写session类,模拟hibernate的实现原理。

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; import com.bjsxt.hibernate.model.Student; public class Session {
static String tableName = "_Student";
static Map<String, String> cfs = new HashMap<String, String>();
static String[] methodNames; public Session() {
cfs.put("_id", "id");
cfs.put("_name", "name");
cfs.put("_age", "age");
methodNames = new String[cfs.size()];
} public static void save(Student s) throws Exception {
// TODO Auto-generated method stub
int index = 0;
String sql = createSql(); try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/jd", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql); for (int i = 0; i < methodNames.length; i++) {
Method m = s.getClass().getMethod(methodNames[i]);
Class r = m.getReturnType();
if (r.getName().equals("java.lang.String")) {
String returnValue = (String) m.invoke(s);
ps.setString(i + 1, returnValue);
System.out.println(returnValue);
}
if (r.getName().equals("int")) {
Integer returnValue = (Integer) m.invoke(s);
ps.setInt(i + 1, returnValue);
System.out.println(returnValue);
}
// System.out.println(m.getName()+","+m.getReturnType()+","+r.getName());
}
System.out.println(sql);
ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } private static String createSql() { String str2 = "";
String str1 = "";
int index = 0;
for (String s : cfs.keySet()) {//s为_id,_Age,_Name
String v = cfs.get(s);
v = Character.toUpperCase(v.charAt(0)) + v.substring(1);//Id,Name,Age
//System.out.println(v+"涛");
methodNames[index] = "get" + v; // str1+=s+",";//getId,getName,getAge
//System.out.println(s);
//System.out.println(methodNames[index]);
str1 += s + ",";
index++;
System.out.println(str1);
}
str1 = str1.substring(0, str1.length() - 1);
//System.out.println(str1);
for (int i = 0; i < cfs.size(); i++) { if (i == cfs.size() - 1) {
str2 += "?";
} else {
str2 += "?,";
} }
//System.out.println(str2);
String sql = "insert into " + tableName + "(" + str1 + ")"
+ " values (" + str2 + ")";
System.out.println(sql+"createSql方法里面");
return sql;
} }

step4,测试,

import com.bjsxt.hibernate.model.Student;
public class StudentTest { public static void main(String[] args) throws Exception { Student s = new Student();
s.setId(10);
s.setName("s1");
s.setAge(1);
Session session = new Session();
session.save(s);
}
}

模拟Hibernate框架的小demo的更多相关文章

  1. RPC框架学习+小Demo实例

    一.什么是RPC协议? 全称:远程过程调度协议 效果:使消费者向调用本地方法一样调用远程服务方法,对使用者透明 目前常用:Dubbo.Thirft.Sofa.... 功能: 建立远程通信(socket ...

  2. struts2和hibernate整合的小Demo

    jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...

  3. hibernate框架学习笔记1:搭建与测试

    hibernate框架属于dao层,类似dbutils的作用,是一款ORM(对象关系映射)操作 使用hibernate框架好处是:操作数据库不需要写SQL语句,使用面向对象的方式完成 这里使用ecli ...

  4. 一个用户管理的ci框架的小demo--转载

    一个ci框架的小demo 最近在学习ci框架,作为一个初学者,在啃完一遍官方文档并也跟着官方文档的例程(新闻发布系统)做了一遍,决定在将之前练习PHP与MySQL数据库的用户管理系统再用ci框架实现一 ...

  5. hibernate框架的搭建

    1 导入所需的jar包 1 导入hibernate必须的jar包 2 导入驱动包 2 创建数据库,准备表,实体 1 创建hibernate数据库 CREATE DATABASE hibernate; ...

  6. Hibernate框架学习(一)——入门

    一.框架是什么 1.框架是用来提高开发效率的 2.封装好了一些功能,我们需要使用这些功能时,调用即可,不需要手动实现 3.框架可以理解成一个半成品的项目,只要懂得如何驾驭这些功能即可 二.hibern ...

  7. (01)hibernate框架环境搭建及测试

    ---恢复内容开始--- 1.创建javaweb项目 2.导包 hibernate包 hibernate\lib\required\*.jar 数据库驱动包 mysql-connector-java- ...

  8. 采用dom4j和反射模拟Spring框架的依赖注入功能

    Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...

  9. 初识hibernate框架之一:进行简单的增删改查操作

    Hibernate的优势 l 优秀的Java 持久化层解决方案  (DAO) l 主流的对象—关系映射工具产品 l 简化了JDBC 繁琐的编码 l 将数据库的连接信息都存放在配置文件 l 自己的ORM ...

随机推荐

  1. 简单的mvvm light 应用

      public  class MainStudentModel:ViewModelBase    { //实体        private StudentModel stu = new Stude ...

  2. JS字符串方法总结整理

    //javascript字符串方法总结   1.String.charAt(n)      //取得字符串中的第n个字符   2.String.charCodeAt(n)  //取得字符串中第n个字符 ...

  3. PHP获取网址的PR值

    PR值是google衡量网站的重要标准之一,根据google提供的结果获取pr值,如:http://toolbarqueries.google.com.hk/tbr?client=navclient- ...

  4. [LeetCode]题解(python):122-Best Time to Buy and Sell Stock II

    题目来源: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意分析: 和上题类似,给定array,代表第i天物品i ...

  5. ios如何实现推送通知

    推送通知的步骤:1.询问是否允许推送通知.2.如果用户允许在APPDELEGATE 中实现 - (void)application:(UIApplication *)application didRe ...

  6. Anatomy of a Program in Memory

    Memory management is the heart of operating systems; it is crucial for both programming and system a ...

  7. freebsd

    #cd /usr/ports/devel/binutils && make install

  8. 12个高矮不同的人,排成两排(catalan数)

    问题描述: 12个高矮不同的人,排成两排,每排必须是从矮到高排列,而且第二排比对应的第一排的人高,问排列方式有多少种? 这个笔试题,很YD,因为把某个递归关系隐藏得很深. 问题分析: 我们先把这12个 ...

  9. ++i和i++哪个效率高?

    这个问题需要分两种情况来解说: 1.当变量i的数据类型是c++语言默认提供的类型的话,他们的效率是一样的. int a,i=0;     a=++i;汇编代码如下: int a,i=0; 01221A ...

  10. C#后台发送HTTP请求

    using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using Syst ...