1. 利用spring boot提供的工具(http://start.spring.io/)自动生成一个标准的spring boot项目架构

2. 因为这里我们是搭建spring boot+mybatis+quartz架构,故在pom.xml文件中配置相关依赖

<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. spring boot的理念是做到零配置,所以这里没有web.xml,只有一个application.yml的配置,这个时配置数据源及mybatis的mapper扫描路径

spring:
datasource:
url: jdbc:mysql://192.168.253.128/scheduler?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;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. 这些基本的配置完成之后项目的结构如下

这里static目录是存放HTML,JavaScript等前端代码的

5. Spring boot web是不需要部署在Tomcat下的,因为自带了Tomcat,只需要执行主程序Application的main方法就可以了。但需要做些重要的配置

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 main(String[] args) {
SpringApplication.run(DispachApplication.class, args);
}
}

这里的配置@SpringBootApplication是默认,注意第二个和第三个配置,如果缺少@MapperScan("com.hq.dispach.dao")配置则会出现dao无法注入到service的情况,如果@EnableAutoConfiguration缺少则会出现quartz任务调度里无法注入service,这里建议不管是什么应用程序都最好加上这个配置。

5. 接下来就是control,service,dao,mapper,scheduleTask的代码编写了

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 class ScheControl {
@Autowired
private ScheService scheService; @GetMapping("/addSche/{name}/{password}")
public Sche findScheByNameAndPassword(@PathVariable("name") String name,
@PathVariable("password") String password) {
return scheService.findScheByNameAndPassword(name, password);
}
}

Sevice:

package com.hq.dispach.service;

import com.hq.dispach.entity.Sche;

public interface ScheService {
public Sche findScheByNameAndPassword(String name,String password);
public String test(); }

Service实现

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 class ScheServiceImpl implements ScheService{
@Autowired
private ScheMapper scheMapper; public Sche findScheByNameAndPassword(String name, String password) {
return scheMapper.findScheByNameAndPassword(name, password);
} public String test() {
return "schedules run";
}
}

Dao:

package com.hq.dispach.dao;

import org.apache.ibatis.annotations.Param;

import com.hq.dispach.entity.Sche;

public interface ScheMapper {
public Sche findScheByNameAndPassword(@Param("name") String 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="com.hq.dispach.dao.ScheMapper"> <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 class ScheduledTasks{
@Autowired
private ScheService scheService; //每1分钟执行一次
@Scheduled(cron = "0 */1 * * * * ")
public void reportCurrentByCron(){
System.out.println(scheService.test());
}
}

Entity:

package com.hq.dispach.entity;

public class Sche {
private String id;
private String name;
private String password;
private String age;
private String department;
private String gendar;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getGendar() {
return gendar;
}
public void setGendar(String gendar) {
this.gendar = gendar;
}
}

6. 搭建好后启动application的main方法,通过页面访问control的URL可以获取数据库里的数据,同时quartz的定时任务会按照配置的时间定时执行

spring boot+mybatis+quartz项目的搭建完整版的更多相关文章

  1. spring Boot + MyBatis + Maven 项目,日志开启打印 sql

    在 spring Boot + MyBatis + Maven 项目中,日志开启打印 sql 的最简单方法,就是在文件 application.properties 中新增: logging.leve ...

  2. myEclipse 搭建 Spring boot+myBatis+maven 项目流程

    1.新建一个工程 new-->maven project-->next-->next-->在filter中搜索webapp-->group id.Artifact id- ...

  3. SSM Spring SpringMVC Mybatis框架整合Java配置完整版

    以前用着SSH都是老师给配好的,自己直接改就可以.但是公司主流还是SSM,就自己研究了一下Java版本的配置.网上大多是基于xnl的配置,但是越往后越新的项目都开始基于JavaConfig配置了,这也 ...

  4. spring boot + mybatis + druid

    因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能时数据库连接的问题,所以我打算 ...

  5. 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面

    贼烦的是,使用spring boot 创建web项目,然后我再idea下创建的,but 仅仅启动spring boot的启动类,就算整个项目都是好着的,就算是能够进入controller中,也不能成功 ...

  6. java 搭建新项目,最佳组合:spring boot + mybatis generator

    java 搭建新项目,最佳组合:spring boot + mybatis generator

  7. spring boot+mybatis搭建项目

    一.创建spring boot项目 1.File->New->Project 2.选择 Spring Initializr ,然后选择默认的 url 点击[Next]: 3.修改项目信息 ...

  8. 快速搭建一个Spring Boot + MyBatis的开发框架

    前言:Spring Boot的自动化配置确实非常强大,为了方便大家把项目迁移到Spring Boot,特意总结了一下如何快速搭建一个Spring Boot + MyBatis的简易文档,下面是简单的步 ...

  9. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

随机推荐

  1. 集合源码分析[2]-AbstractList 源码分析

    AbstractList 类型:抽象类 接口的继承以及实现关系 继承AbstractCollection 实现List接口 典型方法实现解析 public List<E> subList( ...

  2. 小白月赛13 小A的柱状图 (单调栈)

    链接:https://ac.nowcoder.com/acm/contest/549/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  3. POJ 1848 Tree 树形DP

    题目大意: 给出一棵树,现在要往这棵树上加边,使得所有的点都在环中,且每个点只能属于一个环 题解: 考虑DP: \(dp[i][0]\)表示使\(i\)这颗子树的每个点都在环内需要加的最少边数. \( ...

  4. 第二篇--PCI设备解析

    介绍:参考 一个系统上最多有256个PCI总线,每个总线最多有32个设备,每个设备最多有8个功能,每个功能最多有256字节的配置地址空间,所以总的配置地址空间是16M. PCI设备有物理设备和逻辑设备 ...

  5. redis基础知识思维导图

    看到一张redis的基础知识思维导图,比较清晰, 但是没有标明来源,希望知道的给个地址,我也好说明来源 图大小有1M多.在博客上看比较模糊,可以先下载下来查看,或者在浏览器新标签中打开图片查看,就比较 ...

  6. CentOS7_防火墙

    1.查看防火墙状态 firewall-cmd --state 2.启动防火墙 systemctl start firewalld.service 3.关闭防火墙 systemctl stop fire ...

  7. Angular+Ionic+RSA实现后端加密前端解密功能

    因业务需要,需要给android应用安装证书,通过读取证书文件内容实现某些功能的控制: 流程:后台通过publicKey对指定内容的文件进行加密,生成文件共客户下载,客户下载后选择该证书文件读取到应用 ...

  8. PMP知识点(三)——挣值计算汇总表

    在新标签页打开. 附参考图 资料地址:http://pan.baidu.com/s/1bMNroq

  9. Exp1:PC平台逆向破解 20164314 郭浏聿

    一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getS ...

  10. struts2 s2-032漏洞分析

    0x01Brief Description 最近面试几家公司,很多都问到了s2漏洞的原理,之前调试分析过java反序列化的漏洞,觉得s2漏洞应该不会太难,今天就分析了一下,然后发现其实漏洞的原理不难, ...