zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring Boot快速搭建Web项目基本框架

SpringBoot项目Web框架 快速 搭建 基本
2023-09-27 14:27:54 时间

image
image

配置pom.xml
 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" 

 properties 

 project.build.sourceEncoding UTF-8 /project.build.sourceEncoding 

 java.version 1.8 /java.version 

 maven.compiler.source 1.8 /maven.compiler.source 

 maven.compiler.target 1.8 /maven.compiler.target 

 thymeleaf.version 3.0.8.RELEASE /thymeleaf.version 

 thymeleaf-layout-dialect.version 2.0.5 /thymeleaf-layout-dialect.version 

 /properties 

 /project 
依赖父项目
 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" 

 parent 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-parent /artifactId 

 version 1.5.7.RELEASE /version 

 /parent 

 /project 
引入web项目核心依赖包
spring-boot-starter-web

如果使用thymeleaf模版,可直接引入spring-boot-starter-thymeleaf,这个已经包含了spring-boot-starter-web的依赖

 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" 

 dependencies 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-thymeleaf /artifactId 

 /dependency 

 /dependencies 

 /project 

如果还用到了thymeleaf的layout layout dialect,还需要引入另外一个依赖

 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" 

 dependencies 

 dependency 

 groupId nz.net.ultraq.thymeleaf /groupId 

 artifactId thymeleaf-layout-dialect /artifactId 

 /dependency 

 /dependencies 

 /project 
实现项目的热部署 添加devtools依赖
 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" 

 dependencies 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-devtools /artifactId 

 optional true /optional 

 /dependency 

 /dependencies 

 /project 
添加maven-plugin插件
 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" 

 build 

 plugins 

 plugin 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-maven-plugin /artifactId 

 /plugin 

 /plugins 

 /build 

 /project 

添加依赖

 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" 

 dependencies 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-test /artifactId 

 scope test /scope 

 /dependency 

 /dependencies 

 /project 
创建目录结构和项目包结构

image
Application.java

@SpringBootApplication

public class Application {

 public static void main(String[] args) {

 SpringApplication.run(Application.class, args);

}

application.yml

server:

 port: 8181
配置Thymeleaf
package com.simpth.autumn.config;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.thymeleaf.spring4.SpringTemplateEngine;

import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;

import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import org.thymeleaf.templatemode.TemplateMode;

import nz.net.ultraq.thymeleaf.LayoutDialect;

@Configuration

public class ThymeleafConfig {


@Bean public SpringResourceTemplateResolver templateResolver(ApplicationContext applicationContext) { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(applicationContext); //配置模板文件所在位置 templateResolver.setPrefix("classpath:/templates/"); //配置模板文件后缀 templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); //开发过程中配置为不开启缓存,以便开发过程中,所更改的模板代码能实时生效;上线时需设置为true templateResolver.setCacheable(false); templateResolver.setCharacterEncoding("UTF-8"); return templateResolver; @Bean public SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) { // SpringTemplateEngine automatically applies SpringStandardDialect and // enables Springs own MessageSource message resolution mechanisms. SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); // Enabling the SpringEL compiler with Spring 4.2.4 or newer can // speed up execution in most scenarios, but might be incompatible // with specific cases when expressions in one template are reused // across different data types, so this flag is "false" by default // for safer backwards compatibility. //templateEngine.setEnableSpringELCompiler(true); templateEngine.addDialect(new LayoutDialect()); return templateEngine; @Bean public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine); return viewResolver; }

Spring boot 使用 ON DUPLICATE KEY UPDATE属性控制版本 更新数据不成功 如果更新不成功会报下面的错误(此错误是自定义的): The data you want to update has been updated by another user. Please reopen and try again! 一、主要按下面的流程检查: 1、检查数据库的段alastupdatetime定义 2、检查.xml的alastupdatetime 3、要更新数据的主键是否在同一个updateByBatch中的List中重复出现
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载