准备环境
- 1.idea编译器
- 2.java项目
- 3.代码结构:
4.下载导入上面图中的三个jar包
这里强调下关于如何导入jar包
快捷键Ctrl+Shift+Alt+s.依次选择Modeles---> Dependencies--> +JARS or derectories--->选择存放jar包文件夹--->apply-->ok 即可
5,实现
接口类
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
* 定义注解
* 需要定义元注解
* @target描述注解的使用范围
*也就是说可以在那些地方使用
* 包(package)、类(type)、类型成员(fleld)、方法参数(local)
*
* @Retention表示在什么级别保存该注释信息,用于描述注解的生命周期
* 在源文件中有效、在class文件中有效、在运行时有效
*
* */
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)//可通过反射读到该注解
public @interface Table{
String name() default "";//参数名
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)//可通过反射读到该注解
public @interface Field {
String columnname();
String type();
int length();
}
c3p0工具类:
package annotation;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
public class C3p0 {
//c3p0连接池
private static ComboPooledDataSource compool=new ComboPooledDataSource();
static {
try {
//配置c3p0相关属性也可用xml文件
compool.setDriverClass("com.mysql.jdbc.Driver");
compool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/webchat?useUnicode=true&characterEncoding=utf-8");
compool.setUser("root");
compool.setPassword("root");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public static ComboPooledDataSource getComboPooled() {
return compool;
}
}
package annotation;
import org.apache.commons.dbutils.QueryRunner;
import java.sql.SQLException;
public class Insert {
public static void insert(String sql){
//dbutils工具类
QueryRunner qr=new QueryRunner(C3p0.getComboPooled());
try {
qr.update(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
实现类:
package annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/*
* 利用反射读取注解信息
*
* */
public class Demo {
public static void main(String[] args) {
try {
Class stu=Class.forName("annotation.Student");
//获得这个类所有注解
Annotation[] annotations=stu.getAnnotations();
for(Annotation i:annotations)
System.out.println(i);
//或者
Table t1=(Table)stu.getAnnotation(Table.class);
System.out.println(t1.name());
Field field=stu.getDeclaredField("name");
Field f1=stu.getDeclaredField("id");
Field f2=stu.getDeclaredField("age");
//拿到方法的注解
annotation.Field field1=field.getAnnotation(annotation.Field.class);
annotation.Field field2=f1.getAnnotation(annotation.Field.class);
annotation.Field field3=f2.getAnnotation(annotation.Field.class);
System.out.println(field1.columnname()+" "+field1.type()+" "+field1.length());
//写出插入数据库的sql语句
StringBuilder str=new StringBuilder();
str.append("CREATE TABLE "+t1.name()+"(");
str.append(field2.columnname()+" "+field2.type()+"("+field2.length()+")"+"PRIMARY KEY,");
str.append(field1.columnname()+" "+field1.type()+"("+field1.length()+")"+",");
str.append(field3.columnname()+" "+field3.type()+"("+field3.length()+")"+");");
System.out.println(str.toString());
//执行sql语句
Insert.insert(str.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
效果图: