ORM(Object Relation Mapping)
对象关系模型
关系模型(数据库表): 表、字段、字段类型
对象模型(java实体类):类、属性、属性类型
通过ORM框架解除模型之间的阻抗。
利用反射实现ORM中的准备预编译SQL语句
- 自定义注解:
Column.java:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.xzy.ORM.annotation;
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 Column { String name(); }
|
PK.java:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.xzy.ORM.annotation;
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 PK { boolean isAuto() default true; }
|
table.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.xzy.ORM.annotation;
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 Table { String name(); }
|
- 实体类:User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package com.xzy.ORM.entity;
import com.xzy.ORM.annotation.Column; import com.xzy.ORM.annotation.PK; import com.xzy.ORM.annotation.Table;
import java.io.Serializable;
@Table(name = "users") public class User implements Serializable {
@PK(isAuto = false) @Column(name = "user_id") private Integer id;
@Column(name = "user_name") private String name;
private String email; private String country;
@Column(name = "user_password") private String password;
public void setId(int id) { this.id = id; }
public Integer getId() { return id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getCountry() { return country; }
public void setCountry(String country) { this.country = country; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; } }
|
- Dao层实现SQL语句:
UserDao.java:
1 2 3 4 5
| package com.xzy.ORM.dao;
public interface UserDao { public int save(Object obj) throws Exception; }
|
UserDaoImpl.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package com.xzy.ORM.dao;
import com.xzy.ORM.annotation.Column; import com.xzy.ORM.annotation.Table;
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List;
public class UserDaoImpl implements UserDao { @Override public int save(Object obj) throws Exception {
String sql = prepareSQL(obj);
return 0; }
private String prepareSQL(Object obj) throws Exception { StringBuffer stringBuffer = new StringBuffer("INSERT INTO "); Class<?> clazz = obj.getClass(); String tablename = clazz.getSimpleName().toUpperCase(); Table table = clazz.getAnnotation(Table.class); if (table != null) { tablename = table.name().toUpperCase(); } stringBuffer.append(tablename + "("); Field[] fields = clazz.getDeclaredFields(); List<Object> valueList = new ArrayList<>(); for (Field field:fields){ String firldName = field.getName().toUpperCase(); Column column = field.getAnnotation(Column.class); if (column!=null){ firldName = column.name().toUpperCase(); } field.setAccessible(true); Object value = field.get(obj); if (value!=null){ stringBuffer.append(firldName+","); valueList.add(value); } } stringBuffer.deleteCharAt(stringBuffer.length()-1); stringBuffer.append(") VALUES ("); for (Object value:valueList){ Object temp = value; if (temp instanceof String){ temp = "'"+value+"'"; } stringBuffer.append(temp+","); } stringBuffer.deleteCharAt(stringBuffer.length()-1); stringBuffer.append(")");
System.out.println(stringBuffer); return null; } }
|
- 启动类:App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.xzy.ORM.app;
import com.xzy.ORM.dao.UserDaoImpl; import com.xzy.ORM.entity.User;
public class App { public static void main(String[] args) { User user = new User(); user.setId(3); user.setName("Jack"); user.setEmail("jack@google.com"); user.setCountry("US"); user.setPassword("12546"); UserDaoImpl userDao = new UserDaoImpl(); try { userDao.save(user); } catch (Exception e) { e.printStackTrace(); } } }
|