注解例子代码比较简单就没有加注释,打印出的SQL是有问题的因为此例子意图并不是练习java操作数据库
package com.zjb.test.annont;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
package com.zjb.test.annont;
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 Column {
String value();
}
package com.zjb.test.annont;
@Table("table")
public class Student {
private Integer id;
@Column("Name")
private String name;
@Column("Password")
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.zjb.test.annont;
@Table("dept")
public class Department {
private Integer id;
@Column("Name")
private String name;
@Column("Code")
private String code;
@Column("UserId")
private String userId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
package com.zjb.test.annont;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class DatabaseUtils {
public static Boolean insert(Object object) throws Exception {
StringBuffer sqlBuffer = new StringBuffer();
StringBuffer middleBuffer = new StringBuffer();
StringBuffer endBuffer = new StringBuffer();
HashMap<String, String> sqlMap = new HashMap<>();
Class<? extends Object> c = object.getClass();
// 判断是否含有table注解
boolean isExists = c.isAnnotationPresent(Table.class);
if (!isExists) {
return null;
}
Table table = c.getAnnotation(Table.class);
String tableName = table.value();
sqlBuffer.append("insert into " + tableName);
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
String fieldName = field.getAnnotation(Column.class).value();
Method method = c.getDeclaredMethod("get" + fieldName);
Object value = method.invoke(object);
sqlMap.put(field.getName(), value.toString());
}
}
Set<Entry<String, String>> entySet = sqlMap.entrySet();
middleBuffer.append(" ( ");
endBuffer.append(" ( ");
for (Entry<String, String> entry : entySet) {
middleBuffer.append(entry.getKey() + " , ");
endBuffer.append(entry.getValue() + " , ");
}
middleBuffer.append(" ) ");
endBuffer.append(" ) ");
sqlBuffer.append(middleBuffer.toString()).append(" values ").append(endBuffer.toString());
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@start@@@@@@@@@@@@@@@@@@@@");
System.out.println(sqlBuffer.toString());
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@end@@@@@@@@@@@@@@@@@@@@");
return true;
}
}
package com.zjb.test.annont;
import org.junit.Test;
public class AnnontionTest {
@Test
public void test1() throws Exception {
Student student = new Student();
student.setName("小红");
student.setPassword("123456");
Department dept = new Department();
dept.setName("销售部");
dept.setCode("001");
dept.setUserId("0001");
DatabaseUtils.insert(student);
DatabaseUtils.insert(dept);
}
}