springboot框架构建

完整项目下载: https://pan.baidu.com/s/1PjzQZKpezKU7jnpdBWxGDw

1. 开始,右键



2. next

3. finish

4. 然后IDE就开始下载生成项目,可能是网速的原因吧,下载接近5分钟

下载失败或者速度慢可以下载顶部的文件,那是一个完整maven项目,下完之后把文件名和pox.xml的name改成你的项目名即可,如下图


5. pom.xm

<?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>springboot-template</groupId>
	<artifactId>springboot-template</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>springboot-template</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.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>
		
		<!--引入themeleaf-->  
        <thymeleaf.version> 3.0.2.RELEASE </thymeleaf.version>  
        <thymeleaf-layout-dialect.version> 2.1.1 </thymeleaf-layout-dialect.version>  
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<version>7.0.59</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-rest</artifactId>
		</dependency>
		<!-- 添加servlet-api的依赖 -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-servlet-api</artifactId>
			<version>8.0.36</version>
			<scope>provided</scope>
		</dependency>
		<!--spring boot 整合 mybatis 依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>1.5.0</version>
		</dependency>
		<!-- 添加热不熟,开发者模式 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<!-- true 依赖不会传递,依赖devtools -->
		</dependency>
		<!-- 添加对mysql数据库的支持 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.34</version>
		</dependency>
		<!-- web项目, 对tomcat的支持 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.11</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.datatype</groupId>
			<artifactId>jackson-datatype-joda</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.module</groupId>
			<artifactId>jackson-module-parameter-names</artifactId>
		</dependency>
		<!-- alibaba的druid数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.0</version>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!-- 日志处理 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>
		<!-- 时间操作组件 -->
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
		</dependency>
		<!-- Apache工具组件 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.3</version>
		</dependency>
		<!-- Jackson Json处理工具包 -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>
		<!-- httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-util</artifactId>
			<version>9.3.7.v20160115</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.mail</groupId>
			<artifactId>mail</artifactId>
			<version>0.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.assertj</groupId>
			<artifactId>assertj-core</artifactId>
			<version>3.6.0</version>
		</dependency>
		<!-- 阿里云OS -->
		<dependency>
			<groupId>com.aliyun.oss3</groupId>
			<artifactId>aliyun-sdk-oss3</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>com.hamcrest.core</groupId>
			<artifactId>hamcrest-core</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>com.httpcore</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.1</version>
		</dependency>
		<dependency>
			<groupId>com.jdom</groupId>
			<artifactId>jdom</artifactId>
			<version>1.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- 添加热部署 -->
				<configuration>
					<fork>trie</fork>
					<!-- 跳过测试代码 -->
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

把 <dependencies> 和 </project> 之间的替换掉即可;

6. 包结构及启动类

下载顶部文件,把main目录下的文件复制到main下即可,controller文件包含启动类;



启动类:

package cn.nordrassil.controller;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 启动整体项目的启动类 启动项目的方式: 1.运行此类即可; 2.添加到tomcat中发布使用也可
 * 
 * @author guyin
 *
 */
@ComponentScan(basePackages = { "cn.nordrassil" }) /* 1.扫描com.nordrassil包下面的所有spring相关的组件 */
@EntityScan("cn.nordrassil.entity") /* 3.加载JavaBean */
@SpringBootApplication /* 4.加载springboot组件 */
@EnableScheduling // Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。
@MapperScan("cn.nordrassil.mapper")
@EnableCaching
public class SpringbootApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}

	/** 
     * 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式 
     */  
    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
        builder.sources(this.getClass());    
        return super.configure(builder);   
    } 
}

7. 配置文件结构

删除application.properties,创建application.yml

application.yml 

guyin: 
  number: ${random.int} # 随机数字
  number2: ${random.int[1024,9999]} # 随机数字
  hello: hello,spring boot !!!  ${guyin.number} ${guyin.number2}

server: 
  port: 9001
  tomcat: 
    uri-encoding: UTF-8
management:  
  security:  
    enabled: false # spring-boot 1.5.2之后严格执行安全策略,所以需要配置这个为false 

spring:

  #thymeleaf start
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
    content-type: text/html
    #开发时关闭缓存,不然没法看到实时页面
    cache: false
  #thymeleaf end
      
  # 数据库
  datasource:
      name: test
      url: jdbc:mysql://localhost:3360/tes
      username: root
      password: xxxx
      # 使用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
      
  # REDIS (RedisProperties)
  # Redis数据库索引(默认为0)
  redis: 
    database: 0  
    # Redis服务器地址
    host: 192.168.1.140
    # Redis服务器连接端口
    port: 6379  
    # Redis服务器连接密码(默认为空)
    password:
    # 连接超时时间(毫秒)
    timeout: 0   
    # 连接池最大连接数(使用负值表示没有限制)
    pool: 
      max-active: 8  
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      max-wait: -1  
      # 连接池中的最大空闲连接
      max-idle: 8  
      # 连接池中的最小空闲连接
      min-idle: 0 
      
  # 编码设置
  http: 
    encoding: 
      force: true
      charset: UTF-8
      enabled: true

# mybatis
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: cn.nordrassil.entity
 
# 打印sql
logging:
  level: 
    cn.nordrassil.mapper: DEBUG

# pagehelper分页插件
pagehelper: 
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSq

END

猜你喜欢

转载自blog.csdn.net/Lxinccode/article/details/80967629