Learn ORM mapping framework implementation principle

Before we read with a few minor problems to read the content of the following text, the paper Hibernateframework for understanding ORMimplement the principles framework map.

  1. ORMMapping framework entity(entity classes) name and the table name is inconsistent with how the corresponding database table names?
  2. entity(Entity classes) in the property takes 驼峰命名to: userName, and database table user_Nameis how the corresponding?
  3. Why Hibernateby entity(entity classes) will be able to get a complete SQLsentence?

Here we have completed a ORMframework entity(entity classes) is inconsistent with the table field, was able to generate a complete SQL statement of the case.

In use Hibernatethe frame, we will create an entity class, create database tables and fields to the corresponding fields will be used to the core of the two notes, @Tableand @Columnother @Entity, @Idand other notes we first ignore, focusing on the principle.

@Table Notes
The notes make when using the entity class name corresponds to the table, @Table("user_table")this is equivalent to comment fact played the role of an alias, we can try to customize a comment imitate restore the annotations.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTable {
	
	String value();
	
}

About custom annotations , I do not do too much explanation.

@Column Annotations
The annotation for the entity class field, the field corresponding to the alias, and so the database table columns ( Columnie: column) corresponds. We went to two and a custom @Columnannotation considerable comment.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyColumn {

	String columnName();
	
	int columnLength();
	
	String columnType();
	
}

Entity class

@MyTable("user_table")
public class User {

	@MyColumn(columnName="user_name",columnLength=5,columnType="String")
	private String userName;
	
	@MyColumn(columnName="user_age",columnLength=5,columnType="int")
	private int userAge;
	
	@MyColumn(columnName="user_id",columnLength=5,columnType="int")
	private int userId;
	
	@MyColumn(columnName="user_gender",columnLength=1,columnType="int")
	private int gender;

	// 省略get/set/构造/toString等方法
	
}

Now we have completed the two core annotated entity classes and also the corresponding database table, then the next step is to generate entity classes SQL.

SQL generation
want to generate a complete SQL, you need to get the table names, field names. In just two notes and custom database tables that we have done a mapped. So how do we get it? We can 反射!

import java.lang.reflect.Field;

public class MyOrmTest {

	public static void main(String[] args) throws ClassNotFoundException {
		System.out.println(selectUserTable());
	}

	public static String selectUserTable() throws ClassNotFoundException {

		// 通过反射获取到Class
		Class<?> classForName = Class.forName("orm.test.User");
		// 获取注解@MyTable
		MyTable table = classForName.getAnnotation(MyTable.class);
		// 获取到类的所有字段
		Field[] Fields = classForName.getDeclaredFields();

		// 拼接SQL
		StringBuffer sb = new StringBuffer();
		sb.append(" select ");

		// 获取注解@MyTable的值
		String tableName = table.value();

		// 迭代类中所有字段,获取字段上注解@MyColumn的值并拼接SQL
		for (int i = 0; i < Fields.length; i++) {
			Field field = Fields[i];
			MyColumn column = field.getAnnotation(MyColumn.class);
			sb.append(" " + column.columnName() + " ");
			if (i == Fields.length - 1) {
				sb.append(" from ");
			} else {
				sb.append(",");
			}
		}
		sb.append(" " + tableName);

		return sb.toString();
	}
}

ORMMany details, such as the field type of treatment and so on, not to understand, and we understand ORMand core principles can be.

Published 13 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/QingHe97/article/details/103837236