java 反射和注解的简单应用
2016-06-13 09:31
645 查看
简单的主键注解:
package com.bj.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrimaryKey {
}
//下面是关联表的注解
package com.bj.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NonField {
}
//利用注解来拼接sql语句
public static String getUpdate(Object obj) throws IllegalArgumentException, IllegalAccessException{
Class<? extends Object> classzz = obj.getClass();
String sql = "update "+classzz.getSimpleName()+" set";
String sql2 = "";
String sql3 = " where ";
//获取字段
Field[] fields = classzz.getDeclaredFields();
for(Field f:fields){
f.setAccessible(true);//可以访问private字段的值
Object value = f.get(obj);
//如果该字段具备primaryKey注解
if(f.isAnnotationPresent(PrimaryKey.class)){
if(value instanceof String ){
sql3 +=f.getName()+"=' "+value+" ' ";
}else{
sql3 += f.getName()+" = "+value+" ";
}
}else if(!f.isAnnotationPresent(NonField.class)){//这里利用了NonField关联注解
if(value instanceof String){
sql2 += f.getName() +"='"+value+"',";
}else{
sql2+=f.getName()+"="+value+", ";
}
}
}
sql2 = sql2.substring(0,sql2.length()-1);
sql = sql+sql2+sql3;
return sql;
}
package com.bj.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrimaryKey {
}
//下面是关联表的注解
package com.bj.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NonField {
}
//利用注解来拼接sql语句
public static String getUpdate(Object obj) throws IllegalArgumentException, IllegalAccessException{
Class<? extends Object> classzz = obj.getClass();
String sql = "update "+classzz.getSimpleName()+" set";
String sql2 = "";
String sql3 = " where ";
//获取字段
Field[] fields = classzz.getDeclaredFields();
for(Field f:fields){
f.setAccessible(true);//可以访问private字段的值
Object value = f.get(obj);
//如果该字段具备primaryKey注解
if(f.isAnnotationPresent(PrimaryKey.class)){
if(value instanceof String ){
sql3 +=f.getName()+"=' "+value+" ' ";
}else{
sql3 += f.getName()+" = "+value+" ";
}
}else if(!f.isAnnotationPresent(NonField.class)){//这里利用了NonField关联注解
if(value instanceof String){
sql2 += f.getName() +"='"+value+"',";
}else{
sql2+=f.getName()+"="+value+", ";
}
}
}
sql2 = sql2.substring(0,sql2.length()-1);
sql = sql+sql2+sql3;
return sql;
}
相关文章推荐
- SpringMVC-前端控制器源码分析
- 数独小游戏
- eclipse创建maven多模块项目(单个类似)
- PAFA 框架报错java.io.FileNotFoundException: class path resource [log4j.properties] cannot be resolved to
- Java虚拟机(五):虚拟机类加载
- struts2和springmvc比较1
- SpringMVC-轻松上手
- Java虚拟机(四):Class文件结构及字节码指令
- Ti:关于java中Date转时间戳
- Spring MVC Hello World Example
- 【初探Spring】——Spring IOC(一)
- 内回文数-java
- leetcode 40. Combination Sum II-回溯算法
- Java中的观察者模式
- Java学习之二进制基础
- java 反射机制
- java任务调度的几种方式
- [置顶] Java并发编程与技术内幕:ThreadGroup线程组应用
- java 反射
- java按行读取文件并输出到控制台