SpringBoot入门知识

SpringBoot简介

Spring Boot 是由 Pivotal[ˈpɪvətl]团队提供的全新框架,其设计目的是用来简化新 Spring
应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再
需要定义样板化的配置。通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域
(rapid application development)成为领导者。
简单来说,SpringBoot 可以简化 Spring 应用程序的开发,使我们不再需要 Spring 配置文
件及 web.xml 文件

 

SpringBoot的工程创建(在idea中创建)

添加依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
   <!-- 数据库连接-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <!-- 引入依赖-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

编写Controller类

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
}

编写配置(application.propertise)

#mysql连接
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/myschool
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#oracle连接
spring.datasource.username=system
spring.datasource.password=root
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

application属性文件中常用的配置

Server这块常用的配置(tomcat的相关信息)
server.port ---------服务器端口
server.servlet.context-path  ---------应用上下文路径
server.tomcat.uri-encoding=UTF-8 ----读取属性配置文件,项目文件编码成功设置,页面还乱码时候设置
Tomcat这块常用的配置
server.tomcat.accesslog.directory ----创建日志文件的目录
server.tomcat.accesslog.enabled  ---------是否开启访问日志(默认:false)
server.tomcat.accesslog.pattern  ---------访问日志的格式(默认common)
server.tomcat.accesslog.prefix ---------日志名前缀(默认:access_log)
server.tomcat.accesslog.suffix ---------日志名后缀(默认:.log)
server.tomcat.max-http-header-sizeHttp ---------消息头最大字节数(默认:0)
server.tomcat.uri-encoding  ---------用来解码 URI 的字符编码
DataSource这块常用的配置
spring.datasource.username   ----数据库的登录用户名。
spring.datasource.password   ---数据库的登录密码。
spring.datasource.url         ----数据库的 JDBC URL。
spring.datasource.driver-class-name   ----JDBC 驱动的全限定类名。默认根据 URL 自动检测。

spring.datasource.name   ---数据源的名称。
spring.datasource.pool-name    ----连接池名称。
spring.datasource.max-active    ----连接池中的最大活跃连接数。
spring.datasource.connection-timeout  ----连接超时(单位毫秒)
spring.datasource.max-idle   -----连接池中的最大空闲连接数。
Redis这块常用的配置
spring.redis.database        ----连接工厂使用的数据库索引。(默认值: 0 。)
spring.redis.host         ----Redis服务器主机地址。(默认值: localhost 。)
spring.redis.password        ----Redis服务器的登录密码。
spring.redis.port         ----Redis服务器端口。(默认值: 6379 。)
spring.redis.timeout        ----连接超时时间,单位为秒。(默认值: 0 。)
spring.redis.pool.max-active ---连接池在指定时间里能分配的最大连接数。负数表示无限制。默认值: 8
spring.redis.pool.max-idle  ---连接池里的最大空闲连接数。负数表示空闲连接数可以是无限大。默认值:8

springboot注解版

@Mapper  //指定这是一个操作数据库的mapper
public interface SubjectMapper {
    @Select("select * from Subject where subjectNo=#{subjectNo}")
    public Subject getSubjectById(Integer subjectNo);
}

配置热部署

    目前的Springboot,当发生了任何修改之后,必须关闭后再启动Application类才能够生效,显得略微麻烦。 Springboot提供了热部署的方式,当发现任何类发生了改变,马上通过JVM类加载的方式,加载最新的类到虚拟机中。 这样就不需要重新启动也能看到修改后的效果了。

 

<!-- 配置springBoot项目的热部署启动 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

<!--之后在pom.xml的插件中加入下图红框中所示的配置,用于开启热部署-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

 

Thymeleaf入门

导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
注:可切换thymeleaf版本
<properties>
   <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
   <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

在html页面导入

<html lang="en" xmlns:th="http://www.thymeleaf.org">

常用标签如下:

标签

说明

th:insert

th:replace

Html代码片段包含,类似于jsp:include

th:each

遍历,类似于c:forEach

th:if

th:swtich

th:case

条件判断:类似于c:if

th:object

th:with

声明变量,类似于c:set

th:value

th:href

th:src

修改指定的属性默认值,如th:href修改a标签中的href属性。

th:src修改script中的src属性。

th:text

th:utext

修改标签体的内容,th:text会进行转义字符串,如<th:text=”${name}”/>如果取出的值中的<h1>标签,那么在页面上展示的效果是<h1>

th:utext才会解析成一级标题

th:fragment

声明HTML代码片段。用于重用页面

 

 

 

表达式

说明

 

${...}

获取变量值

 

SelectionVariable Expressions: *{...}

选择表达式,和${}功能是一样。配置th:object

<p th:object="${user}">
 ssss:<span th:text="*{id}"></span>- <span th:text="*{name}"></span>
</p>

 

Link URL Expressions: @{...}

 

定义URL链接。

如<a href="#" th:href="@{/order/details(orderId=${o.id})}">view</a> 

 

Message Expressions: #{...}

获取国际化内容。

 

Fragment Expressions: ~{...}

片段引用表达式

如<div th:insert="~{commons :: main}"></div>

数学运算

and,or

 

 

! , not

 

比较运算

>,<,>=,<= ( gt, lt, ge, le )

 

 

==, != , ( eq, ne )

 

条件运算(三元运算)

(if) ? (then)

(if) ? (then) : (else)

(value) ?: (defaultvalue)

 

示例:

在Controller中添加一个请求
@RequestMapping("/")
public String testThymeLeaf(Model model) {
    model.addAttribute("uname", "我的名字是<h1>tina</h1>");
    model.addAttribute("list", gradeService.getListGrade());
    return "success";
}
创建success.html页面,显示数据
<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>test thymeleaf</title>
</head>

<body class="text-center">
<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<!--里面的h1会显示到页面,不会被解析成h1标签-->
<p th:text="${uname}"></p>
<!--里面的h1会被解析成h1标签,不会看见h1几个字-->
<p th:utext="${uname}"></p>
<table>
    <tr>
        <th>部门编码</th>
        <th>部门名称</th>
        <th>操作</th>
    </tr>
    <tr th:each="dept:${listDept}">
        <td>[[${dept.id}]]</td>
        <td th:text="${dept.dname}"></td>
        <td >
<!-- @{/del(id=value,name=value)}相当于 /del?id=value&name=value-->
            <a href="#" th:href="@{/del(id=${dept.id})}">删除</a>
        </td>
    </tr>
</table>
</body>
</html>
posted @ 2020-01-22 21:52  蜗牛LSQ  阅读(153)  评论(0编辑  收藏  举报