Java注解【五、注解实战】
需求:
1、表:用户ID,用户名,年龄,邮箱。
2、实现方法,传入实体,打印sql。
实现:
1、表:
package Annotation;
@Table("user")
public class User {
@Column("id")
public int UserId;
@Column("user_name")
public String Username;
@Column("age")
public int Age;
@Column("e_mail")
public String Email;
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
}
2、注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
public String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
public String value();
}
3、调用
public class QueryUser {
public static void main(String[] args) {
User u = new User();
u.setAge(18);
u.setEmail("123@123.com,234@132.com");
u.setUsername("amy");
String sql = getQuerySql(u);
System.out.println(sql);
}
private static String getQuerySql(Object o) {
StringBuilder sb = new StringBuilder();
// 1、获取类
Class<? extends Object> c = o.getClass();
// 2、获取表名
String tableName = "";
if (c.isAnnotationPresent(Table.class)) {
Table t = (Table) c.getAnnotation(Table.class);
tableName = t.value();
}
if (tableName == "") {
return "no table name";
}
sb.append("select * from ").append(tableName).append(" where 1=1 ");
// 3、获取字段名、字段值、拼装where条件
Field[] fs = c.getFields();
for (Field field : fs) {
// 3.1获取字段名称
String fieldName = field.getName();
// 3.2用字段名称获取get方法
String getMethodName = "get" + fieldName;
Method getMethod = null;
try {
getMethod = c.getMethod(getMethodName);
} catch (Exception e) {
System.out.println(e.toString());
}
if (getMethod == null) {
return "no method";
}
// 3.3用get方法获取字段值,当有值时才拼装
Object fieldValue = null;
try {
fieldValue = getMethod.invoke(o);
} catch (Exception e) {
System.out.println(e.toString());
}
//无值跳过
if(fieldValue==null ||
(fieldValue instanceof Integer && (Integer)fieldValue==0)){
continue;
}
//3.4 有值时获取表字段名
String columnName = "";
if(field.isAnnotationPresent(Column.class)){
Column co = field.getAnnotation(Column.class);
columnName=co.value();
}
if(columnName==""){
return "no column name";
}
// 3.5获取字段类型根据类型进行语句处理
Class<?> fieldType = field.getType();
// string类型,有,分割,需要in
if (fieldType.toString().toLowerCase().contains("string")
&& fieldValue.toString().contains(",")) {
String[] values = fieldValue.toString().split(",");
sb.append("and ").append(columnName).append(" in (");
for (String v : values) {
sb.append("'").append(v).append("' ,");
}
// 删掉最后一个逗号
sb.deleteCharAt(sb.length() - 1);
sb.append(") \n");
} else if (fieldType.toString().toLowerCase().contains("string")) {
sb.append("and ").append(columnName).append(" = '")
.append(fieldValue).append("' \n");
} else {
sb.append("and ").append(columnName).append(" = ")
.append(fieldValue.toString()).append("\n");
}
}
return sb.toString();
}
}