<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
激活热部署:Ctrl + F9
关于热部署
默认不触发重启的目录列表
自定义不参与重启排除项
devtools:
restart:
exclude: public/**,static/**
@SpringBootApplication
public class SsmpApplication {
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled","false");
SpringApplication.run(SsmpApplication.class);
}
}
@Bean
@ConfigurationProperties(prefix = "datasources")
public DruidDataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
datasources:
driverClassName: com.mysql.cj.jdbc.Driver
@SpringBootApplication
@EnableConfigurationProperties(serverConfig.class)
public class Springboot0701Application {
@Bean
@ConfigurationProperties(prefix = "datasources")
public DruidDataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Springboot0701Application.class, args);
DruidDataSource dataSource= run.getBean(DruidDataSource.class);
System.out.println(dataSource.getDriverClassName());
serverConfig serverConfig = run.getBean(serverConfig.class);
System.out.println(serverConfig.toString());
}
}
//@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class serverConfig {
private String ipAddress;
private int port;
private long timeout;
}
注意事项
@EnableConfigurationProperties和@Component不能同时使用
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
dependency>
public class serverConfig {
private String ipAddress;
private int port;
private long timeout;
}
ip-Address: 189.176.0.1
ip_Address: 189.176.0.1
ipaddress: 189.176.0.1
IPADDRESS: 189.176.0.1 # 常量模式
ip-address: 189.176.0.1 # 烤肉串模式
注意事项
宽松绑定不支持注解@Value引用单个属性的方式
@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class serverConfig {
private String ipAddress;
private int port;
private long timeout;
@DurationUnit(ChronoUnit.HOURS)
private Duration serverTimeout;
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize dataSize;
}
<dependency>
<groupId>javax.validationgroupId>
<artifactId>validation-apiartifactId>
dependency>
<dependency>
<groupId>org.hibernate.validatorgroupId>
<artifactId>hibernate-validatorartifactId>
dependency>
@Component
@Data
@ConfigurationProperties(prefix = "servers")
//2. 开启对当前bean的属性注入校验
@Validated
public class serverConfig {
}
@Component
@Data
@ConfigurationProperties(prefix = "servers")
//2. 开启对当前bean的属性注入校验
@Validated
public class serverConfig {
private String ipAddress;
//3.设置具体的规则
@Max(value = 8888,message = "最大值不能超过8888")
@Min(value = 202,message = "最小值不能小于202")
private int port;
private long timeout;
@DurationUnit(ChronoUnit.HOURS)
private Duration serverTimeout;
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize dataSize;
}
int: 0123 # 会判断为八进制的123导致转换出现问题
注意yaml文件中对于数字的定义支持进制书写格式,如需使用字符串请使用引号明确标注
string: "0123"
//properties属性可以为当前测试用例添加临时的属性配置
@SpringBootTest(properties = {"test.prop=testValue2"})
public class propertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
//args属性可以为当前测试用例添加临时的命令行参数
SpringBootTest(args = {"--test.prop=testValue3"})
public class propertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
@SpringBootTest
@Import({MsgConfig.class})
public class ConfigTest {
@Autowired
private String msg;
@Test
void test1(){
System.out.println(msg);
}
}
package com.smulll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class Springboot0801ApplicationTests {
@Test
void contextLoads() {
}
}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
class Springboot0801ApplicationTests {
@Test
void testWeb(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行请求
ResultActions perform = mvc.perform(builder);
}
}
@Test
void testStatus(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行请求
ResultActions perform = mvc.perform(builder);
//设置预期值与真实值进行比较,测试成功通过,失败测试停止
//定义本次调用的预期值
StatusResultMatchers status = MockMvcResultMatchers.status();
//预计本次调用的成功值,状态200
ResultMatcher ok = status.isOk();
//添加预计值到本次调用过程中进行匹配
perform.andExpect(ok);
}
@Test
void testBody(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行请求
ResultActions perform = mvc.perform(builder);
//设置预期值与真实值进行比较,测试成功通过,失败测试停止
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用执行结果
ResultMatcher result = content.string("springboot");
//添加预计值到本次调用过程中进行匹配
perform.andExpect(result);
}
@Test
void testJSON(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行请求
ResultActions perform = mvc.perform(builder);
//设置预期值与真实值进行比较,测试成功通过,失败测试停止
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用执行结果
ResultMatcher result = content.json("{\n" +
" \"id\": 1,\n" +
" \"type\": \"springboot\",\n" +
" \"name\": \"springboot\",\n" +
" \"description\": \"springboot\"\n" +
"}");
//添加预计值到本次调用过程中进行匹配
perform.andExpect(result);
}
@Test
void testContentType(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行请求
ResultActions perform = mvc.perform(builder);
//设置预期值与真实值进行比较,测试成功通过,失败测试停止
//定义本次调用的预期值
HeaderResultMatchers Header = MockMvcResultMatchers.header();
//预计本次调用执行结果
ResultMatcher string = Header.string("Content-Type", "application/json");
//添加预计值到本次调用过程中进行匹配
perform.andExpect(string);
}
@SpringBootTest
@Transactional
public class TestSave {
@Autowired
private bookServer bookServer;
}
@SpringBootTest
@Transactional
@Rollback(false)
public class TestSave {
}
testcase:
book:
id: ${random.int}
name: ${random.value} #随机字符串,MDS字符串,32位
type: ${random.int(10,100)}
uuid: ${random.uuid} #随机uuid
publishTime: ${random.long}
${random.int}
表示随机整数${random.int(10)}
表示10以内的随机数${random.int(10,20)}
表示10到20的随机数()
可以是任意字符,例如[]
,!!
均可Druid + MyBatis-Plus + MySQL
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springbootbase?serverTimezone=UTC
username: root
password: 123456
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springbootbase?serverTimezone=UTC
username: root
password: 123456
hikari:
maximum-pool-size: 50
@SpringBootTest
class Springboot0901ApplicationTests {
@Test
void testJdbc(){
String sql = "select * from tb_book where id=1";
List<Book> list = jdbcTemplate.query(sql, new RowMapper<Book>() {
@Override
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setType(rs.getString("type"));
book.setDescription(rs.getString("description"));
return book;
}
});
System.out.println(list);
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
spring:
jdbc:
template:
query-timeout: -1 #查询超时时间
max-rows: 500 #最大行数
fetch-size: -1 #缓存行数
springBoot提供了3种内嵌数据库供开发者选择,提高开发测试效率
导入H2相关坐标
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<scope>runtimescope>
dependency>
server:
port: 80
spring:
h2:
console:
path: /h2
enabled: true
server:
port: 80
spring:
h2:
console:
path: /h2
enabled: true
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:~/test
username: sa
password: 123456
server:
port: 80
spring:
h2:
console:
path: /h2
enabled: false
Redis是一款key-value存储结构的内存级NoSQL数据库
redis下载
redis下载(Windows)
安装启动
redis-server.exe redis.windows.conf
redis-cli.exe
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
spring:
data:
redis:
host: localhost
port: 6379
@SpringBootTest
class Springboot10RedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void set() {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("age",19);
}
@Test
void get(){
ValueOperations valueOperations = redisTemplate.opsForValue();
Object o = valueOperations.get("age");
System.out.println(o);
}
}
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void Demo1(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
System.out.println(ops.get("name"));
}
}
spring:
data:
redis:
host: localhost
port: 6379
client-type: jedis
spring:
data:
redis:
host: localhost
port: 6379
client-type: jedis
lettuce:
pool:
max-active: 16
jedis:
pool:
max-active: 16
mongod --dbpath=..\data\db
mongo --host=127.0.8.1 --port=27017