大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
java注意事项演示 地图产生表 演示样本 来自thinking in java 4 20代码的章
thinking in java 4免费下载:http://download.csdn.net/detail/liangrui1988/7580155
package org.rui.annotation.database; import java.lang.annotation.*; /** * 生成一个数据库表 * @author lenovo * */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface DBTable { public String name() default ""; }
package org.rui.annotation.database; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * sql类型 * @author lenovo * */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface SQLString { int value() default 0; String name() default ""; Constraints constraints() default @Constraints; }
package org.rui.annotation.database; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * sql类型 * @author lenovo * */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface SqlInteger { int value() default 0; String name() default ""; Constraints constraints() default @Constraints; }
package org.rui.annotation.database; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * javaBean域准备的注解 * @author lenovo * */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Constraints { boolean primaryKey() default false; boolean allowNull() default true; boolean unique() default false; }
package org.rui.annotation.database; /** * 假设要令嵌入的@Constraints注解中的unique()元素为true 并以此作为constraints()元素的默认值 * 则须要例如以下定义该元素 * @author lenovo * */ public @interface Uniqueness { Constraints constraints() default @Constraints(unique=true); }
package org.rui.annotation.database; @DBTable(name="MEMBER") public class Member { @SQLString(30) String firstName; @SQLString(50) String lastName; @SqlInteger Integer age; @SQLString(value=30,constraints=@Constraints(primaryKey=true)) String handle; static int memberCount; //get public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Integer getAge() { return age; } public String getHandle() { return handle; } @Override public String toString() { return handle; } }
package org.rui.annotation.database; import java.io.ObjectInputStream.GetField; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; /** * 相前资料请參考 thinking in java 第4版 20章 * @author lenovo * */ public class TableCreator { //解释注解 获得对应的值 public static String getConstraints(Constraints con) { String constraints=""; if(con.allowNull()) constraints+="NOT NULL"; if(con.primaryKey()) constraints+="PRIMARY KEY"; if(con.unique()) constraints+="UNIQUE"; return constraints; } public static void main(String[] args) throws ClassNotFoundException { String[] arr=new String[]{"org.rui.annotation.database.Member"}; if (arr.length<1) { System.out.println("0000"); System.exit(0); } for(String className:arr) { Class<?> clzz=Class.forName(className); DBTable table=clzz.getAnnotation(DBTable.class); if(table==null) { System.out.println("dbtable annotaions in class:"+className); continue; } String tableName=table.name(); if(tableName.length()<1) tableName=clzz.getName().toUpperCase(); //结果集合 List<String> columnDefs=new ArrayList<String>(); //获取全部字段 for(Field f:clzz.getDeclaredFields()) { String columnName =null; //获取字段上面的注解 Annotation[] annot=f.getAnnotations(); if(annot.length<1)continue; //假设是SqlInteger if(annot[0] instanceof SqlInteger) { SqlInteger sInt=(SqlInteger)annot[0]; //假设没有指定名称 使用字段名称 if(sInt.name().length()<1) { columnName=f.getName().toUpperCase(); }else { columnName=sInt.name(); } columnDefs.add(columnName+" INT"+getConstraints(sInt.constraints())); } //假设是string 内型 if(annot[0] instanceof SQLString) { SQLString sStr=(SQLString)annot[0]; if(sStr.name().length()<1) { columnName=f.getName().toUpperCase(); }else { columnName=sStr.name(); } columnDefs.add(columnName+" VARCHAR("+sStr.value()+")"+ getConstraints(sStr.constraints())); } StringBuilder sql=new StringBuilder("CREATE TABLE "+tableName+"( "); for(String c:columnDefs) { sql.append("\n "+c+",");//拼接字段 //remove trailing comma String tableCreate=sql.substring(0,sql.length()-1)+");"; System.out.println("table ceration sql fro "+className+" is :\n "+tableCreate); } } } } } /**output: table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL, LASTNAME VARCHAR(50)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL, LASTNAME VARCHAR(50)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL, LASTNAME VARCHAR(50)NOT NULL, AGE INTNOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL, LASTNAME VARCHAR(50)NOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL, LASTNAME VARCHAR(50)NOT NULL, AGE INTNOT NULL); table ceration sql fro org.rui.annotation.database.Member is : CREATE TABLE MEMBER( FIRSTNAME VARCHAR(30)NOT NULL, LASTNAME VARCHAR(50)NOT NULL, AGE INTNOT NULL, HANDLE VARCHAR(30)NOT NULLPRIMARY KEY); */
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117114.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...