在常見的orm框架中,大都提供了使用注解方式來實現entity與數據庫的映射,這里簡單地使用自定義注解與反射來生成可執行的sql語句。
這是整體的目錄結構,本來是為復習注解建立的項目^.^
好的,首先我們來確定思路。
1. 自定義@table @column注解,
我們稍微模仿hibernate,讓@table作用于類上,來表明實體類與數據表的映射關系,且讓@table中的屬性value映射為數據表的名稱tablename;讓@column作用于屬性上(這里沒實現作用于set方法上),表明屬性與數據表字段的映射關系,且讓@column中的屬性value映射為數據表的某個字段columnname。
2. 我們需要模仿一個dao層,
這個時候我們先定義一個實體user,具有name和password屬性,然后定義一個userdao,userdao中存在一個save(user user)的方法,當我們實例化一個userdao和user,我們調用userdao的save方法,傳入我們的user對象,返回一段sql,這段sql我們將它簡單設定為“insert into user (name,password) values('cai','123456')”,其中user為我們的數據表名稱,name,password為數據表字段,'cai','123456'為user對象的屬性。
--------------------------------0.0代碼分割線----------------------------------------------
//column.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.shu.annotation; import java.lang.annotation.documented; import java.lang.annotation.elementtype; import java.lang.annotation.inherited; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; @target ({elementtype.field}) @retention (retentionpolicy.runtime) @inherited @documented public @interface column { string value(); } |
//table.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.shu.annotation; import java.lang.annotation.documented; import java.lang.annotation.elementtype; import java.lang.annotation.inherited; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; @target ({elementtype.method,elementtype.type}) @retention (retentionpolicy.runtime) @inherited @documented public @interface table { string value(); } |
//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
|
package com.shu.entity; import com.shu.annotation.column; import com.shu.annotation.table; @table ( "t_user" ) public class user { @column ( "name" ) private string name; @column ( "password" ) private string password; public string getname() { return name; } public string getpassword() { return password; } public void setname(string name) { this .name = name; } public void setpassword(string password) { this .password = password; } } |
//ibasedao.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
|
package com.shu.dao; import java.lang.reflect.field; import com.shu.annotation.column; import com.shu.annotation.table; public interface ibasedao<t> { //最終目標:insert into user (name,password) values('cai','123456'); default string save(t entity) throws illegalargumentexception, illegalaccessexception{ //sql用于存放最終返回的sql語句 stringbuilder sql = new stringbuilder( "insert into " ); //tablename用于存放sql語句中表名部分 stringbuilder tablename; //columnname用于存放sql語句的字段部分 stringbuilder columnname = new stringbuilder( "(" ); //values用于存放sql語句中的賦值部分 stringbuilder values = new stringbuilder( "(" ); //獲取對象user的class對象 class clazz = entity.getclass(); //判斷該user類是否有@table注解 boolean istable = clazz.isannotationpresent(table. class ); if (istable) { //獲取user類@table注解的值value,該值我們定義為user表的表名稱 table t = (table) clazz.getannotation(table. class ); tablename = new stringbuilder(t.value()); //拼接表名 sql.append(tablename+ " " ); //獲取user對象的屬性列表 field[] fieldlist = clazz.getdeclaredfields(); //遍歷屬性列表,分別拿出屬性列表中被@column注解的屬性,并獲取屬性的值 for ( int i= 0 ;i<fieldlist.length;i++){ field f = fieldlist[i]; boolean iscolumn = f.isannotationpresent(column. class ); if (!iscolumn){ continue ; } column column = f.getannotation(column. class ); f.setaccessible( true ); object columnvalue = f.get(entity); if (i==fieldlist.length- 1 ){ columnname.append(column.value()+ ") values " ); values.append( "'" +columnvalue+ "')" ); sql.append(columnname); sql.append(values); continue ; } columnname.append(column.value()+ ", " ); values.append( "'" +columnvalue+ "'," ); } // boolean iscolumn = clazz.isannotationpresent(annotationclass); } return sql.tostring(); } } |
//userdao.java
1
2
3
4
5
6
7
|
package com.shu.dao; import com.shu.entity.user; public class userdao implements ibasedao<user> { } |
//userservice.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
|
package com.shu.service; import com.shu.dao.userdao; import com.shu.entity.user; public class userservice { public static void main(string[] args) { // todo auto-generated constructor stub userdao userdao = new userdao(); user user = new user(); user.setname( "cai" ); user.setpassword( "123456" ); try { system.out.println(userdao.save(user)); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalaccessexception e) { // todo auto-generated catch block e.printstacktrace(); } } } |
--------------------------------0.0代碼分割線----------------------------------------------
在這里我們最后定義了一個service層,讓它去調用dao層然后生成對應的sql語句,你可以自定義其他的entity,比如我目錄中的order,然后定義一個orderdao實現ibasedao接口,定義一個orderservice調用orderdao的save方法,同樣可以生成order表對應的sql語句。
這里總結一下不足:
1.hibernate的注解可以不用賦值,所以我認為要實現無需賦值直接獲取表的名稱或者字段的名稱,可以利用反射的方法來獲取。
2.這里只做了save方法,而且只是動態生成了sql語句,并沒有和數據庫交互,可以包裝一個factory,提供持久化方法來實現對sql語句的執行。
3.這里對sql的拼接比較粗糙,各位可以想想有沒有其他更為高效的方法。
以上這篇java利用自定義注解、反射實現簡單basedao實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/Little-tree/p/7449538.html