仲灏小栈 仲灏小栈
首页
大前端
后端&运维
其他技术
生活
关于我
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

仲灏

诚意, 正心, 格物, 致知
首页
大前端
后端&运维
其他技术
生活
关于我
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Java

    • java初学者maven项目建立
    • Maven学习笔记
    • Untitled
    • idea 插件推荐
    • springboot项目学习笔记
    • springboot项目学习笔记2 用户模块
    • springboot项目学习笔记3 分类模块
    • springboot项目学习笔记4 商品模块
    • springboot项目学习笔记6 订单模块
    • 开源的物联网平台thingsboard部署
    • Docker 学习
    • java版本管理 jenv使用
    • mac中安装卸载java
    • springboot学习上手
      • 初始化项目
      • 数据库
      • 业务
      • 问题
  • Docker

  • Jenkins

  • Nacos

  • SQL

  • Nginx

  • Windows

  • Linux

  • 虚拟机

  • Git

  • 网络

  • 其他

  • 后端&运维
  • Java
仲灏
2022-04-09
目录

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

新增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

新增MySQL依赖

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
1
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

# 数据库

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

# 业务

新建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

新建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

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

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

# 问题

[ERROR] [ERROR] Some problems were encountered while processing the POMs:

包名不对 再仓库中引入不了 解决方法:

  • 确认包名
  • 修改mirror
  • 手动引入
上次更新: 2022/06/05, 20:31:36
mac中安装卸载java
Docker 学习

← mac中安装卸载java Docker 学习→

最近更新
01
vim日常使用记录
04-02
02
滑动窗口最大值
04-02
03
有效的字母异位词
04-02
更多文章>
Theme by Vdoing | Copyright © 2021-2025 izhaong | github | 蜀ICP备2021031194号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式