java注解练习

注解例子代码比较简单就没有加注释,打印出的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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值