如何快速的编写一个简单的springboot项目,并访问mysql

本文介绍如何使用SpringBoot快速构建一个包含增删改查功能的简易应用程序,包括项目搭建、依赖配置、实体类定义及数据库交互等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 

1.新建一个springboot项目,选择Create new Project

2.选择Spring initializr,再点击next

3.把需要修改的信息修改后,点击下一步

4.选择web——>Spring web,再点击下一步

5.点击finish

6.打开pom.xml文件,添加依赖

7.新建controller,service,dao,entity类

      Controller层

      Service层

      entity

      Dao层

      配置文件application.properties

8.建立一个demo数据库

9.启动demo,可以发现已经自动建表

10.使用postman测试

11.在数据库中得到结果


1.新建一个springboot项目,选择Create new Project


2.选择Spring initializr,再点击next


3.把需要修改的信息修改后,点击下一步


4.选择web——>Spring web,再点击下一步


5.点击finish


6.打开pom.xml文件,添加依赖


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

 


7.新建controller,service,dao,entity类


Controller层

package com.example.demo.controller;

import com.example.demo.entity.DemoEntity;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoService demoService;


    @PostMapping("/add")
    public void addChargingHub(@RequestBody DemoEntity demoEntity){
        demoService.saveDemoEntity(demoEntity);
    }
}

Service层

package com.example.demo.service;

import com.example.demo.dao.DemoDao;
import com.example.demo.entity.DemoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DemoService {

    @Autowired
    private DemoDao demoDao;

    public void saveDemoEntity(DemoEntity demoEntity) {
        demoDao.save(demoEntity);
    }
}

entity

package com.example.demo.entity;

import javax.persistence.*;

@Entity
@Table(name = "demo_info")
public class DemoEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name", length = 255)
    private String name;

    @Column(name = "description", length = 255)
    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Dao层

package com.example.demo.dao;

import com.example.demo.entity.DemoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DemoDao extends JpaRepository<DemoEntity, Integer> {
}

配置文件application.properties

server.port=8088

###数据库连接信息
# 连接地址
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
# 数据库账户
spring.datasource.username = root
#数据库密码
spring.datasource.password = 123456
#数据库驱动
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

8.建立一个demo数据库


9.启动demo,可以发现已经自动建表


10.使用postman测试


11.在数据库中得到结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小江小河点、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值