自定义注解,并通过注解进行数据库建表

    技术2022-07-11  116

    1、自定义注解 1.1、这个注解用来指定表名

    // An highlighted block package test.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Table { String value(); }

    1.2 这个注解用来指明 数据库字段名、类型、长度

    // An highlighted block package test.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 Field { String colName(); String type(); int length(); }

    2、新建实体类,并使用自定义的注解

    // An highlighted block package test.annotation; @Table("DB_STUDENT") public class Student { @Field(colName="id",type="int",length=10) private int id; @Field(colName="NAME",type="varchar",length=50) private String name; @Field(colName="age",type="int",length=10) private String age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }

    3、获取注解,然后把获取的注解内容拼接成SQL 语句

    // An highlighted block package test.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class demo02 { private static String SPACE =" "; private static String COMMA =","; public static void main(String[] args) { try { //获取类的注解 Class clazz = Class.forName("test.annotation.Student"); Annotation[] annotations = clazz.getAnnotations(); for(Annotation a : annotations){ System.out.println(a); } //获取指定注解 Table t = (Table) clazz.getAnnotation(Table.class); System.out.println(t.value()); //获取属性注解 Field fid = clazz.getDeclaredField("id"); Field fname = clazz.getDeclaredField("name"); Field fage = clazz.getDeclaredField("age"); test.annotation.Field f1 = fid.getAnnotation(test.annotation.Field.class); test.annotation.Field f2 = fname.getAnnotation(test.annotation.Field.class); test.annotation.Field f3 = fage.getAnnotation(test.annotation.Field.class); System.out.println(f1.colName()+"--"+f1.type()+"--"+f1.length()); System.out.println(f2.colName()+"--"+f2.type()+"--"+f2.length()); System.out.println(f3.colName()+"--"+f3.type()+"--"+f3.length()); String hql = " CREATE TABLE "+t.value()+"" + "("+f1.colName()+SPACE+f1.type()+"("+f1.length()+")" + COMMA+f2.colName()+SPACE+f2.type()+"("+f2.length()+")" + COMMA+f3.colName()+SPACE+f3.type()+"("+f3.length()+"))"; System.out.println(hql); DBExecute.execute(hql); } catch (Exception e) { e.printStackTrace(); } } } @test.annotation.Table(value=DB_STUDENT) DB_STUDENT ID--int--10 NAME--varchar--50 AGE--int--10 CREATE TABLE DB_STUDENT(ID int(10),NAME varchar(50),AGE int(10))

    4、建立数据库的连接,将连接数据库的参数写到配置文件里,通过读取配置文件来获取驱动、URL、 用户名和密码

    // An highlighted block package test.annotation; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class DBUtility { static String PATH_PROPERTY = "src/jdbc.properties"; private static Properties properties = new Properties(); private static String driver = null; private static String url = null; private static String user = null; private static String pwd = null; static{ try { InputStream in = new BufferedInputStream (new FileInputStream(PATH_PROPERTY)); properties.load(in); driver = properties.getProperty("driver"); url = properties.getProperty("url"); user = properties.getProperty("user"); pwd = properties.getProperty("password"); Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } } public static Connection openConnection() throws SQLException{ Connection con = null; con=DriverManager.getConnection(url, user, pwd); return con; } public static void closeconnection(Connection con){ if(con!= null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); System.out.println("关闭异常"); } } } }

    5、执行SQL语句创建表, 在demo02里调用下面这个方法即可

    // An highlighted block package test.annotation; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBExecute { public static void execute(String sql){ Connection con = null; Statement stmt = null; ResultSet re = null; try { con=DBUtility.openConnection(); stmt = con.createStatement(); stmt.execute(sql); } catch (SQLException e) { System.out.println("数据库访问异常"); throw new RuntimeException(e); } finally{ try { if(re!=null){ re.close(); } if(stmt!=null){ stmt.close(); } } catch (SQLException e2) { System.out.println("关闭异常"); } DBUtility.closeconnection(con); } } }

    6、登录数据库验证,建表成功,字段与注解内容一致

    Processed: 0.012, SQL: 9