一、该技术是什么?
1、spring Boot是Spring社区较新的一个项目。该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验,让Java开发也能够实现Ruby on Rails那样的生产效率。为Spring生态系统提供了一种固定的、约定优于配置风格的框架。
2、spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功能,让我们快速构建企业级应用程序。
二、该技术有什么特点(使用注意)?
Spring Boot具有如下特性:
为基于Spring的开发提供更快的入门体验。
开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
Spring Boot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。
三、该技术怎么使用?
技术参考:http://blog.csdn.net/u011054333/article/details/62976388
四、该技术什么时候用?
1、新建工程
创建项目很简单。如果使用STS的话,新建Spring Starter项目即可。如果使用IDEA的话,新建Spring Initializer项目。如果不想使用IDE的话,从start.spring.io创建项目也可以。
这里我使用的是STS,直接新建Spring Starter项目,项目命名为springboot-demo,初始结构如下图:
2、项目配置
1、配置MySQL数据库
application.properties文件配置
# DataSource
spring.datasource.url=jdbc:mysql://IP:端口/erp_dev
spring.datasource.username=erp_dev
spring.datasource.password=erp_dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2、集成MyBatis框架
加入pom.xml文件配置
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
3、根据你的业务需求完善项目结构
我本次开发的是定时任务系统,只集成了mybatis,项目结构中只使用到了dao、service,如果你开发的是web工程,请另外完善结构和集成所需资源。
4、springboot-demo源码(供参考)
TradeOrder.java
package com.example.demo.entity;
import java.util.Date;
public class TradeOrder {
private int id;
private String platform;
private Date createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
TradeOrderDao.java
package com.example.demo.dao;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.example.demo.entity.TradeOrder;
public interface TradeOrderDao {
List<TradeOrder> selectBySiteAndTime(@Param("platform")String platform,@Param("beginTime")Date beginTime,@Param("endTime")Date endTime);
}
TradeOrderDao.xml
<?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.example.demo.dao.TradeOrderDao">
<resultMap type="com.example.demo.entity.TradeOrder" id="TradeOrderMap">
<id column="id" property="id" />
<result column="platform" property="platform" />
<result column="create_time" property="createTime" />
</resultMap>
<select id="selectBySiteAndTime" parameterType="java.util.Map" resultMap="TradeOrderMap">
SELECT id,platform,create_time FROM trade_order
WHERE platform=#{platform} and create_time between #{beginTime} and #{endTime}
</select>
</mapper>
TradeOrderService.java
package com.example.demo.service;
import java.util.Date;
import java.util.List;
import com.example.demo.entity.TradeOrder;
public interface TradeOrderService {
List<TradeOrder> findBySiteAndTime(String platform,Date beginTime,Date endTime);
}
TradeOrderServiceImpl.java
package com.example.demo.service.impl;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dao.TradeOrderDao;
import com.example.demo.entity.TradeOrder;
import com.example.demo.service.TradeOrderService;
@Service("tradeOrderService")
public class TradeOrderServiceImpl implements TradeOrderService {
@Autowired
private TradeOrderDao tradeOrderDao;
@Override
public List<TradeOrder> findBySiteAndTime(String platform, Date beginTime, Date endTime) {
return tradeOrderDao.selectBySiteAndTime(platform, beginTime, endTime);
}
}
Demo.java
package com.example.demo.task;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.example.demo.entity.TradeOrder;
import com.example.demo.service.TradeOrderService;
/**
* 使用Spring的@Scheduled实现定时任务
**/
@Component
public class Demo {
//加载日志
private final static Logger logger = Logger.getLogger(Demo.class);
@Autowired
private TradeOrderService tradeOrderService;
/*每3秒钟执行一次,如简单的定期调度不能满足你的业务需求,请直接度娘@Scheduled,使用其它表达式,如cron( @Scheduled(cron = "0 0 2 * * ?") 每天凌晨两点执行)*/
@Scheduled(fixedDelay=3 * 1000l)
public void demo(){
Date now=new Date();
long time = 30*60*1000l;//30分钟
List<TradeOrder> listAmazon=tradeOrderService.findBySiteAndTime("Amazon", new Date(now .getTime() - time), now);
if (listAmazon.size()==0) {
logger.info("正在执行。。。");
}
}
}
SpringbootDemoApplication.java
package com.example.demo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
@EnableScheduling
@MapperScan("com.example.demo.dao")
public class SpringbootDemoApplication {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
DataSource dataSource = new DataSource();
dataSource.setUrl("jdbc:mysql://192.168.2.60:3306/erp_dev");
dataSource.setUsername("erp_dev");
dataSource.setPassword("erp_dev");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
application.properties
# DataSource
spring.datasource.url=jdbc:mysql://IP:端口/erp_dev
spring.datasource.username=erp_dev
spring.datasource.password=erp_dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>