模拟Hibernate框架的小demo
该程序为尚学堂马士兵老师讲解,模拟了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的更多相关文章
- RPC框架学习+小Demo实例
一.什么是RPC协议? 全称:远程过程调度协议 效果:使消费者向调用本地方法一样调用远程服务方法,对使用者透明 目前常用:Dubbo.Thirft.Sofa.... 功能: 建立远程通信(socket ...
- struts2和hibernate整合的小Demo
jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...
- hibernate框架学习笔记1:搭建与测试
hibernate框架属于dao层,类似dbutils的作用,是一款ORM(对象关系映射)操作 使用hibernate框架好处是:操作数据库不需要写SQL语句,使用面向对象的方式完成 这里使用ecli ...
- 一个用户管理的ci框架的小demo--转载
一个ci框架的小demo 最近在学习ci框架,作为一个初学者,在啃完一遍官方文档并也跟着官方文档的例程(新闻发布系统)做了一遍,决定在将之前练习PHP与MySQL数据库的用户管理系统再用ci框架实现一 ...
- hibernate框架的搭建
1 导入所需的jar包 1 导入hibernate必须的jar包 2 导入驱动包 2 创建数据库,准备表,实体 1 创建hibernate数据库 CREATE DATABASE hibernate; ...
- Hibernate框架学习(一)——入门
一.框架是什么 1.框架是用来提高开发效率的 2.封装好了一些功能,我们需要使用这些功能时,调用即可,不需要手动实现 3.框架可以理解成一个半成品的项目,只要懂得如何驾驭这些功能即可 二.hibern ...
- (01)hibernate框架环境搭建及测试
---恢复内容开始--- 1.创建javaweb项目 2.导包 hibernate包 hibernate\lib\required\*.jar 数据库驱动包 mysql-connector-java- ...
- 采用dom4j和反射模拟Spring框架的依赖注入功能
Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...
- 初识hibernate框架之一:进行简单的增删改查操作
Hibernate的优势 l 优秀的Java 持久化层解决方案 (DAO) l 主流的对象—关系映射工具产品 l 简化了JDBC 繁琐的编码 l 将数据库的连接信息都存放在配置文件 l 自己的ORM ...
随机推荐
- iOS 监听键盘变化
//将要显示键盘 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowKeyboard: ...
- UVa10082 WERTYU
#include <stdio.h>#include <string.h> int main(){ // 用C++提交AC char s[] = "`12345 ...
- this function has none of deterministic, no sql,or reads sql data in its declaration and binary logging is enabled
原址:http://blog.chinaunix.net/uid-20639775-id-3031821.html This function has none of DETERMINISTI ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- JVM典型配置
堆大小设置: JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存 限制.32位系统下,一般限制在1.5G~2G:64为 ...
- c++builder 重载WindowProc、WndProc 截获消息(比Delphi多一个Message Map方法)
c++builder 重载WindowProc.WndProc 截获消息 方法一WindowProc void __fastcall myWindowProc(Messages::TMessage ...
- 在GridView中实现全选反选的例子
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView控件.asp ...
- elasearch 版本控制
http://192.168.32.81:9200/library/books/8/ GET { "_index": "library", "_typ ...
- 动态链接库dll,静态链接库lib, 导入库lib
转载地址:http://www.cnblogs.com/chio/archive/2008/08/05/1261296.html 目前以lib后缀的库有两种,一种为静态链接库(Static Libar ...
- SpringMVC静态文件(图片)访问+js访问 简单小例子
项目文件布局: web.xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app vers ...