天天看点

SpringBoot入门及thymeleaf的使用

SpringBoot

采用了习惯优于配置概念快速搭建开发环境

整合ssm

springBoot在远程服务器创建

SpringBoot入门及thymeleaf的使用

SpringBoot目录结构

applicationspringBoot启动类

resources

​ static:存放在此目录的资源文件不会被拦截

​ templates:模板目录;存放项目中动态模板,例如 jsp、thymeleaf

​ application.properties :配置文件 :此文件对springBoot的默认配置修改。

​ 默认扫描目录:application同目录的都会扫描

SpringBoot入门及thymeleaf的使用

配置mybatis数据源

在resources/application.properties文件进行数据库连接信息配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_2111?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:mappers/ *Mapper.xml
mybatis.type-aliases-package=com.qfedu.springboot.demo1.beans
           

启动类

添加

@MapperScan

注解声明mybatis数据库操作存放DAO接口的包的路径

//扫描Dao包
@SpringBootApplication
@MapperScan("com.zdq.springboot.demo1.dao")
public class SpringbootDemo1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemo1Application.class, args);
    }

}
           

application

@SpringBootApplication
@MapperScan("com.zdq.demo4.Dao")
public class Demo4Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo4Application.class, args);
    }

}

           
SpringBoot入门及thymeleaf的使用

自定义starter 步骤

依赖;jar等

配置类:@configuration @bean

META-INF/spring.factories文件----声明配置类的路径

springBoot项目结构

SpringBoot入门及thymeleaf的使用

SpringBoot的pom文件

  • 聚合工程: springBoot官方创建的项目
  • 在maven中引入就是引别人的项目

    -maven的继承就是pom文件的继承

  • 对于parent引入的pom文件;加了父文件加了的文件,子文件无法引入;此文件只为一个模板;
  • 不加的文件,子文件会直接引入
  • 而在父文件;定义了plugins的依赖;在对很多依赖都进行了版本定义
  • 在子项目pom配置后更改版本需要用进行指定
  • spring自动配置通过加载配置类;满足条件则对配置类进行初始化来完成的。
  • spring2.x企业开发到底是用xml配置还是注解配置?
  • 对于基础配置、引入第三方依赖配置需要用xml配置;对于项目业务开发(控制器类、业务类)使用注解
  • Spring3.x:开始支持java类的配置(注解)
  • @configuration注解类 :识别为配置类
  • xml、注解、java配置
  • java配置 的好处
    SpringBoot入门及thymeleaf的使用
    spring自动整合@Bean 的文件 作为父类被引用时就可以直接导入,不需要用xml等方式

SpringBoot全局配置文件

  • 每一个springboot创建之后都会默认在resources创建一个 application.properteis文件;此文件就是springboot应用的全局配置文件
  • 此文件用于开发者进行自定义配置;以及对springbot默认配置的修改
  • 此全局文件支持两种语法配置
  • 1:属性文件键值对key=value 2:yaml语法
  • yaml配置:1:重命名为application.yaml 2:
    SpringBoot入门及thymeleaf的使用
    SpringBoot入门及thymeleaf的使用

SpringBoot整合Druid连接池

  • 在SpringBoot整合Mybatis时;默认集成Hikari连接池。
  • SpringBoot需要从外部导入Druid的strat
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
      username: root
      password: root
      min-idle: 3
      max-active: 20
      max-wait: 30000
      initial-size: 1
           
SpringBoot所有的配置都是java类;没有xml文件了
           

- Static文件的资源可以直接访问

  • 直接访问HTML文件;不能访问jsp页面
    SpringBoot入门及thymeleaf的使用
    jsp页面浏览器不识别;将jsp转换为java再通过http流输出到前端页面

SpringBoot自动配置过程

SpringBoot入门及thymeleaf的使用

springBoot的pom文件

SpringBoot入门及thymeleaf的使用

SpringBoot整合jsp

  • springBoot默认不支持jsp动态网页技术
  • SpringBoot集成网页技术:Thymeleaf
  • 整合:1:添加依赖 2:main下创建webapp 和打包方式;3:在webapp创建jsp页面;springBoot应用中的jsp不能被直接识别;必须通过控制器访问。
  • :写一个controller,让controller转发到jsp页面;同时要导入jar包。
  • <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.58</version> </dependency>

    -

    javax.servlet

    jstl

    1.2

  • 通过controller转发

