Springboot中常用的注解

3 篇文章 0 订阅

Spring注解

  1. @Configuration用于定义配置类,可替换xml配置文件

  2. @ComponentScan告诉Spring 哪个packages 的用注解标识的类 会被spring自动扫描并且装入bean容器。例如,如果你有个类用@Controller注解标识了,那么,如果不加上@ComponentScan,自动扫描该controller,那么该Controller就不会被spring扫描到,更不会装入spring容器中,因此你配置的这个Controller也没有意义。

  3. @Repository 代表数据访问层(DAO)的时候

  4. @Service 当一个组件代表业务层时

  5. @Controller 一个组件作为前端交互的控制层

  6. @Autowired 注解可用于为类的属性、构造器、方法进行注值

SpringBoot注解

  1. @SpringBootApplication 注解
很多Spring Boot 开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的 main 类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时), Spring Boot 提供一个方便的 @SpringBootApplication 选择。

该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。
  1. @RestController继承自Controller,标注为一个Controller,同时返回数据为json,开发 REST 服务,用在类上面。responsebody用在方法上面。

  2. @requestMapping 该注解可以填写,请求的方法,请求路径。SpringBoot中,还有Get mapping,postmapping等

  3. @EnableAutoConfiguration 注解

  4. @ResponseBody
    表示该方法的返回结果直接写入HTTP response body 中

一般在异步获取数据时使用,在使用@RequestMapping 后,返回值通常解析为跳转路径,加上

@responsebody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。比如

异步获取json 数据,加上 @responsebody 后,会直接返回 json 数据

swagger

1.ApiOperation

swagger内的注解,(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”)

  1. API

其余注解

1.Transactional

//对于一组数据库操作特别是增删改操作,为了保证原子性,通过需要用事务来控制,要么全部成功,要么全部失败。Spring中可以通过注解@Transaction

@Transactional
public void testTransaction(User user) {
    int rowNum = userMapper.insertUser(user);
    List<User> userList = userMapper.selectAllUsers();
}

//将两个操作insert和select当作原子操作,如果在testTransaction方法中有异常,则回滚。

2.Param

dao层定义接口时,如果传入多个参数,就需要用到@Param注

采用#{}的方式把@Param注解括号内的参数进行引用
#{} 能够防止sql注入

dao层示例

Public User selectUser(@param(“userName”) String name,@param(“userpassword”) String password);

xml映射对应示例


<select id=" selectUser" resultMap="BaseResultMap">  
   select  *  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}  
</select>

@Param注解JavaBean对象

dao层示例

public List<user> getUserInformation(@Param("user") User user);

xml映射对应示例


<select id="getUserInformation" parameterType="com.github.demo.vo.User" resultMap="userMapper">  
        select   
        <include refid="User_Base_Column_List" />  
        from mo_user t where 1=1  
                      <!-- 因为传进来的是对象所以这样写是取不到值得 -->  
            <if test="user.userName!=null  and user.userName!=''">   and   t.user_name = #{user.userName}  </if>  
            <if test="user.userAge!=null  and user.userAge!=''">   and   t.user_age = #{user.userAge}  </if>  
    </select>  
  1. @PropertySource
    注解加载指定的属性文件

  2. @EnableAspectJAutoProxy开启AOP

  3. @RequestParamRequestParam从request中接收请求的,两个都可以接收参数
    用在方法的参数前面:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

根据上面的这个URL,可以这样来获取参数。

public String getDetails(
    @RequestParam(value="param1", required=true) String param1,
        @RequestParam(value="param2", required=false) String param2){
...
}

@PathVariable,URL中的路径变量会成为方法参数。

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

上面的一个url你可以这样写:

@RequestMapping("/hello/{id}")
    public String getDetails(@PathVariable(value="id") String id,
    @RequestParam(value="param1", required=true) String param1,
    @RequestParam(value="param2", required=false) String param2){
.......
}
  1. @RequestBody
    一般是post请求的时候才会使用这个请求,把参数丢在requestbody里面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值