锦通 发表于 2024-8-26 19:04:20

Java:创建一个SpringBoot架构,并尝试访问一个简朴的HTML页面:Hello HTML.

下面我们开始教程:
第一步:创建Maven工程
https://img-blog.csdnimg.cn/direct/6ab11481b30a4077adc9b561dd127b4d.png
我这里是Maven工程:之后再在pom文件导入SpringBoot坐标:
https://img-blog.csdnimg.cn/direct/20c7d5c2d49c469fac2869889aa7c631.png
注:我的平台版本是2020.1,有可能跟各人的不太一样,但创建项目大体类似。Maven即可。直接
SpringBoot也可。
Next下一步:
https://img-blog.csdnimg.cn/direct/42cdf6903d1a4775ad3d4a717fb704f0.png
取名项目名称:Infomanage
Next下一步:
https://img-blog.csdnimg.cn/direct/4e041e95cb8e4d3bb36010091601392f.png
Fish:
然后进入pom.xml文件开始导入各种依赖:
https://img-blog.csdnimg.cn/direct/ec3a10a8fb3043279e6be1bc1fc1d129.png
导入依赖:
第一步:
继续父工程:(我们所开发的都是SpringBoot的子工程)
spring-boot-starter-parent https://img-blog.csdnimg.cn/direct/cc7f8c592ff644aa949f270de4d73d85.png
导入各种依赖:
<dependencies>
<!--    继承父工程 Spring-Boot-starter-parent启动器后,子工程的启动器版本无需再指定-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
      <!-- 添加spring-boot-configuration-processor的启动依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
<!--    lombok-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>
      <!-- 添加mysql-connector-java的启动依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
      <!-- 添加mybatis-plus-boot-starter的启动依赖 -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.2</version>
    </dependency>
      <!-- 添加Thymeleaf的启动依赖 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <!-- 添加spring-boot-starter-test的启动依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>
<build>
<!--    静态资源s-->
    <resources>
      <!--    静态资源-->
      <resource>
<!--      目录下-->
      <directory>src/main/java</directory>
<!--      囊括-->
      <includes>
<!--      匹配所有xml文件-->
      <include>**/*.xml</include>
      </includes>
      <filtering>true</filtering>
      </resource>
    </resources>
    <plugins>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
          <source>9</source>
          <target>9</target>
      </configuration>
      </plugin>
    </plugins>
</build> 点击M刷新:开始导入:需要联网***,这样底部就出现了蓝色进度条:耐心等待一会儿
https://img-blog.csdnimg.cn/direct/e37348613e1346d0b27afa79fd90eaf7.png
下一步:在蓝色的Sources的java包下(源码包)开始建包:com.infomanage
https://img-blog.csdnimg.cn/direct/91b18da16bbb48509f5ac8a77b81847f.png
在resources包下:创建存放静态资源、各种html文档的包templates:
在templates下创建一个html包:包下创建一个hello.html
https://img-blog.csdnimg.cn/direct/f3b484729a434818bc120bad9c849f99.png
hello.html:
https://img-blog.csdnimg.cn/direct/b0603af7ca78486cb0ce3bdf1175597e.png

在resources下创建SpringBoot的设置文件:application.yml和application-dev.yml:
https://img-blog.csdnimg.cn/direct/1c593fc41bf447f1922032f2e81b126e.png
在application.yml中设置前后端之间的映射端口、激活dev文件、数据库链接等等各种设置:
参考:
application.yml:
server:
port: 8089

spring:
profiles:
    active: dev
main:
    allow-circular-references: true
datasource:
    url: ${datasource.url}
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: ${datasource.username}
    password: ${datasource.password}
    hikari:
      maximum-pool-size: ${connectionpool.maximumpoolsize}
      minimum-idle: ${connectionpool.minimumidle}
      connection-timeout: ${connectionpool.connectiontimeout}
      pool-name: ${connectionpool.poolname}
      idle-timeout: ${connectionpool.idletimeout}
      max-lifetime: ${connectionpool.maxlifetime}
thymeleaf:
    mode: HTML
    cache: false
    prefix: classpath:/templates/html/
    suffix: .html
    #注意:classpath:/templates/html/中,templates后的/html是个人之后再继续创建的一个包,
    所以这里的html前缀须再加上/html,
    因为html文档都是再html下的。
    SpringBoot的thymeleaf默认的是到/templates/
    。


logging:
level:
    com.infomanage.mapper: debug

mybatis-plus:
type-aliases-package: com.infomanage.pojo

application-dev.yml:
datasource:
url: jdbc:mysql://127.0.0.1:3306/attendance?characterEncoding=UTF-8&useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123456
   #attendance是数据库名
connectionpool:
maximumpoolsize: 50
minimumidle: 10
connectiontimeout: 600000
poolname: connectionpool1
idletimeout: 60000
maxlifetime: 60000 基本建成了一个SpringBoot开端工程:
创建Controller层:
并创建一个LoginController类:
https://img-blog.csdnimg.cn/direct/b2e8934770db4aaeb530ce4105bb8076.png
LoginController类:
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/login")
@Slf4j
public class LoginControl {
    @GetMapping("/")
    public String hello(){
      log.info("开始访问...");
      return "hello";
    }
} 最后,建一个启动类:Run 
启动类Run的位置需要在当前模块包下的第一层:即com.infomanage下即可:之后创建的各种类都必须在com.infomanage下的:当前包及其子包下:
https://img-blog.csdnimg.cn/direct/0615062f5d114551afbe40bd9a6ac337.png
点击绿色三角形按钮:启动SpringBoot
https://img-blog.csdnimg.cn/direct/d1dfd17df1844bda8d035f3104acedb7.png
打开欣赏器:输入设置好的端口信息即转发地点信息:
http://localhost:8089/login/
https://img-blog.csdnimg.cn/direct/026e5f055180433b9e76bf318f78c1de.png
回车:
https://img-blog.csdnimg.cn/direct/ec6dc53e43694da6b29205fba7c400aa.png
看后台:
https://img-blog.csdnimg.cn/direct/5b1e3d9954ec45c0ba447ae42fe34b62.png
控制层:
https://img-blog.csdnimg.cn/direct/67001e7eceaf4650ad68acbc5e6fd40b.png
这样,一个简朴的SpringBoot开端工程就就建成了,并简朴进行访问了一个Html页面。
拓展:

在IDEA集成数据库的连接:这样在写长期层的时候,mybatis或mybatis-plus会有SQL语句的提示:效率会高许多:https://img-blog.csdnimg.cn/direct/6d0a556cced74584b08ad81ae55e1e01.png
第一步:
https://img-blog.csdnimg.cn/direct/3fb4564e89a54e5789c88b573061796a.png
首先你要开启你的MySQL:
这里演示的是MySQL:
https://img-blog.csdnimg.cn/direct/d06f90b3a0ec4e549fdc02646a99ba81.png
https://img-blog.csdnimg.cn/direct/aeafb5b618dd4145afaa870434ca2fed.png
https://img-blog.csdnimg.cn/direct/36b8fe88485f4fe8975e2b2ed62c9a4c.png
点击ok:
设置完成:
https://img-blog.csdnimg.cn/direct/b74afd3866584e6187985271d73b8e79.png
这样在写Mapper.xml就高效多了。
以上是本期内容。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Java:创建一个SpringBoot架构,并尝试访问一个简朴的HTML页面:Hello HTML.