创建项目框架的步骤:
一. 定义好项目和各个模块名称
(1)根项目GroupId: com.ai.zhome; ArtifactId: projects
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artifactId是tomcat。
比如创建一个项目,一般会将groupId设置为cn.zhome,cn表示域为中国,zhome是我个人标识,artifactId设置为projects,表示你这个项目的名称是projects,依照这个设置,你的包结构最好是cn.zhome.projects打头的,如果有个StudentDao,它的全路径就是cn.zhome.projects.dao.StudentDao
二. 开始创建单项目
(1) Create New Project->Gradel->选择JDK版本和Java
(2)配置GAV
点击Finish;
三. 配置各配置文件
1. setting.gradle配置根项目名称
rootProject.name = 'pms'
此时项目显示
2. build.gradle配置
//buildScript里是gradle脚本本身执行所需依赖库和插件
buildscript {
ext {
springBootVersion = '2.1.8.RELEASE'
}
//repositories是project一个方法,闭包作为参数,资源库
repositories {
//本地仓库
mavenLocal()
//maven私服,此处设置为ali的,地址是url
maven {
name "public"
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
//远程中央仓库,地址是https://repo1.maven.org/maven2
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
//该项目所依赖的仓库
repositories {
mavenLocal()
maven {
name "public"
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.ai.zhome'
version '1.0-SNAPSHOT'
tasks.withType(JavaCompile){
//JDK版本,兼容性,编码环境
sourceCompatibility = 1.8
//编译环境
targetCompatibility = 1.8
//设置编码
options.encoding = "UTF-8"
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
implementation 'org.springframework.data:spring-data-redis'
implementation 'redis.clients:jedis'
implementation 'javax.persistence:persistence-api:1.0.2'
implementation 'com.alibaba:druid:1.1.9'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
3. 配置启动Application
package com.ai.zhome.pms.source;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SourceApplication {
public static void main(String[] args){
SpringApplication.run(SourceApplication.class, args);
}
}
/*
* 1. SpringBoot会自动加载数据源,如在配置文件未配置则报异常。取消启动时自动加载数据源,在application类配置
* @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
* 该配置会忽略数据源配置(没有配置,或者配置数据源错误都忽略)
* 2. 按ctrl+F9,重新自动构建
*/
4. 配置application.yml文件
spring:
profiles:
active: dev #激活application-dev.yml配置文件
application:
name: pms-projects
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
5. 配置application-dev.yml文件
server:
port: 8066 #端口
connection-timeout: 5000 #请求超时时间
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/zhome?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
thymeleaf:
cache: false
6. 构建项目,然后启动相关,成功!