java 注解和反射创建数据库表

版权声明:未经允许,不得转载 https://blog.csdn.net/qq_29881259/article/details/83792723

类注解

package com.market.annotation;
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 ClassAnnotation {
	String value();
}
 

字段注解

package com.market.annotation;
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 FieldAnnotation {
	String colunm();

	String type();

	String length() default "0";
	
	boolean iskey() default false; 
	
}

实体类示例

package com.market.entity;

import com.market.annotation.ClassAnnotation;
import com.market.annotation.FieldAnnotation;

@ClassAnnotation(value="UserBean")
public class UserBean {
	@FieldAnnotation(colunm="u_id",type="int", length="10",iskey=true)
	private int u_id;
	@FieldAnnotation(colunm="u_name",type="varchar", length="50")
	private String u_name;
	@FieldAnnotation(colunm="u_password",type="varchar", length="50")
	private String u_password;
	@FieldAnnotation(colunm="u_gender",type="varchar", length="50")
	private String u_gender;
	@FieldAnnotation(colunm="u_age",type="int", length="10")
	private int u_age;
	@FieldAnnotation(colunm="u_phone",type="varchar", length="50")
	private String u_phone;
	@FieldAnnotation(colunm="u_address",type="varchar", length="50")
	private String u_address;
	@FieldAnnotation(colunm="u_auth",type="varchar", length="50")
	private String u_auth;//
    
	public UserBean() {
		super();
	}

	public UserBean(int uId, String uName, String uPassword, String uGender,
			int uAge, String uPhone, String uAddress, String uAuth) {
		super();
		u_id = uId;
		u_name = uName;
		u_password = uPassword;
		u_gender = uGender;
		u_age = uAge;
		u_phone = uPhone;
		u_address = uAddress;
		u_auth = uAuth;
	}

	public int getU_id() {
		return u_id;
	}

	public void setU_id(int uId) {
		u_id = uId;
	}

	public String getU_name() {
		return u_name;
	}

	public void setU_name(String uName) {
		u_name = uName;
	}

	public String getU_password() {
		return u_password;
	}

	public void setU_password(String uPassword) {
		u_password = uPassword;
	}

	public String getU_gender() {
		return u_gender;
	}

	public void setU_gender(String uGender) {
		u_gender = uGender;
	}

	public int getU_age() {
		return u_age;
	}

	public void setU_age(int uAge) {
		u_age = uAge;
	}

	public String getU_phone() {
		return u_phone;
	}

	public void setU_phone(String uPhone) {
		u_phone = uPhone;
	}

	public String getU_address() {
		return u_address;
	}

	public void setU_address(String uAddress) {
		u_address = uAddress;
	}

	public String getU_auth() {
		return u_auth;
	}

	public void setU_auth(String uAuth) {
		u_auth = uAuth;
	}

}

利用注解创建数据库表 

package com.market.util;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.market.annotation.ClassAnnotation;
import com.market.annotation.FieldAnnotation;

public class CreateTable {
	public static void CreateTableByClassName(Class<?> classname,
			String databasename) {

		// TODO Auto-generated method stub
		Connection conn = ConnectionManager.getConnection();
		System.out.println(conn);
		String sql = "select table_name from" + " information_schema.TABLES"
				+ " where table_name=? and table_schema=?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);

			ClassAnnotation classan = classname
					.getAnnotation(ClassAnnotation.class);

			ps.setString(1, classan.value());
			ps.setString(2, databasename);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				System.out.println("表已存在");
				rs.previous();
				while (rs.next()) {
					System.out.println(rs.getString(1));
				}
			} else {
				System.out.println("表不存在");
				// 创建表
				Field[] fields = classname.getDeclaredFields();

				StringBuffer createsql = new StringBuffer("create table "
						+ classan.value() + "(");
				for (int i = 0; i < fields.length; i++) {
					FieldAnnotation field = fields[i]
							.getAnnotation(FieldAnnotation.class);
					if (field.iskey()) {
						createsql.append(field.colunm() + " " + field.type()
								+ "(" + field.length() + ")"
								+ " primary key auto_increment,");

					} else {
						createsql.append(field.colunm()
								+ " "
								+ field.type()
								+ ("0".equals(field.length()) ? "," : "("
										+ field.length() + "),"));
					}
				}
				createsql = createsql.deleteCharAt(createsql.length() - 1);
				createsql.append(")");

				System.out.println(createsql);
				ps = conn.prepareStatement(createsql.toString());
				ps.execute();
				System.out.println("创建表:" + classan.value() + " 成功");

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

阅读更多
换一批

没有更多推荐了,返回首页