Spring Boot快速搭建Web项目基本框架
2023-09-27 14:27:54 时间
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创建目录结构和项目包结构
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仓库 立即下载
相关文章
- Spring boot项目使用Base64加密,部署到Windows系统tomcat中遇到中文乱码问题
- 深入实践Spring Boot1.5 关于Spring Boot配置
- 《深入实践Spring Boot》一1.2 创建项目工程
- 在Spring boot项目中实现多数据源的方案
- 搭建Spring Boot+Spring Cloud微服务
- Spring Boot全面总结(超详细,建议收藏)
- Spring Boot整合Mybatis(注解版+XML版)
- Spring Boot 项目打包 + Shell 脚本部署
- 注意:Spring Boot 2.7开始spring.factories不推荐使用了,接下来这么玩...
- Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面
- springboot(三):Spring boot中Redis的使用
- 江帅帅:Spring Boot 底层级探索系列 01- 搭建项目
- 最详细的自定义Spring Boot Starter开发教程
- Spring Boot JPA 中transaction的使用
- Spring Boot Actuator
- 【MySQL】Spring Boot项目基于Sharding-JDBC和MySQL主从复制实现读写分离(8千字详细教程)
- Spring Boot 定时任务的使用
- spring boot no identifier specified for entity
- [转] Spring Boot 自动配置之@Enable* 与@Import注解
- 12 Spring Boot密码加密算法