Java SimpleORM 使用介紹

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


轉自:Java SimpleORM 使用介紹


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章