springboot基础笔记

springboot简介

springboot的目的是用来简化spring应用的初始搭建和开发过程。该框架使用了特定的方式配置,使得开发人员不再需要定义样板化的配置。

springboot(微框架)= springmvc(控制器)+spring(项目管理)

特点
1.创建独立的spring应用程序
2.嵌入的tomcat,无需部署warwenjian
3.简化maven配置
4.自动配置spring,没有xml配置

项目目录结构
在这里插入图片描述

关于程序的一些注释等的理解


@EnableAutoConfiguration //作用:开启自动配置 初始化spring环境 springmvc环境
@ComponentScan //作用:用来扫描注解(controll里面的注解) 范围:入口类的包以及子包
public class Application {

    public static void main(String[] args) {

        //springApplication是spring的应用类, 作用:用来启动springboot的应用
        //参数1:传入入口类对象  参数2:main函数的参数
        SpringApplication.run(Application.class,args);
    }


    /**
     *     @EnableAutoConfiguration  作用:开启自动配置   修饰范围:只能用在类上
     *          实际作用:根据pom.xml文件引入的依赖自动判断进行环境搭建
     *
     *          例:引入了spring-boot-starter-web 会自动根据引入的这个依赖构建相应的环境 springmvc环境 web容器环境
     *
     *
     *      @ComponentScan  作用:用来开启注解扫描  修饰范围:只能作用在类上
            范围:当前包以及子包

            @RestControll  作用:用来实例化当前对象为一个控制器对象,并将类上所有方法的返回值转化为json,响应给浏览器  修饰范围:只能在类上

                @Restcontroll = @Controll (实例化当前类为一个控制器)+ @ResponseBody(将当前类上所有方法的返回值转化为json,响应给浏览器)


            @RequestMapping:作用:用来加入访问路径,  修饰范围:类(加入命名空间)和方法(指定具体路径)
                @GEtMapping: 作用:限定请求方式只能是get,并指定路径   修饰范围:方法上
                @PostMapping,@DeleteMapping ,@PutMapping


     *      main方法的作用:通过java标准入口方式委托给SpringApplication,并告知springboot主应用类是谁,从而启动springboot中tomcat容器
     *
     *      args作用:可以启动时传入外部参数
     *
     *
     *
     */


}

配置文件中内容的信息

server.port=8989//定义项目开始的端口
server.servlet.context-path=/springboot_day6  //定义项目的根路径

spring.thymeleaf.encoding=UTF-8		//定义thymeleaf编码格式
spring.thymeleaf.suffix=.html		//定义thymeleaf查找文件的后缀
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.prefix=classpath:/templates/  //定义thymeleaf网页文件的根路径
spring.thymeleaf.cache=false  
spring.resources.static-locations=classpath:/templates/,classpath:/static/ //定义thymeleaf既可以通过controller打开动态页面,也可以直接打开静态页面



spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/ems?useSSL=false&useUnicode=false&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=131556

logging.level.root=INFO
logging.level.com.feng.dao=debug
logging.level.com.feng.Controller=debug


mybatis.mapper-locations=classpath:/com/feng/mapper/*.xml //使用mybatis时mapper的路径
mybatis.type-aliases-package=com.feng.entity  //给实体类取别名

thymeleaf的入门语法

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">  //注意
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/css/index.css}"/>//注意
</head>
<body>
    <h1>this is thymeleaf!</h1>


    <h1> 展示单个数据</h1>

    欢迎:<span th:text="${name}"/>//注意
    <span th:utext="${username}"/>
    <input type="text" value="${username}"/>


        <h2>展示对象数据</h2>
    <ul>
        <li>id:<span th:text="${user.id}"/> </li>
        <li>name:<span th:text=sSWSWZ${user.name}"/> </li>
        <li>age:<span th:text="${user.age}"/> </li>
        <li>id:<span th:text="${#dates.format(user.bir,'yyyy-MM-dd')}"/> </li>
    </ul>

    <h1>有条件的展示数据</h1>
    <span th:if="${user.age} le 23" th:text="${user.name}"></span>


    <h1>展示多个数据</h1>
    <ul th:each="user:${users}">
        <li>id:<span th:text="${user.id}"/> </li>
        <li>name:<span th:text="${user.name}"/> </li>
        <li>age:<span th:text="${user.age}"/> </li>
        <li>id:<span th:text="${#dates.format(user.bir,'yyyy-MM-dd')}"/> </li>

    </ul>

    <h1>测试连接路径</h1>
    <a th:href="@{/user/delete(id=123,name='小小')}">删除记录</a>

</body>
</html>

关于写项目的心得

1.postmapping和getmapping。一般网页上写数据即使用表单,提交方法时post则写postmapping。其他一般是getmapping。

2.写控制器时,需要写一个不需要条件就可以跳转到页面的控制器,其他控制器则需要条件的写。

3.一定要把依赖写好,有时候运行不出来,可能是某些版本高了,需要降低
4.配置文件需要写好

5.thymeleaf页面需要注意格式

6.写项目时maties用到的mapper需要注意参数

spring boot中的devtools热部署

热部署是jvm虚拟器中开启两个类加载器,一般开始是使用类加载器 1,当发生变化时,类加载器2会编译代码,类加载器2开始替换1.如此反复。

开启热部署可以让你不需要重启项目,网页上也可以完成更新。一般小项目不需要开启。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值