赞
踩
导入依赖
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
使用maven的package进行打包

运行jar包,从项目文件夹进入cmd,运行命令 java -jar xx.jar
3种: application.properties application.yml bootstrap.yml(优先级最高,多用于中心化配置)
读取yml
user:
username: root
password: 123
使用@Value注解
@Value("${user.username}")
private String username;
使用对象接收
将对象交给spring管理(@component),使用@ConfigurationProperties(prefix=“”),然后自动注入获取值
@Component
@ConfigurationProperties(prefix = "user")
@Data
@AllArgsConstructor
@NoArgsConstructor
/**
* 读取配置文件参数方式二: 使用对象接收,需要有构造方法和get,set方法
* 将对象交给spring管理(@component),使用@ConfigurationProperties(prefix="") 然后自动注入获取值
*/
public class User {
private String username;
private String password;
private List<String> hobby;
}
读取properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql///young
jdbc.username=root
jdbc.password=root
使用对象接收: 使用PropertySource和ConfigurationPropertis注解,使用@Autowired注入值
@Component
@Data
@PropertySource(value = "classpath:jdbc.properties")
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperty {
private String driver;
private String url;
private String username;
private String password;
}
3.读取xml文件
新建类,使用@ConfigurationPropertis(prefix=“”),@ImportResource(“classpath:application-context.xml”)注解
在一个文件中配置
多环境之间用 - - - 分隔,使用active激活指定环境
spring: profiles: active: test #激活环境 #多环境之间用 --- 分隔 #开发环境 dev --- server: port: 8081 spring: profiles: dev #环境名称 application: name: spring-boot-dev #测试环境 test --- server: port: 8082 spring: profiles: test application: name: spring-boot-test
使用多文件配置
application.yml
spring:
profiles:
active: dev #激活环境
application-dev.yml
server:
port: 8081
spring:
profiles: dev #环境名称
application:
name: spring-boot-dev
application-test.yml
#测试环境 test
server:
port: 8082
spring:
profiles: test
application:
name: spring-boot-test
-Dspring.profiles.active=dev
java -jar xx.jar --spring.profiles.active=prod
springboot2.4及之前,spring-boot-starter-test中是Junit4
测试类上注解:@RunWith(SpringRunner.class),@SpringBootTest(classes = 启动类.class)
springboot2.4之后,为Junit5
注解为:@ExtendWith(SpringExtension.class),@SpringBootTest(classes = 启动类…class)
包含了@Setter @Getter @EqualsAndHashCode @ToString
日志注解,使用 log.info(“info打印”);
thymeleaf是springboot提供的前端页面模板技术,其实是templates下的html,需要引入头文件,用th:text=${}获取值
<!DOCTYPE html>
<!--引入头文件-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>接收数据</title>
</head>
<body>
<!--使用thymeleaf语法 th:text=${} 将div中的innerText为获取的值:${}-->
<div th:text="${msg}">显示默认数据</div>
</body>
</html>
springboot默认配置了视图解析器 前缀 /templates/ 后缀 .html
可以在yml中写配置并在ThymeleafProperties中配合修改
编写类实现HandlerInterceptor接口,实现对应的方法
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(request.getRequestURI()+" 通过了拦截器检查...");
return true;
}
}
写配置类(@Configuration)实现WebMvcConfigurer接口,并重写addInterceptors方法
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private MyInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor)
.addPathPatterns("/**")
.excludePathPatterns("/login", "/logout");
}
}
导包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///young
username: root
password: root
Mapper映射文件路径和别名配置
mybatis:
mapper-locations: classpath:com/zhc/mapper/*Mapper.xml
type-aliases-package: com.zhc.domain
编写Mapper和Service(@Service)
启动类添加映射器扫描注解 @MapperScan(“com.zhc.mapper”)
@SpringBootApplication
@MapperScan("com.zhc.mapper")
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class, args);
}
}

别名配置,多个包路径之间有,隔开
mybatis:
# Mapper.java 和 Mapper.xml在相同对应的包下(编译之后在同一个包),可以不配置
# mapper-locations: classpath:com/zhc/mapper/*Mapper.xml
type-aliases-package: com.zhc.auth.server.domain,com.zhc.org.server.domain
MapperScan中的不同包用 * 表示,一个 * 表示一个层级
@MapperScan("com.zhc.*.*.mapper")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。