SpringBoot2

 

一 回顾    

  1. 谈谈你如何理解springboot
  2. Springboot常用的注解有哪些分别有什么用
  3. Springboot的原理是什么
  4. 什么是Java配置

总结见:https://mp.csdn.net/postedit/88732742

二 视图渲染

2.1、模板引擎

      我们之前使用的视图都是JSP,但是JSP速度比较慢(本质是servlet),我们的SpringBoot也不建议我们使用JSP,SpringBoot给我们提供了很多种模板引擎的默认配置有:Thymeleaf,FreeMarker,Velocity,Groovy,Mustache。我们使用的时候无需做多余的配置,只需要配置启动器即可。

注:模板引擎有哪些?:Thymeleaf,FreeMarker,Velocity,Groovy,Mustache

       使用模板引擎的好处:SpringBoot给我们提供了很多种模板引擎的默认配置,我们使用模板引擎的时候无需做多余的配置,只需要配置启动器即可。

2.2、Thymeleaf

    我们使用Thymeleaf是在HTML文件中完成的(在jsp中也可以,不过麻烦一些,需要引入jsp和jstl的依赖,还得配置一下),并且实现动态数据,使用这些模板引擎的一大好处就是前后端分离。

2.3、开发步骤

2.3.1、thymeleaf入门案例

    注意:如果我们要使用Thymeleaf当做模板引擎,第一步我们需要在pom文件中引入依赖,就会使用它的默认配置,

              springboot要到templates目录中找相关的html文件

2.3.2、thymeleaf表达式

     我们需要使用这些表达式完成前后台的数据交互

第一步:创建maven项目

SpringBoot2

SpringBoot2

SpringBoot2

 

SpringBoot2

 

SpringBoot2

案例一:显示后台传入数据

SpringBoot2

SpringBoot2

2.3.3、thymeleaf链接表达式

作用:引入css,js,发送请求

案例二:a标签发送数据

语法:<a th:href=”@{URL(k1=v1,k2=v2,...)}”></a>

SpringBoot2

src:引入js  action:引入input  href:引入地址

2.3.4、变量表达式

 

三、mybatis整合

 3.1、步骤

  1)创建项目并且在pom文件中引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.zhiyou100</groupId>
	<artifactId>day3_22</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>day3_22</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<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>
		<!--引入springmvc-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--引入thymeleaf依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--mysql依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!--mybatis依赖-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.2.0</version>
		</dependency>
	</dependencies>

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

</project>

SpringBoot2

2)在application.properties文件中配置数据库连接

#mysql驱动类,mysql6.X之后驱动类是com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库地址serverTimezone=GMT%2B8,解决数据库时间问题
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8
#用户
spring.datasource.username=root
#密码
spring.datasource.password=123
#读取实体类映射文件,存放到resources目录下的mapper文件夹中
mybatis.mapper-locations=classpath:mapper/*.xml

SpringBoot2

 

3.2、案例

    需求:使用springboot+mybatis实现数据操作

练习:

SpringBoot2

步骤:

    1 创建数据库

SpringBoot2

如果运行时报错:

   java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required  说明sql版本不一致

修改方式:

SpringBoot2

SpringBoot2

SpringBoot2

  2 创建实体类

SpringBoot2

 3  写实体类映射文件

在resource下建Mapper包

SpringBoot2

SpringBoot2

<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhiyou100.mapper.IGoodsMapper">
    <resultMap id="goodsMap" type="com.zhiyou100.pojo.Goods">
        <id column="g_id" property="g_id"></id>
        <result property="g_name" column="g_name"></result>
        <result property="g_price" column="g_price"></result>
        <result property="g_type" column="g_type"></result>
        <result property="g_number" column="g_number"></result>
        <result property="g_mark" column="g_mark"></result>
    </resultMap>

    <!--查询所有-->
    <select id="queryAll" resultMap="goodsMap">
        SELECT * FROM tb_goods
    </select>

</mapper>

4 写Mapper类

SpringBoot2

SpringBoot2

5  写Service

SpringBoot2

SpringBoot2

报错的解决方案

SpringBoot2

6 写Controller

SpringBoot2

7 页面

注:01

SpringBoot2

02:引入约束

SpringBoot2

03:页面报错解决方法:

SpringBoot2

SpringBoot2

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table border="1px solid pink">
        <tr>
            <th>商品编号</th>
            <th>商品名</th>
            <th>商品价格</th>
            <th>商品类型</th>
            <th>商品数量</th>
            <th>品牌</th>
        </tr>
        <!--遍历tr-->
        <tr th:each="good:${goods_list}">
            <td th:text="${good.g_id}"></td>
            <td th:text="${good.g_name}"></td>
            <td th:text="${good.g_price}"></td>
            <td th:text="${good.g_type}"></td>
            <td th:text="${good.g_number}"></td>
            <td th:text="${good.g_mark}"></td>
        </tr>
    </table>
</body>
</html>

SpringBoot2

展示:

SpringBoot2

实例:

//建立视图
CREATE OR REPLACE VIEW oreder_goods_view
AS
SELECT o.o_id "订单号",o.u_id "用户id" ,o.o_price "订单价格",o.g_id "商品id",g.g_name "商品名",g.g_price "商品价格",g.g_type "商品类型",o.o_time "订单时间"
FROM tb_goods g,tb_order o
WHERE g.g_id=o.g_id
//WITH READ only;

SpringBoot2

四、补充

4.1、前后端分离

前端:页面,HTML,JS,CSS等

后端:逻辑代码,比如Controller,Service

项目部署:

前端和后端分别部署到不同的服务器上。

接口开发:

不是让你写Interface,企业中问你接口开发其实就是开发出一套后台功能,然后发布到服务器上,提供出去让别人使用。其实提供出去的就是一个Controller

4.2、springBoot配置文件

默认的位置是在resources下,不要修改,配置文件个是有两种

application.properties

application.yml

两种的作用是一样的,就是写法不同

比如配置tomcat端口号

Properties:

Server.port=8081

Server.path=/hello

Yml:

server:

    port: 8081

    path: /hello

上一篇:小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_23、SpringBoot2.x启动原理概述


下一篇:springboot2 kafka 配置