【Springboot】学习笔记

Springboot学习笔记

  • 使用jar包发布,不需要使用war包依赖于tomcat
  • 使用properties或者yml配置文件进行配置,默认扫描全局下的application
  • 使用注解风格开发,极简化代码

这里讲一点重要配置

  • springboot一般我们可以使用idea自动创建,或者登陆此网站下载springboot项目然后导入,也可以通过maven导入springboot依赖进行创建

 	<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.3.RELEASEversion>
        <relativePath/>
    parent>




	<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>


        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>



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

  • 热加载部署Springboot,这样可以有效的让我们测试代码,我们只需要修改springboot项目中的文件,springboot就会替我们自动重启项目

 	   <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>


 
	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>springloadedartifactId>
                        <version>1.2.8.RELEASEversion>
                        <scope>providedscope>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

关于热部署,idea用户需要在File-》setting-》build-》Compiler下勾选Build project automatically,应用后关闭。并通过Ctrl+Shift+Alit+/进入register然后勾选compiler.automake.allow.when.app.running即可开启热部署。

关于springboot的部署

  • springboot的部署主要有2种方式

    1. 通过命令行工具使用maven进行部署

      # 先要进入项目的pom.xml同级目录
      mvn spring-boot:run   #就可进行springboot的部署
      
    2. 通过jar包形式部署

      #springboot可以被导出成jar包,这里以courses-0.0.1-SNAPSHOT.jar为例
      java -jar courses-0.0.1-SNAPSHOT.jar #进行springboot的部署
      

springboot的重要注解

  1. @MapperScan用来指定扫描Mapper接口的包位置

  2. @ComponentScan用来指定扫描注解的包位置

  3. @SpringBootApplicationspringboot的启动类,代表全局应用

  4. @RestController类级别注解,修饰controller类,具有把响应参数转换为json字符的功能,相当于@Controller@ResponseBody两个注解的作用

  5. @EnableAutoConfiguration通常要进行某个模块的测试时,可以在那个类上用这个注解,具有和@SpringBootApplication相同的作用

  6. PropertySource用来加载指定的properties文件,可以同时加载多个

  7. @ConfigurationProperties用来加载application.properties或者application.yml全局配置文件的注解,可以指定perfix前缀,对象字段会根据name自动注入属性

  8. @Configuration类级别注解,用来指定当前类为spring的配置类,方法上用Bean注解,并返回一个对象,相当于spring的xml注入IOC,方法名为id

这里我贴出我平时过的最多的全局配置文件

server:
  port: 8080
  tomcat:
    uri-encoding: utf-8

mybatis:
  type-aliases-package: com.shaw.demo.courses.pojo
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名规则
    lazy-loading-enabled: true #开启延迟加载
    cache-enabled: true #开启二级缓存
    aggressive-lazy-loading: true #开启按需加载


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/course?useSSL=true
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: 1111
  thymeleaf: #不缓存页面 热加载刷新
    cache: false
  devtools:   #开启热加载
    restart:
      enabled: true
  output:
    ansi:
      enabled: always  #日记输出彩色
  mvc:
    view:
      prefix: /WEB-INF/pages/
      suffix: .html
logging:
  level:
    com.shaw.demo.courses.dao: debug  #log4j对持久层进行日记管理
  path: F:\\JAVA\\Course\\courses\\logs #log输出地址

你可能感兴趣的:(Spring)