spring boot+mybatis+quartz项目的搭建完整版
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&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. 这些基本的配置完成之后项目的结构如下

这里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项目的搭建完整版的更多相关文章
- spring Boot + MyBatis + Maven 项目,日志开启打印 sql
在 spring Boot + MyBatis + Maven 项目中,日志开启打印 sql 的最简单方法,就是在文件 application.properties 中新增: logging.leve ...
- myEclipse 搭建 Spring boot+myBatis+maven 项目流程
1.新建一个工程 new-->maven project-->next-->next-->在filter中搜索webapp-->group id.Artifact id- ...
- SSM Spring SpringMVC Mybatis框架整合Java配置完整版
以前用着SSH都是老师给配好的,自己直接改就可以.但是公司主流还是SSM,就自己研究了一下Java版本的配置.网上大多是基于xnl的配置,但是越往后越新的项目都开始基于JavaConfig配置了,这也 ...
- spring boot + mybatis + druid
因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能时数据库连接的问题,所以我打算 ...
- 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面
贼烦的是,使用spring boot 创建web项目,然后我再idea下创建的,but 仅仅启动spring boot的启动类,就算整个项目都是好着的,就算是能够进入controller中,也不能成功 ...
- java 搭建新项目,最佳组合:spring boot + mybatis generator
java 搭建新项目,最佳组合:spring boot + mybatis generator
- spring boot+mybatis搭建项目
一.创建spring boot项目 1.File->New->Project 2.选择 Spring Initializr ,然后选择默认的 url 点击[Next]: 3.修改项目信息 ...
- 快速搭建一个Spring Boot + MyBatis的开发框架
前言:Spring Boot的自动化配置确实非常强大,为了方便大家把项目迁移到Spring Boot,特意总结了一下如何快速搭建一个Spring Boot + MyBatis的简易文档,下面是简单的步 ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
随机推荐
- 题解:LOJ540游戏
题目描述 小L计划进行n场游戏,每场游戏使用一张地图,小 L 会同时使用三辆车在该地图上完成游戏. 小 L 的赛车有三辆,分别用大写字母 A.B.C 表示.地图是一张无向简单图(没有重边或自环),每次 ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- 集合源码分析[2]-AbstractList 源码分析
AbstractList 类型:抽象类 接口的继承以及实现关系 继承AbstractCollection 实现List接口 典型方法实现解析 public List<E> subList( ...
- 数位DP::SoSDP
数位DP:: SoSDP 学习博客(待补) 下面做一些例题: SPECIAL PAIRS 题意 给n个数字,求这些数字有多少对的\(AND\) 结果是0.数字不大于1e6.顺序反相反视为不同的对. 思 ...
- mysq建表参数设置
建表的完整性约束: not null 与 default unique primary auto_increment foreign key 外键的变种 三种关系 一.介绍 约束条件与数据类型的宽度 ...
- C#调用Java的WebService添加SOAPHeader验证(2)
C#调用Java的WebService添加SOAPHeader验证 上一篇链接如上,更像是 Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) 来处理xml, ...
- C#调用DLL文件提示试图加载格式不正确的程序
最近在调用第三方的DLL遇到了一个问题:试图加载格式不正确的程序,找了好久发现都没有解决问题,一直报错,后来发现是程序编译时的配置错了,要将项目->属性->平台选中release-X86 ...
- CentOS使用@Value注解为属性赋值的时候出现乱码
在本地开发用windows的没有出现乱码,在CentOS上运行的时候出现乱码. 1.修改中文的编码方式 (成功) env.properties为ANSI格式 先设置idea编码格式,utf-8, 将 ...
- C#代码使用Process类调用SWFTools工具
一.Process类调用SWFTools工具将PDF文档转为swf文档 1 string cmdStr = "D:\\SWFTools\\pdf2swf.exe"; string ...
- 关于dfs
DFS 关于dfs,我的理解就是深度搜索,找到所有与入口相连的路径,可以用于迷宫求出口,利用递归的思想,进行搜索返回所有值. 比如,给你两个值分别表示迷宫的长和宽,迷宫有一个入口,一个出口,判断能否从 ...