转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80275680
Java自定义注解的理论部分参见博文《Java之——注解详解和自定义注解(基于JDK实现简单的注解功能)》,我们直接进入主题
package io.mykit.annotation.jdk.db.provider;
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;
/**
* 自定义Table注解
* @author liuyazhuang
*
*/
@Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Table {
String value() default "";
}
package io.mykit.annotation.jdk.db.provider;
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;
/**
* 自定义Column注解
* @author liuyazhuang
*
*/
@Inherited
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
String value() default "";
}
package io.mykit.annotation.jdk.db.provider.entity;
import io.mykit.annotation.jdk.db.provider.Column;
import io.mykit.annotation.jdk.db.provider.Table;
/**
* 自定义使用注解的实体
* @author liuyazhuang
*
*/
@Table("t_user")
public class User {
@Column("id")
private String id;
@Column("name")
private String name;
public User() {
super();
}
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
package io.mykit.annotation.jdk.db.provider.parser;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import io.mykit.annotation.jdk.db.provider.Column;
import io.mykit.annotation.jdk.db.provider.Table;
/**
* 解析自定义注解
* @author liuyazhuang
*
*/
public class AnnotationParser {
/**
* 通过注解来组装查询条件,生成查询语句
* @param obj
* @return
*/
public static String assembleSqlFromObj(Object obj) {
Table table = obj.getClass().getAnnotation(Table.class);
StringBuffer sbSql = new StringBuffer();
String tableName = table.value();
sbSql.append("select * from " + tableName + " where 1=1 ");
Field[] fileds = obj.getClass().getDeclaredFields();
for (Field f : fileds) {
String fieldName = f.getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
Column column = f.getAnnotation(Column.class);
if (column != null) {
Method method = obj.getClass().getMethod(methodName);
Object v = method.invoke(obj);
if (v != null) {
if (v instanceof String) {
String value = v.toString().trim();
// 判断参数是不是 in 类型参数 1,2,3
if (value.contains(",")) {
//去掉value中的,
String sqlParams = value.replace(",", "").trim();
//value中都是纯数字
if(isNum(sqlParams)){
sbSql.append(" and " + column.value() + " in (" + value + ") ");
}else{
String[] split = value.split(",");
//将value重置为空
value = "";
for(int i = 0; i < split.length - 1; i++){
value += "'"+split[i]+"',";
}
value += "'"+split[split.length - 1]+"'";
sbSql.append(" and " + column.value() + " in (" + value + ") ");
}
} else {
if(value != null && value.length() > 0){
sbSql.append(" and " + column.value() + " like '%" + value + "%' ");
}
}
} else {
sbSql.append(" and " + column.value() + "=" + v.toString() + " ");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return sbSql.toString();
}
/**
* 检查给定的值是不是 id 类型 1.检查字段名称 2.检查字段值
*
* @param target
* @return
*/
public static boolean isNum(String target) {
boolean isNum = false;
if (target.toLowerCase().contains("id")) {
isNum = true;
}
if (target.matches("\\d+")) {
isNum = true;
}
return isNum;
}
}
package io.mykit.annotation.jdk.provider;
import org.junit.Test;
import io.mykit.annotation.jdk.db.provider.entity.User;
import io.mykit.annotation.jdk.db.provider.parser.AnnotationParser;
import io.mykit.annotation.jdk.provider.parser.AnnotationProcessor;
/**
* 测试自定义注解
* @author liuyazhuang
*
*/
public class AnnotationTest {
@Test
public void testDBAnnotation(){
User testDto = new User("123", "34");
User testDto1 = new User("123", "test1");
User testDto2 = new User("", "test1,test2,test3,test4");
String sql = AnnotationParser.assembleSqlFromObj(testDto);
String sql1 = AnnotationParser.assembleSqlFromObj(testDto1);
String sql2 = AnnotationParser.assembleSqlFromObj(testDto2);
System.out.println(sql);
System.out.println(sql1);
System.out.println(sql2);
}
}
select * from t_user where 1=1 and id like '%123%' and name like '%34%'
select * from t_user where 1=1 and id like '%123%' and name like '%test1%'
select * from t_user where 1=1 and name in ('test1','test2','test3','test4')
select * from t_user where 1=1 and id like '%123%' and name like '%34%'
select * from t_user where 1=1 and id like '%123%' and name like '%test1%'
select * from t_user where 1=1 and id like '%456%' and name in (1,2,3,4)