Java???annotation?÷???????????????
???????????? ???????[ 2013/6/21 13:33:50 ] ????????
?????????????????????????????????????????RetentionPolicy.RUNTIME
package whut.annotationDB;
@DBTable(name="MEMBER")
public class Member {
//????????????У???????????value?????????value????????
//????????()?н??д??
@SQLString(30)
private String firstName;
@SQLString(50)
private String lastName;
@SQLInteger
private Integer age;
@SQLString(value=30??constraints=@Constraints(primaryKey=true))
private String handle;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
}
??????????apt?????????????
package whut.annotationDB;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TableCreator {
public Connection getConnection() {
String user = "root";
String password = "";
String serverUrl = "jdbc:mysql://localhost:3306/carrent?user=root&password=";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(serverUrl?? user??
password);
return con;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//????????
public static void main(String[] args) {
TableCreator tc = new TableCreator();
tc.executeCreateDB(Member.class);
}
public void executeCreateDB(Class<?> entity) {
String sqlStr = explainAnnotation(entity);
Connection con = getConnection();
PreparedStatement psql = null;
if (con != null && !sqlStr.equals("error")) {
try {
psql = con.prepareStatement(sqlStr);
psql.execute();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (psql != null)
psql.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null)
psql.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} else
System.out.println("failure to...");
}
// ?????????????Class<?>?????????????
public String explainAnnotation(Class<?> entity) {
// ??????????????
DBTable dbtable = entity.getAnnotation(DBTable.class);
if (dbtable == null) {
System.out.println("No DBTable annotation in class"
+ entity.getName());
return "error";
} else {
String tableName = dbtable.name();// ??????name???????????
// ?????????name??????????????????????
if (tableName.length() < 1)
tableName = entity.getName().toUpperCase();// ?????д
// ?????????????
List<String> columnsDefs = new ArrayList<String>();
// ???????????????
for (Field field : entity.getDeclaredFields()) {
String columnName = null;
// ???????????е????
Annotation[] anns = field.getDeclaredAnnotations();
// Annotation[] anns=field.getAnnotations();
// ???????????
if (anns.length >= 1) {
// ?ж?????????
if (anns[0] instanceof SQLInteger) {
SQLInteger sInt = (SQLInteger) anns[0];
// ?????name???????δ?д?????
if (sInt.name().length() < 1)
columnName = field.getName().toUpperCase();
else
columnName = sInt.name();
columnsDefs.add(columnName + " INT"
+ getConstraints(sInt.constraints()));
}
if (anns[0] instanceof SQLString) {
SQLString sString = (SQLString) anns[0];
// ?????name???????δ?д?????
if (sString.name().length() < 1)
columnName = field.getName().toUpperCase();
else
columnName = sString.name();
columnsDefs.add(columnName + " VARCHAR("
+ sString.value() + ")"
+ getConstraints(sString.constraints()));
}
}
}
StringBuilder createDB = new StringBuilder("CREATE TABLE "
+ tableName + "(");
for (String cols : columnsDefs)
createDB.append(" " + cols + "??");
// ????????????
String tableSQL = createDB.substring(0?? createDB.length() - 1)
+ ");";
// ?????????????
System.out.println("Table Creation SQL is:
" + tableSQL);
return tableSQL;
}
}
// ????????????
public String getConstraints(Constraints con) {
String constras = "";
if (!con.allowNull())
constras += " NOT NULL";
if (con.primaryKey())
constras += " PRIMARY KEY";
if (con.unique())
constras += " UNIQUE";
return constras;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11