Spring Boot万能模板
pom配置文件
最简单的配置
pom.xml
1.添加springboot 的parent起步依赖
2.添加springboot web功能的起步依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<groupId>com.an</groupId>
<artifactId>springboot_quick</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
最快速的配置
直接用IDEA创建工程的时候选择Spring Boot
启动类
最简单的启动类
新建MySpringBootApplication.java
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
热部署配置
添加依赖
<!--热部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
修改IDEA的配置
1.打开preference找到complier选择上build project automaticlly
2.Shift+Ctrl+Alt+/,选择Registry 将complier.xxx.xxx.app.running勾选上
Controller
最简单的Controller
新建QuickController.java
@Controller
public class QuickController {
@RequestMapping("/quick")
@ResponseBody
public String Quick() {
return "Hello Spring Boot";
}
}
SpringBoot配置文件
applicaiton.properties
#服务器端口
server.port=8888
#当前web应用的名称
server.servlet.context-path=/demo
application.yml
书写配置文件的语法
配置普通数据
语法: key: value
示例代码:
```yaml
name: haohao- 注意:value之前有一个空格 ##### 配置对象数据 - 语法: key: key1: value1 key2: value2 或者: key: {key1: value1,key2: value2} - 示例代码: - ```yaml person: name: haohao age: 31 addr: beijing #或者 person: {name: haohao,age: 31,addr: beijing}
注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别
配置Map数据
同上面的对象写法
配置数组(List、Set)数据
语法:
key:
- value1
- value2
或者:
key: [value1,value2]
示例代码:
```yaml
city:- beijing
- tianjin
- shanghai
- chongqing
#或者
city: [beijing,tianjin,shanghai,chongqing]
#集合中的元素是对象形式
student:- name: zhangsan
age: 18
score: 100 - name: lisi
age: 28
score: 88 - name: wangwu
age: 38
score: 90
name: anbingxu- 注意:value1与之间的 - 之间存在一个空格 2. 获取配置文件数据的方法 配置如下:
person:
name: jack
age: 18获取方法1 @Value注解: ```java @RestController public class GetValueController { @Value("${name}") private String name; @Value("${person.name}") private String personName; @Value("${person.age}") private String personAge; @RequestMapping("/name") public String whatName() { return "name-->" + name; } @RequestMapping("/person") public String whatPerson() { return "name-->" + personName + "\n" + "age-->" + personAge; } }
获取方法2: @ConfigurationProperties(prefix=”配置文件中的key的前缀”):
导入包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
写属性+getter+setter
@RestController @ConfigurationProperties(prefix = "person") public class GetValueController2 { private String name; private String age; private String love; public String getLove() { return love; } public void setLove(String love) { this.love = love; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @RequestMapping("/person2") public String whatPerson() { return "name-->" + name + "\n" + "age-->" + age; } }
整合
mybatis
1.导入mysql driver
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
2.导入mybatis的起步依赖
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
修改application.properties
#数据源配置 spring.datasource.username=root spring.datasource.password=mysql123 spring.datasource.url=jdbc:mysql://localhost:3306/vuejsDemo?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #spring集成Mybatis环境 #pojo别名扫描包 mybatis.type-aliases-package=com.an.domain #加载Mybatis映射文件 mybatis.mapper-locations=classpath:mapper/*Mapper.xml
创建映射实体类(注意添加getter setter)
创建mapper/*Mapper.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.an.mapper.UserMapper"> <select id="queryUserList" resultType="user"> select * from user </select> </mapper>
创建mapper接口 com.an.mapper.*Mapper.java
import com.an.domain.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * @Author: bxan * @Date: 2020/2/14 14:54 * @Description: */ @Mapper public interface UserMapper { public List<User> queryUserList(); }
创建controller使用
@RestController public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("query") public List<User> queryAll() { return userMapper.queryUserList(); } }
Junit
导包
<!--测试的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
编写测试类
下面的两个注解必不可少!!!
@RunWith(SpringRunner.class) @SpringBootTest(classes = MySpringBootApplication.class) public class MyBatisTest { @Autowired private UserMapper userMapper; @Test public void testqueryAll() { List<User> users = userMapper.queryUserList(); System.out.println(users); } }
redis
引入依赖
<!-- 配置使用redis启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
修改配置
#Redis spring.redis.host=127.0.0.1 spring.redis.port=6379
简单的使用方式
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootJpaApplication.class) public class RedisTest { @Autowired private RedisTemplate<String, String> redisTemplate;//自动注入模板 @Test public void testReds() { //得到 BoundValueOperations对象 BoundValueOperations<String, String> a = redisTemplate.boundValueOps("a"); //打印 key System.out.println(a.getKey()); //打印 value System.out.println(a.get()); } }