java 实体建表 注释_JAVA 注解(Annotation) ,模拟自动创建表

hibernate 里面通过注解,映射等手段,可以自动生成表,现在模拟实现。随便学学注解如何使用。首先,我们要定义几个注解:Table 用于定义表名字,类型使用Typeimport java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionP...
摘要由CSDN通过智能技术生成

hibernate 里面通过注解,映射等手段,可以自动生成表,现在模拟实现。随便学学注解如何使用。

首先,我们要定义几个注解:

Table 用于定义表名字,类型使用Type

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 Table {

// 表名

String name() default "";

}

Types,定义一下常用的类型,这里我写得比较乱,也可以使用 枚举  等其他方法,方便使用

import java.util.HashMap;

import java.util.Map;

public class Types {

// 自己定义的一些基本类型,和数据对应就行了,就是组成字符串

// 这是临时的办法,需要大家从新设计

public static final String VARCHAR = "VARCHAR";

public static final String INT = "INT";

public static final String BOOLEAN = "BOOLEAN";

// 默认长度,和数据库对应

public static final int STRING_LENGTH =32;

public static final int INT_LENGTH = 10;

// 将类型 已经默认长度,放入集合

public static Map map = new HashMap();

static{

map.put(VARCHAR, STRING_LENGTH);

map.put(INT, INT_LENGTH);

map.put(BOOLEAN, 0);

}

public static String getType(String type,int length){

if(type == null){

throw new RuntimeException("Not recognized the type :"+type);

}

// 防止boolean 这类型

if( length > 0){

return type+"("+length+")";

}

return type;

}

public static String getString(){

return getStirng(VARCHAR, STRING_LENGTH);

}

public static String ge

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java注解是在编译时或运行时处理的特殊标记,可以用来给代码添加元数据信息。如果需要在运行时修改字段的注解值,可以使用Java反射机制来实现。 以下是修改字段的注解值的示例代码: ```java // 定义一个注解 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @interface MyAnnotation { String value(); } // 定义一个类,包含一个被注解的字段 class MyClass { @MyAnnotation("oldValue") private String myField; } // 在运行时修改字段的注解值 public class Main { public static void main(String[] args) throws Exception { MyClass obj = new MyClass(); Field field = obj.getClass().getDeclaredField("myField"); MyAnnotation annotation = field.getAnnotation(MyAnnotation.class); String oldValue = annotation.value(); System.out.println("oldValue = " + oldValue); // 修改注解值 MyAnnotation newAnnotation = new MyAnnotation() { @Override public String value() { return "newValue"; } @Override public Class<? extends Annotation> annotationType() { return MyAnnotation.class; } }; field.setAnnotation(newAnnotation); // 再次获取注解值 MyAnnotation updatedAnnotation = field.getAnnotation(MyAnnotation.class); String newValue = updatedAnnotation.value(); System.out.println("newValue = " + newValue); } } ``` 在上面的示例代码中,首先定义了一个注解`MyAnnotation`,然后在`MyClass`类中给`myField`字段添加了这个注解。接着在`Main`类中使用Java反射机制获取`myField`字段的注解值,并将其修改为新值。最后再次获取注解值,可以看到已经被修改为新值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值