发表于: 2017-11-13 17:46:14

1 1030



今天完成的任务

看了一些基础


学了一个注解的小程序


package test;

import hibernate_annotation.*;
import pojo.Hero;

import java.lang.reflect.Method;

public class ParseHibernateAnnotation {

public static void main(String[] args) {

Class<Hero> clazz = Hero.class;
       // 1. 确定是实体类
       // 2. 对应表
       // 3. 自增长id是,id策略是,对应字段是
       // 4. 其他属性对应字段是
       MyEntity myEntity = (MyEntity) clazz.getAnnotation(MyEntity.class);//但为毛你加个这个就有胆子管自己叫实体类
       if (null == myEntity) {
System.out.println("Hero类不是实体类");
       } else {
System.out.println("Hero类是实体类"+myEntity);
           MyTable myTable= (MyTable) clazz.getAnnotation(MyTable.class);
           String tableName = myTable.name();
           System.out.println("其对应的表名是:" + tableName);//实际上就是pojo.Hero@MyTable(name="hero_")注入的名字

           Method[] methods =clazz.getMethods();
           Method primaryKeyMethod = null;
           for (Method m: methods) {
MyId myId = m.getAnnotation(MyId.class);
               if(null!=myId){
primaryKeyMethod = m;
                   break;
               }
}

if(null!=primaryKeyMethod){
System.out.println("找到主键:" + method2attribute( primaryKeyMethod.getName() ));//所谓找到主键就是找到@MyId
               MyGeneratedValue myGeneratedValue =
primaryKeyMethod.getAnnotation(MyGeneratedValue.class);
               System.out.println("其自增长策略是:" +myGeneratedValue.strategy());//获得strategy标签
               MyColumn myColumn = primaryKeyMethod.getAnnotation(MyColumn.class);
               System.out.println("对应数据库中的字段是:" +myColumn.value());
           }
System.out.println("其他非主键属性分别对应的数据库字段如下:");
           for (Method m: methods) {
if(m==primaryKeyMethod){
continue;
               }
MyColumn myColumn = m.getAnnotation(MyColumn.class);
               //那些setter方法上是没有MyColumn注解的
               if(null==myColumn)
continue;
               System.out.format("属性: %s\t对应的数据库字段是:%s%n",method2attribute(m.getName()),myColumn.value());

           }



}


}

private static String method2attribute(String methodName) {
String result = methodName;
       result = result.replaceFirst("get", "");
       result = result.replaceFirst("is", "");
       if(result.length()<=1){
return result.toLowerCase();
       }
else{
return result.substring(0,1).toLowerCase() + result.substring(1,result.length());
       }


}}

不算复杂,比较重要的地方都用标注写了一下



收获的知识是明白了注解是怎样运行的与如何使用自定义注解


--------------------------------------------------------------------


多线程

多线程一般是两种写法

1.继承Thread类

2.实现Runnable接口


一般情况下选择第二种方法,因为实现接口较为简单




多线程一般是以重写run方法,再使用start方法进行,单纯的调用run方法并不能直接实现多线程

实现接口

public class Battle implements Runnable{}


继承类

public class KillThread extends Thread {}



--------------------------------------------------

   //匿名类
   Thread t1= new Thread(){
public void run(){
//匿名类中用到外部的局部变量teemo,必须把teemo声明为final
           //但是在JDK7以后,就不是必须加final的了
           while(!teemo.isDead()){
gareen.attackHero(teemo);
           }
}
};

   t1.start();

   Thread t2= new Thread(){
public void run(){
while(!leesin.isDead()){
bh.attackHero(leesin);
           }
}
};
   t2.start();

}

其实还有一种通过匿名类重写run方法调用,但感觉和第一种继承thread实际仿佛,就不再提了


看了一下任务五的要求和做法,感觉师兄在cookie session这一部分的日报对我启发很大,明天开始正是着手




遇到的问题



收获



返回列表 返回列表
评论

    分享到