栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

快速创建Spring Boot项目(with Spring Initializr)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

快速创建Spring Boot项目(with Spring Initializr)

一、在spring 官网中,提供了快速搭建Spring Boot的方式  放置在网页最下方  点击即可进入如下页面 二、开始搭建Spring Boot项目         1.选择项目的基本属性         2.选择添加技术集,添加依赖(Dependencies)

在Dependencies中添加下图的依赖项

种类依赖名
WEBSpring web
TEMPLATE ENGINES  (模板引擎)Thymeleaf  (百夜香)
SECURITY  (安全)Spring Security
SQL   (数据库)JDBC API
MyBatis Framework
Spring Data JPA
Spring Data JDBC
        3.点击generate(生成)按钮,生成并下载代码框架

         4. 由于下载的是压缩包,将文件解压后在idea中打开这个文件夹

        由于要访问数据库,所以需要配置数据库驱动程序,同样采用maven管理,我们到mvnrepositories网站 搜索需要的软件包,并拷贝其maven依赖片段到pom.xml中

 

 最后将下面的依赖粘贴到pom.xml中。(如果用的mariadb数据库,则要用mariadb关键词进行搜索)


    mysql
    mysql-connector-java
    8.0.29

        5.配置IDEA使用的maven版本,通过菜单File|settings打开对话框,搜索maven,将maven改成内置的版本, 并确定

 

        6.刚才生成的pom.xml中包含了很多的配置,我们可以将暂时不用的部分注释掉,这样相关库和配置就不 会进入我们的程序,可以先注释掉(按ctrl+/,再次按可取消注释):

spring-boot-starter-data-jdbc

spring-boot-starter-data-jpa

spring-boot-starter-thymeleaf

spring-boot-starter-security

mybatis-spring-boot-starter 

最终形成的pom.xml 如下



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.7.0
		 
	
	com.example
	demo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
		17
	
	








		
			org.springframework.boot
			spring-boot-starter-jdbc
		








		
			org.springframework.boot
			spring-boot-starter-web
		





		
			org.thymeleaf.extras
			thymeleaf-extras-springsecurity5
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.security
			spring-security-test
			test
		
		
			mysql
			mysql-connector-java
			8.0.29
		
	
        7.默认的项目配置文件 是application.properties,现在更多的是使用 yml 的格式。将文件改名成yml格式。

 配置文件中初始是空的,编辑内容如下

spring:
 datasource:
 url: jdbc:mysql://127.0.0.1:3306/news?
serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
 username: news
 password: 123456
 driver-class-name: com.mysql.jdbc.Driver
mybatis:
# type-aliases-package: com.ldsoft.model
 configuration:
 map-underscore-to-camel-case: true
        8.在Application.java文件(主启动类)的同级别位置

添加一个业务类,名字任意取,比如controller,其内容如下

        9. 打开Demo1Application.java文件,这是SpringBoot程序的启动文件,一般不用修改,点击右键,在菜单 中选择Run,在console窗口中会看到启动过程,如果启动失败或遇到异常,也可以看到详细错误信息。 启动完成之后,spring boot应用在本地端口8080监听用户请求。通过console左侧的按钮,可以停止或重新运行spring boot应用。

 在 主类 的前面添加备注

@SpringBootApplication(exclude={

SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class,DataSourceAutoConfiguration.class

})

        点击运行可在下面的框格中看见spring boot的日志如下

        9.在浏览器地址框里面输入

localhost:8080/hello

即可在网页上显示,你刚刚做的SpringBoot项目内容。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991718.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号