1. Automatically generate a standard spring boot project schema using the tools provided by spring Boot (http://start.spring.io/)
2. Because we are building the Spring Boot+mybatis+quartz architecture here, we configure the dependent dependencies in the Pom.xml file
<dependencies> <dependency> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot</artifactId> <version>1.5.8.RELEASE</version> </dependency> <dependency> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter</artifactId> <version>1.5.8.RELEASE</version> </dependency> <dependency> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter-web</artifactId> <version>1.5.8.RELEASE</version> </dependency> <dependency> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter-test</artifactId> <version>1.5.8.RELEASE</version> <scope>Test</scope> </dependency> <dependency> <groupId>Org.mybatis.spring.boot</groupId> <artifactId>Mybatis-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> <!--Https://mvnrepository.com/artifact/mysql/mysql-connector-java -- <dependency> <groupId>Mysql</groupId> <artifactId>Mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>Com.alibaba</groupId> <artifactId>Druid</artifactId> <version>1.0.11</version> </dependency> <dependency> <groupId>Org.quartz-scheduler</groupId> <artifactId>Quartz</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>Org.slf4j</groupId> <artifactId>Slf4j-api</artifactId> <version>1.7.21</version> </dependency> </dependencies>
3. The concept of spring boot is to do 0 configuration, so there is no web. XML, only one APPLICATION.YML configuration, this time configuration data source and MyBatis Mapper scan path
spring: datasource: url: jdbc:mysql://192.168.253.128/scheduler?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true username: sche password: sche # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select ‘x‘ testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 jobFactory: type: com.gary.operation.jobdemo.demo1.MyJobFactory mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.hq.dispach.entity
4. After these basic configurations are complete, the project is structured as follows
Here the static directory is the front-end code that holds html,javascript and so on
5. The Spring boot Web does not need to be deployed under Tomcat, because it comes with Tomcat, only the main method of executing the Master program application. But we need to do some important configuration.
package Com.hq.dispach; import Org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration @MapperScan ( "Com.hq.dispach.dao" ) public class dispachapplication {public static void Span class= "Fu" >main (string[] args) {springapplication. (dispachapplication., args); }}
Here the configuration @springbootapplication is the default, note the second and third configuration, if the lack of @mapperscan ("Com.hq.dispach.dao") configuration will occur when DAO cannot be injected into the service situation, If the @enableautoconfiguration is missing then the Quartz Task Scheduler will not be able to inject service, it is recommended that no matter what application is best to add this configuration.
5. The next step is to write the Control,service,dao,mapper,scheduletask code.
Control:
Package Com.hq.dispach.control;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import Org.springframework.web.bind.annotation.RestController;import Com.hq.dispach.entity.Sche;import Com.hq.dispach.service.ScheService;@RestController@RequestMapping("/schecontrol") Public classSchecontrol {@Autowired PrivateScheservice Scheservice;@GetMapping("/addsche/{name}/{password}") PublicScheFindschebynameandpassword(@PathVariable("Name") String name,@PathVariable("Password") String password) {returnScheservice.Findschebynameandpassword(name, password); }}
Sevice:
package com.hq.dispach.service;import com.hq.dispach.entity.Sche;publicinterface ScheService { publicfindScheByNameAndPassword(String name,String password); publictest();}
Service implementation
Package Com.hq.dispach.service.impl;import org.springframework.beans.factory.annotation.Autowired;import Org.springframework.stereotype.Service;import Com.hq.dispach.dao.ScheMapper;import Com.hq.dispach.entity.Sche;import Com.hq.dispach.service.ScheService;@Service("Scheservice") Public classScheserviceimplImplementsscheservice{@Autowired PrivateSchemapper Schemapper; PublicScheFindschebynameandpassword(string name, string password) {returnSchemapper.Findschebynameandpassword(name, password); } PublicStringTest() {return "Schedules Run"; }}
Dao:
package com.hq.dispach.dao;import org.apache.ibatis.annotations.Param;import com.hq.dispach.entity.Sche;publicinterface ScheMapper { publicfindScheByNameAndPassword(@Param("name"@Param("password") String password);}
Mapper:
<?xml version= "1.0" encoding = "UTF-8" ?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace= > <select id= "Findschebynameandpassword" resulttype= "Com.hq.dispach.entity.Sche" > SELECT T. ' Age ', T. ' Department ', T. ' Gendar ', T. ' id ', T. ' Name ', T. ' Password ' from Sche t WHERE T. ' Name ' =# {Name} and T. ' Password ' =#{password} </SELECT> </MAPPER>
ScheduleTask:
Package Com.hq.dispach.schedule;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import Com.hq.dispach.service.ScheService;@Component@Configurable@EnableScheduling Public classscheduledtasks{@Autowired PrivateScheservice Scheservice;//Every 1 minutes @Scheduled(cron ="0 */1 * * * *") Public void Reportcurrentbycron() {System. out.println(Scheservice.Test()); }}
Entity:
Package com.hq.dispach.entity; Public classSche {PrivateString ID;PrivateString name;PrivateString password;PrivateString age;PrivateString Department;PrivateString Gendar; PublicStringgetId() {returnId } Public void setId(String ID) { This.ID= ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This.name= name; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This.Password= password; } PublicStringGetage() {returnAge } Public void Setage(String Age) { This. Age= age; } PublicStringgetdepartment() {returnDepartment } Public void setdepartment(String Department) { This.Department= Department; } PublicStringGetgendar() {returnGendar; } Public void Setgendar(String Gendar) { This.Gendar= Gendar; }}
6. Set up the main method to start application, the URL of the page access control can get the data in the database, while the Quartz timer task will be executed according to the configured time
The full version of the Spring Boot+mybatis+quartz project