SpringBoot笔记
0.回顾SSM
1.臃肿的依赖配置
2.添加web组件
3.繁琐的配置文件
1.SpringBoot快速开发
####1.1创建SpringBoot的两种方式
#####1.1.1官方网站创建SpringBoot项目
#####1.1.2IDEA创建SpringBoot项目
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KMbVcMk2-1573126956750)(/1573112259010.png)]
####1.2如何理解Parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
这是springboot的父工程,它规定了许多依赖的版本
####1.3项目结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4fen4GEC-1573126956753)(/1573112852631.png)]
application.properties是全局配置文件,这个文件的名字必须以application开头
全局配置文件扩展名也可以是
yaml
或yml
文件,只是里面的写法需要改变例如:
server.port=8888
server: port: 8888
启动类
package org.neuedu.springboot01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Springboot01Application { public static void main(String[] args) { SpringApplication.run(Springboot01Application.class, args); } }
@SpringBootApplication
该注解可以扫描当前路径及其所有子包下的所有注解
####1.4创建Controller测试接口
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KubZ9o2u-1573126956755)(/1573113728677.png)]
package org.neuedu.springboot01.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello SpringBoot!";
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TpDRzYpF-1573126956756)(/1573113762708.png)]
OK!!!
1.5整合Mybatis、JDBC、连接池
引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
配置文件
# 端口号
server.port=8888
# 数据库参数
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis02?useSSL=true&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
启动类上添加mapper扫描
package org.neuedu.springboot01;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "org.neuedu.springboot01.mapper")
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}