Java SimpleORM 使用介绍
不管你是使用Hibernate还是Mybatis还是什么ORM框架,其最底层都离不开对SQL语句的封装,因此笔者打算自己封装一个简单的Java ORM框架。
程序已经写好,下载地址:Java SimpleORM下载
下面作简单的介绍。
首先用annotation来标记列:
package com.simpleorm.entity;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 用于指定Class中与表的字段对应的属性
* @author Linli
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
}
接下来标记表:DataMember
package com.simpleorm.entity;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 定义实体类的数据库表属性
* @author Shinry
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface DataMember {
/**
* 数据库表名
* @return
*/
String TableName();
/**
* 主键集合
* @return
*/
String[] PrimaryKey() default "";
/**
* 自增列
* @return
*/
String AutoIncr() default "";
}
数据库操作在包:com.simpleorm.dao 包中
SQL语句的拼装在:com.simpleorm.services 包中
测试实例在:com.simpleorm.test 包中
生成pojo类的工具下载:pojo类生成工具下载
生成:
其中生成的Pojo类例子如下:
package com.simpleorm.test;
import java.math.BigDecimal;
import java.util.Date;
import com.simpleorm.entity.Column;
import com.simpleorm.entity.DataMember;
@DataMember(TableName="data",PrimaryKey={"id","bInt"},AutoIncr="id")
public class Data{
@Column
private int id;
@Column
private Integer bInt;
@Column
private String bString;
@Column
private boolean sBool;
@Column
private Boolean bBool;
@Column
private char sChar;
@Column
private Character bChar;
@Column
private Date bDate;
@Column
private BigDecimal bigDecimal;
@Column
private double sDouble;
@Column
private Double bDouble;
@Column
private float sFloat;
@Column
private Float bFloat;
private String moreDemo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getbInt() {
return bInt;
}
public void setbInt(Integer bInt) {
this.bInt = bInt;
}
public String getbString() {
return bString;
}
public void setbString(String bString) {
this.bString = bString;
}
public boolean issBool() {
return sBool;
}
public void setsBool(boolean sBool) {
this.sBool = sBool;
}
public Boolean getbBool() {
return bBool;
}
public void setbBool(Boolean bBool) {
this.bBool = bBool;
}
public char getsChar() {
return sChar;
}
public void setsChar(char sChar) {
this.sChar = sChar;
}
public Character getbChar() {
return bChar;
}
public void setbChar(Character bChar) {
this.bChar = bChar;
}
public Date getsDate() {
return bDate;
}
public void setbDate(Date bDate) {
this.bDate = bDate;
}
public BigDecimal getBigDecimal() {
return bigDecimal;
}
public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}
public double getsDouble() {
return sDouble;
}
public void setsDouble(double sDouble) {
this.sDouble = sDouble;
}
public Double getbDouble() {
return bDouble;
}
public void setbDouble(Double bDouble) {
this.bDouble = bDouble;
}
public float getsFloat() {
return sFloat;
}
public void setsFloat(float sFloat) {
this.sFloat = sFloat;
}
public Float getbFloat() {
return bFloat;
}
public void setbFloat(Float bFloat) {
this.bFloat = bFloat;
}
public String getMoreDemo() {
return moreDemo;
}
public void setMoreDemo(String moreDemo) {
this.moreDemo = moreDemo;
}
@Override
public String toString() {
return "Data [id=" + id + ", bInt=" + bInt + ", bString=" + bString
+ ", sBool=" + sBool + ", bBool=" + bBool + ", sChar=" + sChar
+ ", bChar=" + bChar + ", bDate=" + bDate + ", bigDecimal="
+ bigDecimal + ", sDouble=" + sDouble + ", bDouble=" + bDouble
+ ", sFloat=" + sFloat + ", bFloat=" + bFloat + ", moreDemo="
+ moreDemo + "]";
}
}
简单测试类:com.simpleorm.test.TestDataMngDao
简单性能测试类:com.simpleorm.test.TestDemo