盒子
盒子
文章目录
  1. SpringBoot注解(siwang.hu  V1.0)
    1. 1.  @SpringBootApplication
    2. 2.  @value
    3. 3.  @Configuration
    4. 4.  @Bean
    5. 5.  @Autowired和@Resource
    6. 6.  @Qualifier
    7. 7.  @ResponseBody
    8. 8.  @RestController与@Controller
    9. 9.  @RequestMapping
    10. 10.  组件注解
    11. 11.  @Transactional
    12. 12.  @Entity与@Table(name=”tb_voice”)
    13. 13.  @Query
    14. 14.  @ControllerAdvice与@ExceptionHandler

springboot注解笔记

SpringBoot注解(siwang.hu  V1.0)

官方学习资料

1.  @SpringBootApplication

  • 注解在启动类上

  • 等效于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan

    1
    2
    3
    4
    5
    6
    7
    8
    @SpringBootApplication
    public class StartApplication {

    public static void main(String args[])throws Exception{
    SpringApplication.run(StartApplication.class,args);
    }

    }

2.  @value

  • 该注解可以读取springboot中的配置文件中的参数值
  • 方便读取自定义的配置参数

application.properties文件中的自定义配置如下:

1
2
port=8080
ip=127.0.0.1

使用方式:

1
2
3
4
5
@Value("${port}")
private int Port;

@Value(("${ip}"))
private String Ip;

3.  @Configuration

  • 注解在类上,相当于用spring的时候xml里面的beans标签

  • 代表这个类是一个配置类

4.  @Bean

  • 注解在方法上,产生bean的方法,并且交给Spring容器管理

  • 如果未通过@Bean指定bean的名称,则默认与标注的方法名相同

  • @Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域

    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
    27
    28
    29
    30
    31
    @Configuration
    public class RedisCache extends CachingConfigurerSupport {

    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String,Object> redisTemplate(){
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
    return redisTemplate;
    }

    private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
    }

    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForValue();
    }
    }

5.  @Autowired和@Resource

  • @Autowired与@Resource都可以用来装配bean

  • 可以写在字段上,或写在setter方法上

  • @Autowired是默认按照类型装配的 @Resource默认是按照名称装配

6.  @Qualifier

  • 当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定

  • @Qualifier限定描述符能进行更细粒度的控制如何选择候选者

    1
    2
    3
    @Autowired 
    @Qualifier(value = “userOpService”)
    private UserService userOpService;

7.  @ResponseBody

  • 注解在方法上,表明将结果直接写入HTTP response body,不做界面跳转

  • 比如获取json数据,加上@responsebody后,会直接返回json数据

8.  @RestController与@Controller

  • @Controller一般用于前端界面跳转

  • @RestController相当于@ResponseBody + @Controller合在一起的作用

  • @RestController用于开发restful api服务

9.  @RequestMapping

  • 对外Url链接地址
    1
    2
    3
    4
    @RequestMapping(value="/voice/add",method ={RequestMethod.GET,RequestMethod.POST})
    public boolean addVoice(Voice voice) {
    return voiceService.insertVoice(voice);
    }

10.  组件注解

  • @Component注解放在类的头上,是所有受Spring 管理组件的通用形式,不推荐使用

  • @Service对应的是业务层Bean

  • @Repository对应数据访问层Bean

11.  @Transactional

  • 事务注解,添加在方法或类上,声明为事务

  • 一般用于数据库操作函数中

    1
    2
    3
    4
    5
    6
    7
    @Transactional
    public boolean checkVoiceById(int id,Stringcommit){
    Voice voice=voiceDao.findById(id);
    voice.setCommit(commit);
    voiceDao.save(voice);
    return true;
    }

12.  @Entity与@Table(name=”tb_voice”)

  • @Entity声明持久化类

  • @Table声明表名

  • @Id声明表主键

  • @GeneratedValue(strategy= GenerationType.IDENTITY)声明表主键自动增长

  • @Column(nullable = false,name = “id”)声明表字段的属性和名称

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    @Entity
    @Table(name="tb_voice")
    public class Voice implements Serializable{
    private static final long serialVersionUID = 2300044412175011558L;

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(nullable = false,name = "id")
    private int id;

    @Column(nullable = false , name = "name")
    private String name;

    @Column(nullable = false , name = "label")
    private String label;

    @Column(nullable = false , name = "datetime")
    private Date datetime;

    @Column(nullable = false , name = "uri")
    private String uri;

    @Column(nullable = false , name = "commit")
    private String commit;

    @Column(nullable = false , name = "size")
    private String size;

    public void setId(int id) {
    this.id = id;
    }

    public void setName(String name) {
    this.name = name;
    }

    public void setLabel(String label) {
    this.label = label;
    }

    public void setDatetime(Date datetime) {
    this.datetime = datetime;
    }

    public void setUri(String uri) {
    this.uri = uri;
    }

    public void setCommit(String commit) {
    this.commit = commit;
    }

    public void setSize(String size) {
    this.size = size;
    }

    public int getId() {
    return id;
    }

    public String getName() {
    return name;
    }

    public String getLabel() {
    return label;
    }

    public Date getDatetime() {
    return datetime;
    }

    public String getUri() {
    return uri;
    }

    public String getCommit() {
    return commit;
    }

    public String getSize() {
    return size;
    }

    @Override
    public String toString() {
    return "Voice{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", label='" + label + '\'' +
    ", datetime=" + datetime +
    ", uri='" + uri + '\'' +
    ", commit='" + commit + '\'' +
    ", size='" + size + '\'' +
    '}';
    }
    }

13.  @Query

  • 用于springboot JPA中的自定义sql查询语句
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public interface BookDao extends JpaRepository<Book, Integer>{

    //Hql语句查询
    @Query("select b from Book b where b.bookName like %?1%")
    public List<Book> findByBookName(String bookName);

    //本地sql语句查询
    @Query(value="select * from t_book order by RAND() limit ?1",nativeQuery=true)
    public List<Book> randomList(Integer n);
    }

14.  @ControllerAdvice与@ExceptionHandler

  • 全局异常处理

  • 用于拦截服务器的异常

    1
    2
    3
    4
    5
    6
    7
    8
    @ControllerAdvice
    public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public String handleException(){
    return "Exception!";
    }
    }
支持一下
扫一扫,支持胡思旺
  • 微信扫一扫
  • 支付宝扫一扫