这里主要记录的是 SpringBoot引入Quartz任务调度器和Mybatis,其实呢基本和Spring中引入差不多,他们的区别在于,jar包的引入方式不同了,还有就是我们引入jar包之后,Spring需要在XML里配置Quartz和Mybatis,一系列的比较麻烦,而SpringBoot呢,他采用的是注解的形式来引入插件的,可以说省掉很多配置,体验很不错!
1.首先呢就是SpringBoot 对Quartz和Mybatis,MySql的jar 依赖的引入,至部分就不再列出,在“SpringBoot添加依赖”里面有介绍
2.任务调度的方式也不细说,下面代码
package com.demo.classes;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//注解方式引入
@Component
@EnableScheduling
public class SimpleTask {
@Autowired
private MyTask mytask;//显示调度次数方法
@Autowired
private SimpleDateFormat simpledateformat;//需要调度的方法
//任务调度两种时间定义方式
@Scheduled(fixedDelay=1000*3)
public void reportCurrentTime(){
mytask.tosay();
System.out.println ("每隔3秒任务调度一次 现在时间 " + simpledateformat.format(new Date ()));
}
@Scheduled(cron="*/5 * * * * ?")
public void reportCurrentByCron(){
System.out.println ("\"每隔5秒任务调度一次 Scheduling Tasks Examples By Cron:\r\nThe time is now " + simpledateformat.format(new Date ()));
}
}
//运行结果
大家好,我是springboot任务调度=>0
每隔3秒任务调度一次 现在时间 2018-10-20 12:26:18
2018-10-20 12:26:18.357 INFO 8196 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-10-20 12:26:18.360 INFO 8196 --- [ restartedMain] com.demo.Application : Started Application in 2.391 seconds (JVM running for 2.741)
"每隔5秒任务调度一次 Scheduling Tasks Examples By Cron:
The time is now 2018-10-20 12:26:20
大家好,我是springboot任务调度=>1
每隔3秒任务调度一次 现在时间 2018-10-20 12:26:21
大家好,我是springboot任务调度=>2
每隔3秒任务调度一次 现在时间 2018-10-20 12:26:24
"每隔5秒任务调度一次 Scheduling Tasks Examples By Cron:
The time is now 2018-10-20 12:26:25
大家好,我是springboot任务调度=>3
每隔3秒任务调度一次 现在时间 2018-10-20 12:26:27
3.Mybatis和Mysql的使用需要先在 application.properties配置文件里 配置一下
##数据连接
spring.datasource.url=jdbc:mysql://localhost:3306/re_mamage
spring.datasource.username=root
spring.datasource.password=admin@123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##mybatis配置
##实体类所在的包,起别名
mybatis.typeAliasesPackage=org.spring.springboot.domain
## 映射文件路径
mybatis.mapperLocation=classpath:mapper/*.xml
在这之后就需要我们定义
dao接口
接口的XML文件
Service
Controller
实体类
这些都和Spring整合Mybatis是一样的
4.我们SpringBoot是默认没有Jsp的,所以Jsp也需要我们去手动引入,添加依赖( jsp和JSTL) 也是在上一篇博客
引入之前先介绍一下 SpringBoot 有关静态资源的一些 规定
SpringBoot默认资源路径是在 src/main/resources下
META-INF
META-INF/resources
public
resources
static
这几个文件夹下的资源是默认静态资源,不能修改
依赖引入之后,有时候我们后端需要返回视图,所以我们还需要添加 视图解析配置 在application配置里面进行配置
##视图解析 前缀/后缀
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
之后我们就可以愉快的进行开发了!这里面我只是介绍了如何整合几个框架,但是具体里面每个框架的使用并没有细说,具体实现我可能会另外的去介绍,大家也可以去官网上去学习!很高兴分享!