在学习Spring Boot 框架的时候,我发现里面充斥这大量的注解,因此有必要进行记录及用处分析。
Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。
Annotation注解是一个辅助类,大致的作用有四种:
- 编译检查
- 在反射中使用AnnotationB
- 根据Annotation生成帮助文档
- 能够帮忙查看查看代码
对于Spping Boot框架,需要理解的常用的注解有:
controller层:
1、@Controller
@Controller 用来响应页面,表示当前的类为控制器。
2、@RestController
@RestController 是@ResponseBody和@Controller的结合
表明当前类是控制器且返回的是一组数据,不是页面
3、@Autowired
这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这个注解,类中或接口的方法就可以直接调用了。
4、@RequestMapping
当前台界面调用Controller处理数据时候告诉控制器怎么操作
作用:URL映射。
5、@GetMapping
@RequestMapping(method = RequestMethod.GET)的简写
作用:对应查询,表明是一个查询URL映射
6、@PostMapping
@RequestMapping(method = RequestMethod.POST)的简写
作用:对应增加,表明是一个增加URL映射
7、@PutMapping
@RequestMapping(method = RequestMethod.PUT)的简写
作用:对应更新,表明是一个更新URL映射
8、@DeleteMapping
@RequestMapping(method = RequestMethod.DELETE)的简写
service层:
1.@service
用于标注业务层组件
dao层:
1.@Repository
是用来注解接口,@Repository注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象),并支持自动处理数据库操作产生的异常
配置:
1.@Configuration,@Bean
可理解为用spring的时候xml里面的<beans>标签,用@Configuration注解该类,等价 与XML中配置beans
@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名
例如:
@Configuration
public class ExampleConfiguration {
@Value("com.mysql.jdbc.Driver")
private String driverClassName;
@Value("jdbc://xxxx.xx.xxx/xx")
private String driverUrl;
@Value("${root}")
private String driverUsername;
@Value("123456")
private String driverPassword;
@Bean(name = "dataSource")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(driverUrl);
dataSource.setUsername(driverUsername);
dataSource.setPassword(driverPassword);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
这样,在项目中
@Autowired
private DataSource dataSource;
的时候,这个dataSource就是我们在ExampleConfiguration中配的DataSource。
启动类:
@SpringBootApplication:启动注解,@SpringBootApplication是一个复合注解,包括@ComponentScan, 和@SpringBootConfiguration,@EnableAutoConfiguration
@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。
@EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。在下面博客会具体分析这个注解,快速入门的demo实际没有用到该注解。
@ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。
@ServletComponentScan:在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
@MapperScan("com.Vm.server") :@MapperScan注解只会扫描包中的接口,不会扫描类,扫描指定包中的接口
@EnableScheduling: spring自带的定时服务
public class ScheduledTasks {
@Scheduled(fixedRate = 1000 * 30) //每30秒执行一次
public void reportCurrentTime(){
System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ()));
}
}
Mybits:
//Mapper中的namespace用于绑定Dao接口的,即面向接口编程。
//namespace:一般是dao接口所在的路径+接口名称
<mapper namespace="com.VmService.dao.IBlackDao">
//resultMap属性:type为java实体类;id为此resultMap的标识。
//id和result标签是最简单的映射,id为主键映射;result其他基本数据库表字段到实体类属性的映射。
<resultMap id="BlackBean" type="com.VmService.model.BlackBean">
<id column="ID" property="id"/>
<id column="IM" property="im"/>
</resultMap>
//进行SQL语句查找
<select id="selectAllBlackList" resultMap="BlackBean">
SELECT
id as id,
im as im,
FROM
t_blacklist t1
ORDER BY
id DESC
</select>
Spring Boot 注解功能引用自:https://blog.csdn.net/endlesstwo000/article/details/89511771