1、@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中使用的标签,用来扫描包配置的平行支持)。
2、@ServletComponentScan:在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
3、@MapperScan("com.Vm.server") :@MapperScan注解只会扫描包中的接口,不会扫描类,扫描指定包中的接口
4、@EnableScheduling、@Scheduled:定时任务。@EnableScheduling这个注解需要加在启动类上,表示支持定时任务。
实例:
@EnableSchedulingpublicclassScheduledTasks{@Scheduled(fixedRate =1000*30)//每30秒执行一次@Scheduled(cron ="0 */1 * * * * ")//在固定时间执行// 详细参考:https://www.cnblogs.com/domi22/p/9418433.htmlpublicvoidreportCurrentTime(){
System.out.println ("Scheduling Tasks Examples: The time is now "+ dateFormat ().format (newDate()));}}@EnableConfigurationProperties:将带有@ConfigurationProperties注解的类注入为Spring容器的Bean。如果使用了@ConfigurationProperties但是没有在启动类上增加这个注解,则@ConfigurationProperties将不起作用。
5、全局异常处理
@ControllerAdvice:统一处理异常。
@ExceptionHandler(Exception.class):用在方法上面表示遇到这个异常就执行以下方法。
6、@Async与@EnableAsync:其中@Async表示这个方法为异步方法;@EnableAsync这个注解需要加在启动类上,表示支持异步操作;如果不加,则@Async将不起作用。
实例:
@RestControllerpublicclassAsyncController{@Autowiredprivate AsyncService asyncservice;@RequestMapping("/asyncTest.do")public String asyncTest(){
System.out.println("############asyncTest############");
System.out.println("############a############");
asyncservice.test();
System.out.println("############b############");return"success";}@RequestMapping("/asyncTest2.do")public String asyncTest2(){
System.out.println("############asyncTest2############");
System.out.println("############a############");
asyncservice.test2();
System.out.println("############b############");return"success";}@RequestMapping("/asyncTest3.do")public String asyncTest3(){
System.out.println("############asyncTest3############");
System.out.println("############a############");
asyncservice.test3();
System.out.println("############b############");return"success";}}@ServicepublicclassAsyncService{publicvoidtest(){
System.out.println("############c############");for(int i =0; i <5; i++){
System.out.println(i);}
System.out.println("############d############");}@Asyncpublicvoidtest2(){
System.out.println("############c############");for(int i =0; i <5; i++){
System.out.println(i);}
System.out.println("############d############");}/**
* @Async就相当于另起一个线程
*/publicvoidtest3(){newThread(){@Overridepublicvoidrun(){
System.out.println("############c############");for(int i =0; i <5; i++){
System.out.println(i);}
System.out.println("############d############");}}.start();}}
实验结果是:
asyncTest.do输出为: acdb;
asyncTest2.do输出为: abcd;
asyncTest3.do 输出为: abcd;
所以简单来说:@Async就相当于另起一个线程。
四、Mybatis
//Mapper中的namespace用于绑定Dao接口的,即面向接口编程。
//namespace:一般是dao接口所在的路径+接口名称
<mappernamespace="com.VmService.dao.IBlackDao">
//resultMap属性:type为java实体类;id为此resultMap的标识。
//id和result标签是最简单的映射,id为主键映射;result其他基本数据库表字段到实体类属性的映射。
<resultMapid="BlackBean"type="com.VmService.model.BlackBean"><idcolumn="ID"property="id"/><idcolumn="IM"property="im"/></resultMap>
//进行SQL语句查找
<selectid="selectAllBlackList"resultMap="BlackBean">
SELECT
id as id,
im as im,
FROM
t_blacklist t1
ORDER BY
id DESC
</select>