java hibernate 注解详解_java注解,通过反射解析注解,模仿hibernate,获取sql语句。...

常用注解:

b526756f452f142f53ba8b99eb6f835f.png

自定义注解,标准格式:

1,target:注解作用域

2,Retention:声明周期

1a8f0f74785f288b288f5029640fc44c.png

ac9775a30d7d5941b04658c5092aa2cb.png

255a58943b5e6dec1054b48939ea5e29.png

9ee5d6442b4e5b03ad7eecc38643d500.png

12e3a149e677941be17f8372ec7fa588.png

运行子类继承,但是子类继承只能作用到类注解,字段注解,是继承不了的。

34516696c663918dec4a8e75760e3ed0.png

78f56f3e11547227861f3e6ea536935a.png

使用注解:通过下面这种方式,为注解的成员赋值,使用的时候会通过成员名,找到这些值,去使用这些值。

08e794d1bad327102d4281745aed76cc.png

如果注解只有一共成员,该成员必须命名为value,这样使用注解的时候方便:如:@注解(“值1”),相当于@注解(value=“值1”);

c77f8a80cc7de958d5c67c05cecbf101.png

解析注解:

解析注解主要用到反射的方式,在下面的例子中可以看到。

主要代码如下:

//1,获取到class

Class c=f.getClass();//2,获取到table的名字

boolean exists=c.isAnnotationPresent(Table.class);//判断该类Filete是否是包含注解Table

if(!exists)

{return null;

}

Table t=(Table) c.getAnnotation(Table.class);//取出Filete这个类的Table注解

String tableName=t.value();//获取该注解的值

304840b7421fd5c03e9207e89a5548ce.png

使用注解模仿hibernate获取sql的方法。

1,定义两个注解,Table和column

packagecom.Annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;

@Target({ElementType.TYPE})//作用域为在类上

@Retention(RetentionPolicy.RUNTIME) //生命周期为运行时

public @interfaceTable {

String value();

}

packagecom.Annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;

@Target({ElementType.FIELD})//作用域为在字段上

@Retention(RetentionPolicy.RUNTIME) //生命周期为运行时

public @interfaceColumn {

String value();

}

2,定义一个数据库模型类:并分别在类名和字段名上加上注解,分别跟表名和字段名关联

packagecom.Annotation;

@Table("user")public classFilter {

@Column("id")private intid;

@Column("user_Name")privateString userName;

@Column("age")private intage;

@Column("city")privateString city;

@Column("email")privateString email;public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getUserName() {returnuserName;

}public voidsetUserName(String userName) {this.userName =userName;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}publicString getCity() {returncity;

}public voidsetCity(String city) {this.city =city;

}publicString getEmail() {returnemail;

}public voidsetemail(String email) {this.email =email;

}

}

3,通过set字段值,来定义查询条件,定义一共querey方法,同注解,反射获取sql。

packagecom.Annotation;importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;public classTest {public static voidmain(String[] args) {

Filter f1=newFilter();

f1.setAge(10);//查询id为10的用户

Filter f2=newFilter();

f2.setCity("大连");//查询城市为大连的用户

Filter f3=newFilter();

f3.setCity("liu@sina.com,zh@sina.com,8888@163.com");//查询邮箱为上面的用户

String sql1=query(f1);

String sql2=query(f2);

String sql3=query(f3);

System.out.println(sql1);

System.out.println(sql2);

System.out.println(sql3);

}private staticString query(Filter f)

{

StringBuffer sb=newStringBuffer();//1,获取到class

Class c=f.getClass();//2,获取到table的名字

boolean exists=c.isAnnotationPresent(Table.class);//判断该类Filete是否是包含注解Table

if(!exists)

{return null;

}

Table t=(Table) c.getAnnotation(Table.class);//取出Filete这个类的Table注解

String tableName=t.value();//获取该注解的值

sb.append(" select * from ").append(tableName).append("where 1=1");//3,遍历所有字段

Field[] fArray=c.getDeclaredFields();for(Field field:fArray)

{//4,处理每个字段对应的sql//4,1,拿到字段名

boolean fExis=field.isAnnotationPresent(Column.class);if(!exists)

{continue;

}

Column column=field.getAnnotation(Column.class);//获取该字段上的column注解

String columnName=column.value();//4,2,拿到字段的值

String filedName=field.getName();

String getMethoudName="get"+filedName.substring(0,1).toUpperCase()+filedName.substring(1);

Object fieldValue= null;//为了让各种类型的返回值都能接收,定义Object类型

try{

Method getMethod=c.getMethod(getMethoudName);

fieldValue=getMethod.invoke(f);//调用f的对应字段的get方法

} catch(Exception e) {

e.printStackTrace();

}//4,3,拼装sql

if(fieldValue==null||(fieldValueinstanceof Integer && (Integer)fieldValue==0))

{continue;

}

sb.append(" and ").append(filedName);if(fieldValue instanceof String) //如果字符串是Int类型,加上单引号

{if(((String) fieldValue).contains(",")) //包含,,表示是子查询

{

String []values=((String) fieldValue).split(",");

sb.append("in(");for(String v:values)

{

sb.append("'").append(v).append("'").append(",");

}

sb.deleteCharAt(sb.length()-1);//去掉最后一个逗号

sb.append(")");

}else //否则是普通查询

{

sb.append("=").append("'").append(fieldValue).append("'");

}

}else if(fieldValue instanceofInteger)

{

sb.append("=").append(fieldValue);

}

}returnsb.toString();

}

}

运行结果:

63c201464193571a442ecc3d2d4a1e51.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值