springboot以前看了几次,因为没有跟着敲,没有笔记,就学了个锤子
做个笔记,不然又全都还给老师啦
开启一个springboot项目
两种方式
一.用maven开启
1.直接创建maven项目
2.在pom文件中导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencies>
创建一个主启动类
启动类也是个配置类,自动扫描其包下所有类,和配置文件的作用一样,不过是springboot自动完成
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class MyFirstSpringboot {
public static void main(String[] args) {
SpringApplication.run(MyFirstSpringboot.class,args);
}
}
在启动类下的包里创建测试类
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MySpringbootTest {
@RequestMapping("hello")
public String hello(){
return "你好呀,可以学好哒,会进步哒";
}
}
二.idea的springboot快速创建
其他步骤一一样
springboot亮点是自动配置,大部分配置springboot自动为你做好
springboot默认的配置文件为application.properties/application.yml
热部署
热部署,更好的调试项目,即不用重启项目,就可以进行更新,参考教程
yml
基本的语法
主要有字符串,带特殊字符的字符串,布尔类型,数值,集合,行内集合,行内对象,集合对象这几种常用的数据格式。
yaml:
str: 字符串可以不加引号
specialStr: "双引号直接输出\n特殊字符"
specialStr2: '单引号可以转义\n特殊字符'
flag: false
num: 666
Dnum: 88.88
list:
- one
- two
- three
set: [1,2,2,3]
map: {k1: v1, k2: v2}
positions:
- name: txjava
salary: 15000.00
- name: liangge
salary: 18888.88
@ConfigurationProperties可以将配置文件和实体类关联
实现值的注入
springboot与mybatis
第一步:引入依赖
<!-- 数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 数据库源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- mybatis启动相关依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
写数据库配置文件
application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/public_opinion?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 20011208
type: com.alibaba.druid.pool.DruidDataSource
initial-size: 5
minIdle: 5
因为springboot不会自动用外置数据源Druid的属性,所以我们要进行人工配置
@Configuration
public class JDBCConguion {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource getConnection(){
return new DruidDataSource();
}
}
mybatis增删改查
创建mapper文件
@Mapper
public interface PersonMapper {
@Select("select * from person")
public List<Person> getAllPerson();
@Select("select * from person where pid= #{id}")
public Person getPerson(int id);
// @Options(useGeneratedKeys = true,keyProperty = "pid")
@Insert("insert into person(pid,pname,addr,gender,birth)"+"values(#{pid},#{pname},#{addr},#{gender},#{birth})")
public void insertPerson(Person person);
}