注解

引用> springboot注解大全

一.几个在第一阶段用到的注解

1. 在Application类中用到的注解

1)@SpringBootApplication
申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。
2)@MapperScan和@Mapper注解
@Mapper:为了让项目中的Mapper能够让别的类进行引用,我们可以在Mapper类上添加@Mapper注解:

@Mapper
public interface LuckyMoneyMapper {}

直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。

@MapperScan:通过使用@MapperScan可以指定要扫描的Mapper类的包的路径

@SpringBootApplication(exclude = {WebAutoConfiguration.class})
//@EnableScheduling
//@EnableCaching//开启缓存
@MapperScan(basePackages="com.tony.luckymoney.mybatistest.mapper")
@ComponentScan(basePackages = {"com.tony.luckmoney.config.*"
        ,"com.tony.luckmoney.business.*"
        ,"com.tony.luckmoney.system.*"
        ,"com.tony.luckymoney.mybatistest.*"})
public class Application {

扫描mapper包下的所有mapper接口


扫描图示

3)@ComponentScan:Spring是一个依赖注入(dependency injection)框架。所有的内容都是关于bean的定义及其依赖关系。定义Spring Beans的第一步是使用正确的注解-@Component或@Service或@Repository.但是,Spring不知道你定义了某个bean除非它知道从哪里可以找到这个bean.
ComponentScan做的事情就是告诉Spring从哪里找到bean
总结:
如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
如果你有一些bean所在的包,不在main app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包

2. 在controllor类中用到的注解

1)@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集。

@RestController
public class LuckyMoneyController {
    @Autowired
    private LuckyMoneyService luckyMoneyService;

    /**
     * 增添红包信息
     * @param luckymoney luckymoney
     * @return luckymoney
     */
    @PostMapping("/createluckymoney")
    public int insertLuckyMoney(Luckymoney luckymoney) {
        return luckyMoneyService.insertLuckyMoneyInfo(luckymoney);
    }
}

2)@Autowired:自动导入
3)@GetMapping @PostMapping @PutMapping @RequestMapping(略)
4)@PathVariable : 获取参数
5)@RequestParam : 用在方法的参数前面,提供路由信息,负责URL到Controller中的具体函数的映射。

 @PutMapping("/luckymoneys/{id}")
    public Luckymoney update(@PathVariable("id") Integer id,
                             @RequestParam("consumer") String consumer) {

        return luckyMoneyService.update(id,consumer);
    }

3. 在service类中用到的注解

1)@Service @Component
2)@Transactional :事物
3)@Slf4j : 可以使用log.info()记日志形式输出

4. 在mapper类中用到的注解

1)@Mapper

5. 在Entity类中用到的注解

lombok中的·
@Data 相当于get set方法
@NoArgsConstructor 无参构造函数
@AllArgsConstructor 全参构造函数
@Builder :允许您使用以下代码自动生成使您的类可实例化所需的代码

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Luckymoney {}

@Builder

Luckymoney luckymoney = Luckymoney.builder().producer(producer).money(money).build();

相当于

Luckymoney luckymoney = new Luckymoney(); 
luckymoney.setProducer(producer);
luckymoney.setMoney(money);

6. 在test类中用到的注解

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
@Autowired
@Test

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class LuckyMoneyServiceImplTest {
    @Autowired
    private LuckyMoneyServiceImpl luckyMoneyServiceImpl;
    @Autowired
    private LuckyMoneyMapper luckyMoneyMapper;

    /**
     * 查所有红包
     */
    @Test
    public void findAllLuckyMoneyInfo() {
        List result = luckyMoneyServiceImpl.findAllLuckyMoneyInfo();
        log.info("总共有{}个行记录",result.size());
        Assert.assertTrue(result.size() > 0);
    }

你可能感兴趣的:(注解)