@Controller

public class PageController {

@RequestMapping("/index.html")

public String test(){

return “/index”;

}

}

  • 更改访问路径
    SpringBoot入门及thymeleaf的使用

Thymeleaf动态网页

  • jsp必须用tomcat环境运行;不适合springboot开发
  • BookController --List -->book-list.html ??
  • 引入:既有html后缀 ,能用浏览器直接访问又有类似jsp动态显示的网页技术
  • 特点:可以直接在浏览器查看样式
  • 又能应用于服务器动态展示数据
  • 引入包
    SpringBoot入门及thymeleaf的使用

    创建thymeleaf模板

    在templates目录下新建html文件

<!DOCTYPE html>
<html lang="en" **xmlns:th="http://www.thymeleaf.org"**>
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>

    </body>
</html>
           

主要步骤就是加了一句xmlns

  • thymeleaf接收数据的方式:
<tr th:each="book:${bookList}">
                   <td th:text="${book.bookId}">1</td>
                   <td th:text="${book.bookName}">Java</td>
                   <td th:text="${book.bookAuthor}">张三</td>
                   <td th:text="${book.bookPrice}">1.1</td>
                   <td th:text="${book.bookImg}">a.jpg</td>
                   <td th:text="${book.bookDate}">2022-02-23</td>
                   <td th:text="${book.bookDesc}">haha</td>
                   <td>
                       <a href="" class="btn btn-xs btn-danger">删除</a>
                       <a href="" class="btn btn-xs btn-warning">修改</a>
                   </td>
               </tr>
           

Controller接收方式:

@Controller
public class IndexController {

    @RequestMapping("/index")

    public String MainHtml(){
        return "index";
    }
}
html都存放到templates才能被thymeleaf识别
           

项目BUG:

SpringBoot入门及thymeleaf的使用

thymeleaf项目BUG:

<script type="text/javascript" src="../demo/js/jquery-3.6.0.min.js"></script>
jquery报错:加入../就OK了   ../ 上级目录  项目配置默认/demo访问路径之后;不返回上级目录就会有两个/demo
           

图片上传的Controller;SSM需要引入两个jar

  • @RequestBody :将java对象转换为json发送 给前端ajax
//文件上传 commonsio commonsfileupload
@Controller
@RequestMapping("/file")
public class FileController {
//将java转换为json对象
    @ResponseBody
    @RequestMapping("/upload")
//    MultipartFile 多段提交 bookImgFile 与前端id一致
    public Map<String, String> upload(MultipartFile bookImgFile, HttpServletRequest request) throws IOException {
        //文件保存
        String originalFilename = bookImgFile.getOriginalFilename();//文件全名
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));  //.jpg
        String fileName = UUID.randomUUID().toString().replace("-","")+ext;//存到数据库的随机名字

        String dir = request.getServletContext().getRealPath("images");  // d:/aaa/.../images/通过request获取目录
        bookImgFile.transferTo( new File(dir+"/"+fileName));//图片保存到服务器

        Map<String, String> map = new HashMap<String, String>();//用map构造一个json格式
        map.put("code","200");
        map.put("filePath","images/"+fileName);//存到服务器路径
        return map;//路径给前端
    }

}
           

SpringBoot项目:pom引入两个start

SpringBoot入门及thymeleaf的使用
@Controller
public class FileUpload {
    @ResponseBody
    @RequestMapping("/upload")
    public Map<String,String> upload(MultipartFile bookImgFile, HttpServletRequest request) throws IOException {
        System.out.println("进入图片上传页面");
        String originalFilename = bookImgFile.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName= UUID.randomUUID().toString().replace("-","")+ext;
        String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
        //获取到static文件夹的绝对路径
        System.out.println(path);
        String filepath=path+"/images";
//        String dir = request.getServletContext().getRealPath(path+"/images");
        System.out.println(filepath);
        bookImgFile.transferTo(new File(filepath+"/"+fileName));
        //将接收到的img文件存到项目部署目录下的images目录
        Map<String ,String> map = new HashMap<String,String>();
        map.put("code","200");
        map.put("filePath","images/"+fileName);
        return map;//将路径返回给前端ajax
    }
}
           
  • thymeleaf引入使用标签