Spring Boot应用连接数据库MySQL、及一个简单的demo

一、修改pom.xml文件

  在项目的pom.xml文件上增加如下代码,添加依赖文件。

复制代码
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
复制代码

二、设置全局配置文件

  在src/main/resources/application.properties中设置数据源和jpa配置。标红处是自己建的库

复制代码
spring.datasource.url=jdbc:mysql://123.207.46.41:3306/gwfdemo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=159753
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=1000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.port=8080
server.servlet.session.timeout=10
server.tomcat.uri-encoding=UTF-8
复制代码

三、分层业务逻辑

  POJO:domain层

复制代码
package com.example.demo.Domain;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String grade;

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

}
复制代码

  service层:

复制代码
package com.example.demo.Service;

import com.example.demo.Dao.StudentDao;
import com.example.demo.Domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired StudentDao studentDao;
    public List<Student> getAll(){
        return studentDao.getAll();
    }
}
复制代码

  dao层:

复制代码
package com.example.demo.Dao;

import com.example.demo.Domain.Student;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentDao {
    @Select("select * from student")
    public List<Student> getAll();
}
复制代码

  Controller层:

复制代码
package com.example.demo.controller;

import com.example.demo.Domain.Student;
import com.example.demo.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class StudentController {
    @Autowired
    StudentService studentService;
    @ResponseBody
    @RequestMapping("/student")
    public List<Student> getStudents(){
        return studentService.getAll();
    }
}
复制代码

  然后运行,即可查出数据,很简单的一个demo,但是代码结构大部分如此

posted @   古兰精  阅读(999)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2017-06-06 JSON.parse(JSON.stringify(obj))实现深拷贝应该注意的坑
点击右上角即可分享
微信分享提示