java

(2)java -jar 盘:文件名XXXX.jar

  使得可以双击执行 .jar程序(可选)。 要双击打开.jar文件的话,需要把 javaw (java 应用程序管理器)可执行程序文件路径改变,输入以下代码: C:Program FilesJavaj2rex.y.zinjavaw.exe" -jar "%1" %*

以前写Spring MVC的时候,要添加一个新页面访问总是要新增一个Controller或者在已有的一个Controller中新增一个方法,然后再跳转到设置的页面上去。考虑到大部分应用场景中View和后台都会有数据交互,这样的处理也无可厚非,不过我们肯定也有只是想通过一个URL Mapping然后不经过Controller处理直接跳转到页面上的需求!今天在做Spring Security相关配置的时候偶然发现了Spring也为我们提供了一个办法!那就是 WebMvcConfigurerAdapter !废话不多说了,直接看代码:

  1. package com.cloume.agvs.configuration;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;  
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
  6.   
  7. @Configuration  
  8. public class MVCConfiguration extends WebMvcConfigurerAdapter {  
  9.       
  10.     @Override  
  11.     public void addViewControllers(ViewControllerRegistry registry){  
  12.         registry.addViewController("/login").setViewName("login");  
  13.     }  
  14. }  

那么通过上面的配置,不用添加LoginController或者处理“login”的方法就可以直接通过“http://localhost:8080/projectContext/login”访问到login.html页面了!

从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件(那个xml写的是真的蛋疼。。。)
//UserDAO
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import entity.User;

/**
 * 添加了@Mapper注解之后这个接口在编译时会生成相应的实现类
 * 
 * 需要注意的是:这个接口中不可以定义同名的方法,因为会生成相同的id
 * 也就是说这个接口是不支持重载的
 */
@Mapper
public interface UserDAO {

    @Select("select * from user where name = #{name}")
    public User find(String name);

    @Select("select * from user where name = #{name} and pwd = #{pwd}")
    /**
      * 对于多个参数来说,每个参数之前都要加上@Param注解,
      * 要不然会找不到对应的参数进而报错
      */
    public User login(@Param("name")String name, @Param("pwd")String pwd);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

测试类代码

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDAO;
import entity.User;

public class TestCase {

    @Test
    public void testMapper() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserDAO dao = ac.getBean(UserDAO.class);
        User u1 = dao.find("hehe");
        User u2 = dao.login("hehe", "123");
        System.out.println(u1.getName().equals(u2.getName()));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
测试结果:

这里写图片描述

java中元注解有四个: @Retention @Target @Document @Inherited;

   @Retention:注解的保留位置         

      @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
      @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
      @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
  
  @Target:注解的作用目标

        

        @Target(ElementType.TYPE)   //接口、类、枚举、注解
        @Target(ElementType.FIELD) //字段、枚举的常量
        @Target(ElementType.METHOD) //方法
        @Target(ElementType.PARAMETER) //方法参数
        @Target(ElementType.CONSTRUCTOR)  //构造函数
        @Target(ElementType.LOCAL_VARIABLE)//局部变量
        @Target(ElementType.ANNOTATION_TYPE)//注解
        @Target(ElementType.PACKAGE) ///包   
 
     @Document:说明该注解将被包含在javadoc中
 
   @Inherited:说明子类可以继承父类中的该注解
 
举例:
        @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface AnnatDemo{
        public int value();
    }
以上代码定义了@AnnatDemo注解,作用目标是 用于对方法注解,并且保留在运行时的环境中,我们可以利用反射 获得一个方法上的注解  调用定义的方法,
 
比如@AnnatDemo 作用于以下方法:
public interface IClientProtocolEx extends IProtocol {
  int METHOD_START=0;
  @AnnatDemo(METHOD_START)
   public String say(String person);
}
 
那么可以利用以下代码进行反射:
        Class ipt=IClientProtocalEx.class;
   Method[] mts=ipt.getMethod();
         for(Method mt:mts)
   {
    AnnatDemo ad=mt.getAnnotation(AnnatDemo.class);//如果方法上  没有该注解  则返回null
           int value=ad.value();
       System.out.println("value:"+value);
   }
原文地址:https://www.cnblogs.com/zheli/p/8379736.html