Skip to content

springboot学习上手

仲灏2022-04-09约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/e1e503.md for this page in Markdown format</div>

初始化项目

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 project for Spring Boot&packageName=com.example.spring-boot-learn&dependencies=web

修改springboot版本

xml
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

新增mybatis依赖

xml
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>

新增MySQL依赖

xml
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

配置数据库

properties
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

数据库

sql
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;

业务

新建Student实体类, 即对应数据库中某个表

java
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;
    }
}

新建mapper

java
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);
}

service

java
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);
    }
}

controller

java
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();
    }
}

问题

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

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

  • 确认包名
  • 修改mirror
  • 手动引入