欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot总结

程序员文章站 2024-03-21 23:11:04
...

1 环境变量配置
1.1 JDK安装步骤
1. 找到Jdk的安装路径D:\xxx\xxx\jdk1.8.0_45\bin
2. 配置JAVA_HOME: D:\xxx\xxx\jdk1.8.0_45
3. 配置classpath, %JAVA_HOME%\bin
1.2 检查安装是否正确
java -version (出现版本号,引擎)
2 新的工作空间设置:
1 为了避免编译时耗费大量时间,关闭STS的Validation校验
2 修改新空间的字符集编码 GBK --> UTF-8
3 配置Maven
3.1 设置下载jar包时获取源码
3.2 安装新的maven
3.3 Setting 设置maven的配置文件
3.4 注意检查jdk的配置是否正确(可以使用自己安装的或者STS自己默认自带的)
SpringBoot
pom.xml说明: 坐标作用 讲项目打成 jar/war 为项目的依赖提供了支持
jar 多用于工具项目或通用项目
war 多用于web项目 项目中有页面需要展示
配置文件proprties的说明:
YML配置是有层级的,注意缩进
YML配置中key/value中间必须添加": “空格
YML配置的名称必须是application.yml
SpringBoot中属性赋值方式(三种)
1 @value赋值
先在配置文件中指定相关的key/value
当程序中使用相关属性进行赋值时通过@value(”xxxx.xxx")21pom.xml2key/value3@ConfigurationProperties(prefix="xxx")3.1Jsonget(),Jsonset()3yml,,,properties1key/value2@propertySource(value="classpath:/prooerties/xxxx.properties")@value("{xxxx.xxx}")赋值 2 批量赋值 1 需要在pom.xml中添加依赖 2 在配置文件中定义相关属性 key/value 3 在需要赋值的类上@ConfigurationProperties(prefix="xxx") 即可完成相关赋值 3.1 因为对象与Json转换需要get()方法,Json转换对象需要set()方法 所以需要添加相关方法 3 指定配置文件为属性赋值 因为yml是核心配置文件,一般用来整合第三方框架 ,写入属性赋值是的文件冗余,可读性差 一般采用 引入properties文件赋值 1 定义相关文件 key/value 2 在需要赋值的类上@propertySource(value="classpath:/prooerties/xxxx.properties")引入文件 在需要赋值的属性上添加@value("{xxxx.xxxx}")
YML切换环境:
" — " 用来分割配置环境
spring:
profiles:
acive: prod

spring:
profiles: dev
xxxxx

spring:
profiles: prod
xxxxx


SpringBoot整合mybatis:
1 在pom.xml中添加mybatis依赖

mysql
mysql-connector-java
runtime

	<!--引入druid数据源 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.12</version>
	</dependency>

	<!--spring整合mybatis 暂时 -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>1.3.2</version>
	</dependency>

2 配置YML文件:
server:
port: 8090
servlet:
context-path: /
spring:
datasource:
#引入druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#如果数据库密码以0开头 使用:""包裹

mybatis:
#定义别名包 简化查询resultType配置
type-aliases-package: com.jt.pojo
#Mybatis的映射文件路径
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射 字段 属性 对应 去除字段中的"_"之后首字母大写
configuration:
map-underscore-to-camel-case: true