springboot学习上手
# 初始化项目
https://start.spring.io/
https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.10&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=spring-boot-learn&name=spring-boot-learn&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.spring-boot-learn&dependencies=web
修改springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
1
2
3
4
5
6
2
3
4
5
6
新增mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
1
2
3
4
5
2
3
4
5
新增MySQL依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
1
2
3
4
2
3
4
配置数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=mysql8password
spring.datasource.url=jdbc:mysql://localhost:3306/springbootlearn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
1
2
3
4
2
3
4
# 数据库
CREATE TABLE students (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增id',
`name` VARCHAR(250) NOT NULL DEFAULT '' COMMENT 'name',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
1
2
3
4
5
2
3
4
5
# 业务
新建Student实体类, 即对应数据库中某个表
package com.example.springbootlearn;
/**
* 描述: 学生实体类
*/
public class Student {
Integer id;
String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
新建mapper
package com.example.springbootlearn;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface StudentMapper {
@Select("select * from students where id = #{id}")
Student findById(Integer id);
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
service
package com.example.springbootlearn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
public Student findStudent(Integer id) {
return studentMapper.findById(id);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
controller
package com.example.springbootlearn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@GetMapping({"/student"})
public String student(@RequestParam Integer id) {
Student student = studentService.findStudent(id);
return student.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 问题
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
包名不对 再仓库中引入不了 解决方法:
- 确认包名
- 修改mirror
- 手动引入
上次更新: 2022/06/05, 20:31:36