• spring 基础(2.5) 注解说明


    1注解与xml的相应说明

      @EnableConfigurationProperties: 配置@ConfigurationProperties使用,在@EnableConfigurationProperties写入class名称,然后class中写入@ConfigurationProperties

      @SpringBootApplication:springboot的核心注解,目的是开启自动配置
      @PropertySource:指定加载配置文件
      @Configuration: 等价于<Beans></Beans>。用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
      @Bean:等价于<Bean></Bean>
      @ComponentScan:指定范围 等价于<context:component-scan base-package=”com.item.demo”/>
      @ConfigurationProperties: 主要用来把properties配置文件转化为bean(类)来使用的。

      @Mapper:目的就是为了不再写mapper映射文件、为了把mapper这个DAO交給Spring管理 、为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类。

        @Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})")
        int add(Student student);

       @Results:当数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来。column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。

        @Select("select * from student where sno=#{sno}")
        @Results({
             @Result(property = "sno", column = "sno", javaType = String.class,id=true),
             @Result(property = "name", column = "sname", javaType = String.class),
             @Result(property = "sex", column = "ssex", javaType = String.class)
        })
        Student queryStudentBySno(String sno);
    目的就是为了不再写mapper映射文件

       1.1@Configuration  @Bean beans bean  

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">
      <bean id="student" class="domain.Student">
             <property name="id" value="11"/>
             <property name="age" value="22"/>
             <property name="name" value="jack"/>
         </bean>
         //扫描 com.item.demo包下所有的 带注解的文件
         <context:component-scan base-package=”com.item.demo”/>
    </beans>


    1.2需要导入的依赖

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>     


    2四类主键类型注解

      @Component:组件注解,通用注解,被该注解描述的类将被loC容器管理并实例化,以下三个都是主键注解的细化,当不确当用哪一个时候,就可以用这个。
      @Controller:语义注解,说明当前类是MVC应用中的控制器类。将当前修饰的类注入SpringBoot IOC容器,使得从该类所在的项目跑起来的过程中,这个类就被实例化.
      @Service:语义注解,说明当前类是Service业务服务类。
      @Repository:语义注解,说明当前类用于业务持久层通常描述对应Dao类。

    3model注解,

    下面是在model的注解,在使用的时候,必须加上@Valid

    @Null  被注释的元素必须为null
    @NotNull  被注释的元素不能为null
    @AssertTrue  被注释的元素必须为true
    @AssertFalse  被注释的元素必须为false
    @Min(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @Max(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @DecimalMin(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @DecimalMax(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @Size(max,min)  被注释的元素的大小必须在指定的范围内。
    @Digits(integer,fraction)  被注释的元素必须是一个数字,其值必须在可接受的范围内
    @Past  被注释的元素必须是一个过去的日期
    @Future  被注释的元素必须是一个将来的日期
    @Pattern(value) 被注释的元素必须符合指定的正则表达式。
    @Email 被注释的元素必须是电子邮件地址
    @Length 被注释的字符串的大小必须在指定的范围内
    @NotEmpty  被注释的字符串必须非空
    @Range  被注释的元素必须在合适的范围内

     

    4注解

      @ResponseBody:它的作用简短截说就是指该类中所有的API接口返回的数据,甭管你对应的方法返回Map或是其他Object,它会以Json字符串的形式返回给客户端。对于Map返回则是JSON String,对于String则仍然是String。

      @RestController:=@Controller + @ResponseBody。

      @RequestMapping:在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求。【@RequestMapping(value = "/add", method = RequestMethod.GET)/

    @RequestMapping(value = "/add"),指定请求方式之后只能额根据指定请求来,比如get。如果不写。则get和post都可以 @GetMapping("/add")@PostMapping("/add")】

      @Primary:常使用@Autowired, 默认是根据类型Type来自动注入的。但有些特殊情况,对同一个接口,可能会有几种不同的实现类,而默认只会采取其中一种的情况下 @Primary 的作用就出来了。只要被@Praimary标记,则spring ioc会自动去使用这个。

      @Autowired 注解,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。在使用@Autowired之前,我们对一个bean配置起属性时,如下

    <property name="属性名" value=" 属性值"/>    

         @CrossOrigin 启用CORS(默认情况下,@CrossOrigin允许在@RequestMapping注解中指定的所有源和HTTP方法),其中@CrossOrigin中的2个参数: origins: 允许可访问的域列表 maxAge:准备响应前的缓存持续的最大时间(以秒为单位)。@CrossOrigin(origins = "http://localhost:9999", maxAge = 3600) 参考

      @TableLogic 实体类中属性加上@TableLogic,调用BaseMapper的deleteById(id)或者调用IService的removeById(id); 就会去修改值,而不是删除。同时数据库中字段的值必须存在(比如,默认,0未删除、1删除,如果字段没有0则修改失败,必须有0才会修改为1)

     效果:
            没有@TableLogic注解调用deleteById/removeById,直接删除数据。
                SQL:delete from table where id = 1
            有注解走Update方法
                SQL:Update table set isDelete = 1 where id = 1

    @TableLogic注解参数
        value = "" 未删除的值,默认值为0
        delval = "" 删除后的值,默认值为1
        @TableLogic(value="原值",delval="改值")

     

    
    
  • 相关阅读:
    数据类型转换:高级向低级转换可能出现的问题和取得不同精度的方法
    Xen Server虚拟机数据恢复的方法和数据恢复过程
    VMware虚拟机误删除vmdk文件后如何恢复?
    MSSQL 2000 错误823恢复
    服务器存储误操作导致数据丢失的恢复过程
    我从业11年来遇到的最奇葩的raid0+1数据恢复经历
    Raid 5数据恢复原理以及raid 5数据恢复实际操作案例
    服务器数据恢复_Linux网站服务器故障数据恢复案例
    Raid5两块硬盘掉线可以恢复数据吗_raid数据恢复案例分享
    V7000存储数据恢复_底层结构原理拆解及Mdisk磁盘掉线数据恢复方法
  • 原文地址:https://www.cnblogs.com/1439107348s/p/14292893.html
Copyright © 2020-2023  润新知