java 从零实现简易版本的 hibernate
-
简单版本的 hibernate
-
建表语句生成+执行
-
插入语句生成+执行
jdk1.3+
maven 3.x+
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>hibernate</artifactId>
<version>0.1.0</version>
</dependency>
首先准备一个 mysql 对应的测试库 hibernate
mysql> create database hibernate;
Query OK, 1 row affected (0.00 sec)
mysql> use hibernate;
Database changed
我们配置一下 hibernate.cfg.xml
为对应的数据库访问信息:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
</hibernate-configuration>
SQL 的生成:
Table table = new Table();
User user = new User();
System.out.println(table.buildCreateTableSQL(user));
结果为:
create table t_user (id BIGINT AUTO_INCREMENT, name VARCHAR(255), password VARCHAR(255), myAge INTEGER , createOn DATETIME, modifiedOn DATETIME , PRIMARY KEY (`id`) );
我们可以直接通过程序建表
Session session = new Session();
Table table = new Table();
User user = new User();
Connection connection = session.createConnection();
PreparedStatement preparedStatement = connection.prepareStatement(table.buildCreateTableSQL(user));
preparedStatement.execute();
执行后:
mysql> show tables;
+---------------------+
| Tables_in_hibernate |
+---------------------+
| t_user |
+---------------------+
1 row in set (0.00 sec)
表结构
mysql> desc t_user;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| myAge | int(11) | YES | | NULL | |
| createOn | datetime | YES | | NULL | |
| modifiedOn | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
PS: 这里可以看到对应的字段没有驼峰转下划线
User user = new User();
user.setId(3L);
user.setName("ryo");
user.setAge(21);
user.setPassword("123456");
user.setCreateOn(new Date());
user.setModifiedOn(new Date());
System.out.println(new Session().buildInsertSQL(user));
结果为:
INSERT INTO t_user ( id,name,password,myAge,createOn,modifiedOn ) VALUES ( '3','ryo','123456','21','2024-07-30 00:37:36','2024-07-30 00:37:36' ) ;
也可以程序直接插入
User user = new User();
user.setId(3L);
user.setName("ryo");
user.setAge(21);
user.setPassword("123456");
user.setCreateOn(new Date());
user.setModifiedOn(new Date());
new Session().save(user);
效果
mysql> select * from t_user;
+----+------+----------+-------+---------------------+---------------------+
| id | name | password | myAge | createOn | modifiedOn |
+----+------+----------+-------+---------------------+---------------------+
| 3 | ryo | 123456 | 21 | 2024-07-30 00:38:14 | 2024-07-30 00:38:14 |
+----+------+----------+-------+---------------------+---------------------+
1 row in set (0.00 sec)
-
添加基于 gen-case 的测试用例
-
建表对应的字段没有驼峰转下划线
-
CRUD
-
list 等常见查询
手写 Hibernate ORM 框架-01-注解定义、常量定义
手写 Hibernate ORM 框架-02-实体 Bean 定义,建表语句自动生成
手写 Hibernate ORM 框架-03-Hibernate 配置文件读取, 数据库连接构建