package com.core.annotation;import static java.lang.annotation.elementtype.type;import static java.lang.annotation.retentionpolicy.runtime;import java.lang.annotation.retention;import java.lang.annotation.target;/** * @author zy * @version 创建时间:jul 2, 2010 2:39:20 pm */@target({type})@retention(runtime)public @interface table { public abstract string name() default ""; public abstract string catalog() default ""; public abstract string schema() default ""; }package com.core.annotation;import static java.lang.annotation.elementtype.field;import static java.lang.annotation.elementtype.method;import static java.lang.annotation.retentionpolicy.runtime;import java.lang.annotation.retention;import java.lang.annotation.target;/** * @author zy * @version 创建时间:jul 2, 2010 2:54:28 pm */@target({field,method})@retention(runtime)public @interface column { public abstract string name() default ""; public abstract boolean unique() default false; public abstract boolean nullable() default true; public abstract string type() default "string"; public abstract string definition() default ""; public abstract int length() default 32; }package com.entity;import java.io.serializable;import com.core.annotation.column;import com.core.annotation.table;/** * @author zy * @version 创建时间:jul 6, 2010 9:15:18 am */@table(name="t_user")public class user implements serializable{ private static final long serialversionuid = 6602217772155966261l; string id; string name; string sex; @column(name="user_name") public string getname() { return name; } public void setname(string name) { this.name = name; } @column(name="sex") public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public string getid() { return id; } public void setid(string id) { this.id = id; }}package com.core.util;import java.io.serializable;import java.lang.reflect.invocationtargetexception;import java.lang.reflect.method;import com.entity.user;import com.core.annotation.column;import com.core.annotation.table;/** * @author zy * @version 创建时间:jul 2, 2010 2:00:55 pm */public class annotationutil { @suppresswarnings("unchecked") public static string save(user u){ string temp = null; stringbuffer sb = new stringbuffer(); string id = string.valueof(u.getid()); if(id == null || id.trim().equals("")){ stringbuffer key = new stringbuffer(); stringbuffer value = new stringbuffer(); sb.append("insert into "); class c = u.getclass(); object [] obj = null; if(c != null && c.isannotationpresent(table.class)){ table table = (table) c.getannotation(table.class); try { temp = table.name(); if(temp.trim().equals("")){ sb.append(c.getname().trim()); }else{ sb.append(temp.trim()); } } catch (exception e) { sb.append(c.getname().trim()); } }else{ sb.append(c.getname().trim()); } method [] method = u.getclass().getdeclaredmethods(); for(method m : method){ if(m != null && m.isannotationpresent(column.class)){ column column = m.getannotation(column.class); temp = column.name(); if(temp.trim().equals("")){ key.append(m.getname().substring(3).trim()).append(","); }else{ key.append(temp.trim()).append(","); } try { temp = string.valueof(m.invoke(u, obj)); value.append("'").append(temp.trim()).append("',"); } catch (illegalargumentexception e) { e.printstacktrace(); value.append("'',"); } catch (illegalaccessexception e) { e.printstacktrace(); value.append("'',"); } catch (invocationtargetexception e) { e.printstacktrace(); value.append("'',"); } } } key.deletecharat(key.lastindexof(",")); value.deletecharat(value.lastindexof(",")); sb.append(" (").append(key).append(") values"); sb.append(" (").append(value).append(")"); }else{ sb.append("update "); class c = u.getclass(); object [] obj = null; if(c != null && c.isannotationpresent(table.class)){ table table = (table) c.getannotation(table.class); try { temp = table.name(); if(temp.trim().equals("")){ sb.append(c.getname().trim()); }else{ sb.append(temp.trim()); } } catch (exception e) { sb.append(c.getname().trim()); } }else{ sb.append(c.getname().trim()); } sb.append(" set "); method [] method = u.getclass().getdeclaredmethods(); for(method m : method){ if(m != null && m.isannotationpresent(column.class)){ column column = m.getannotation(column.class); temp = column.name(); if(temp.trim().equals("")){ sb.append(m.getname().substring(3).trim()).append("="); try { temp = string.valueof(m.invoke(u, obj)); sb.append("'").append(temp.trim()).append("',"); } catch (illegalargumentexception e) { e.printstacktrace(); sb.append("'',"); } catch (illegalaccessexception e) { e.printstacktrace(); sb.append("'',"); } catch (invocationtargetexception e) { e.printstacktrace(); sb.append("'',"); } }else{ sb.append(temp.trim()).append("="); try { temp = string.valueof(m.invoke(u, obj)); sb.append("'").append(temp.trim()).append("',"); } catch (illegalargumentexception e) { e.printstacktrace(); sb.append("'',"); } catch (illegalaccessexception e) { e.printstacktrace(); sb.append("'',"); } catch (invocationtargetexception e) { e.printstacktrace(); sb.append("'',"); } } } } sb.deletecharat(sb.lastindexof(",")); sb.append(" where id='").append(id.trim()).append("'"); } return sb.tostring(); } @suppresswarnings("unchecked") public static string delete(user u,serializable id){ string temp = null; stringbuffer sb = new stringbuffer("delete from "); class c = u.getclass(); if(c != null && c.isannotationpresent(table.class)){ table table = (table)c.getannotation(table.class); try { temp = table.name(); if(temp.trim().equals("")){ sb.append(c.getname().trim()); }else{ sb.append(temp.trim()); } } catch (exception e) { sb.append(c.getname().trim()); } }else{ sb.append(c.getname().trim()); } sb.append(" where id='").append(string.valueof(id)).append("'"); return sb.tostring(); } @suppresswarnings("unchecked") public static void main(string[] args) { user user = new user(); user.setid("234"); user.setname("历史"); user.setsex("男"); system.out.println(save(user)); system.out.println(delete(user,"123")); }}
分享到:
相关推荐
annotation.jar eclipse插件,根据数据库自动生成pojo
使用jsf1.2+spring3.0+hibernate3.3实现集成,利用annotation实现自动对象管理注入,用户表的登录增删改查操作,包括验证码ajax集成,消息机制,国际化处理,自定义转换器,自定义验证器等 qq:38732796 欢迎讨论
NULL 博文链接:https://wg4j.iteye.com/blog/644135
赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...
Spring Annotation
利用annotation(注解)开放简化java开放步骤
赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...
赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...
@androidx.annotation.NonNull 缺失的兼容、androidx.annotation兼容包
javax.annotation-3.0.jar javax.annotation-3.0.jar javax.annotation-3.0.jar
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...
Java annotation 什么是java annotation?annotation 的7种标注类型。nnotation提供了一条与程序元素关联任何信息或者任何元数据(metadata)的途径。从某些方面看,annotation就像修饰符一样被使用,并应用于包、...
NULL 博文链接:https://sam1982.iteye.com/blog/715461
Annotation核心技术Annotation核心技术Annotation核心技术Annotation核心技术Annotation核心技术Annotation核心技术
本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation...
annotation详解 讲解annotation的使用 教程
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
hibernate 注解 annotation 教程
hibernate annotation hibernate3