1. 特点
1、项目依赖管理。
统一管理maven依赖,减少jar包冲突
2、对主流开发框架的无配置集成。
(redis、mq、mybatis、spring data jpa等)可根据所需自己选择
3、注解替代xml配置。
编写配置占用大量开发时间
2. 示例demo
2.0 代码: 【https://github.com/wangxingang0610/springboot-quick01.git】
2.1 idea创建maven工程
1、File — New — Project — Spring Initializr,选择自己的jdk,Next
2、自定义Group、Artifact、Package,完成Next
3、根据需要集成不同的框架(也可以创建project成功后,手动添加),完成则Next
4、Finish
5、这是可以看到创建好的项目(这时检查并完成maven配置)
2.2 controller编写
UserController.java
package com.study.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* RestController 兼有@Controller、@ResponseBody两者功能
*/
@RestController
public class UserController {
@RequestMapping("/list")
public String list(){
return "hello springboot";
}
}
SpringbootDemoApplication.java 创建工程时生成的启动类
package com.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringbootDemoApplication 启动类
*/
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
2.3 热部署配置(修改代码或配置后,无需重启应用,稍有延迟)
可参考:https://www.cnblogs.com/swuyan/p/11164111.html
1、添加依赖
<!--热部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
2、idea自动编译配置
Complier ----> 勾选“Build project automoatically”
shift + option + command + / (mac)
Ctrl + Shift + Alt + / (win)
Registry
勾选 “compiler.automake.allow.when.app.running”
修改controller代码,经测试热部署稍后延迟(14:19:57.709改完后启动时间 - 14:18:38.928上次结束的日志时间)
3. 配置文件(yml/yaml、properties)
1、文件优先级、加载顺序
2、(yml、properties)介绍及语法,(目前所在项目使用properties,个人感觉可读性好些,yml一旦层次太深的话不易读)
3.1 properties(不赘述)
key = value
3.2 yml/yaml
Open Document 打开链接
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
4. 配置文件与类属性映射
4.1 @value(使用较多)
UserController.java 添加代码
@Value("${com.config}")
private String config;
@RequestMapping("/method")
public String method(){
return "配置:" + config;
}
application.yml 添加配置值信息
com.config:
fdaaaaaaassssa
访问http://localhost:8080/method
4.2 @ConfigurationProperties(使用较多)
UserController.java
package com.study.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@ConfigurationProperties(prefix = "person")
public class UserController {
private String name;
private int age;
private boolean sex;
private Date birthday;
private Map<String, Object> location;
private String[] hobbie;
private List<String> skills;
@RequestMapping("/list")
public String list() {
return "list 7777 999 ss";
}
@RequestMapping("/say")
public String say() {
return "name=" + name + ", age=" + age + ", sex="+ sex + ",birthday=" +
birthday + ",location=" + location.toString() + ",hobbie=" + Arrays.toString(hobbie) + ",skills=" + skills;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map<String, Object> getLocation() {
return location;
}
public void setLocation(Map<String, Object> location) {
this.location = location;
}
public String[] getHobbie() {
return hobbie;
}
public void setHobbie(String[] hobbie) {
this.hobbie = hobbie;
}
public List<String> getSkills() {
return skills;
}
public void setSkills(List<String> skills) {
this.skills = skills;
}
}
application.yml
person:
age: 10
name: hello
birthday: 2010/10/1
sex: false
hobbie: ["aaa","bbb"]
skills:
aaa,
bbb,
ccc
location:
"k1": 111
"k2": "dfjakdjflas"
"k3": [1,2,3,4,5,6,7,8]
启动并访问
http://localhost:8080/say
5. springboot与其他技术整合
5.1 集成mybatis
5.1.1 添加依赖
<!--mybatis启动依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
5.1.2 配置信息
application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
5.1.3 数据库表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'zhangfei', 'zhangfei123', '张飞');
INSERT INTO `user` VALUES ('2', 'guanyu', 'guanyu123', '关羽');
5.1.4 实体类、mapper接口、mapper文件 (better-mybatis-generator插件生成代码)
使用better-mybatis-generator插件(idea安装该插件)生成代码,
具体说明: https://plugins.jetbrains.com/plugin/11021-better-mybatis-generator
UserMapper.java 头上需要加注解@Mapper(或@Resource)
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
5.1.5 单元测试
package com.study.mapper;
import com.study.SpringbootQuick01Application;
import com.study.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootQuick01Application.class)
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectByPrimaryKey() {
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user);
}
}
5.2 集成junit
上述已在用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
5.3 集成redis
5.3.1 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
5.3.2 配置redis连接
application.properties
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
5.3.3 redis操作
package com.study.redis;
import com.study.SpringbootQuick01Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootQuick01Application.class)
public class RedisTest {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void testAdd(){
}
}