springboot+mybatis+mysql搭建后端接口案例

本文档详细介绍了如何利用Spring Boot、MyBatis和MySQL来构建后端接口。首先介绍项目结构,接着讲解了在pom.xml中添加依赖、配置application.yml文件以连接数据库。然后分别展示了Mapper.xml、控制层、服务层、服务实现层、DAO层以及DO层的代码编写。最后,启动项目并展示了运行结果,对于初学者来说,这是一个理解Spring Boot框架的好起点。

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

首先导入springboot项目
项目结构图如下
在这里插入图片描述

1.下载依赖,在pom.xml文件添加如下代码

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

接下来便是application.yml文件内容的修改,代码如下,数据库的连接自己修改

server:
  port: 8080

spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shangchen?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
    username: root
    password: root


mybatis:
  mapper-locations: classpath:mybatis/*Mapper.xml

在mybatis文件夹内填写Mapper.xml,本文案例如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.first.dao.GoodsDao">
    <select id="get" resultType="com.example.first.domain.GoodsDo">
		select `gid`,`gname`,`gprice`,`gdesc` from Goods where gid = #{value}
	</select>
    <select id="list" resultType="com.example.first.domain.GoodsDo">
        select
        `gid`,`gname`,`gprice`,`gdesc`
        from Goods
    </select>
    <insert id="save" parameterType="com.example.first.domain.GoodsDo" useGeneratedKeys="true" keyProperty="gid">
		insert into Goods
		(
          `gid`, `gname`,`gprice`,`gdesc`
		)
		values
		(
			#{gid},#{gname},#{gprice},#{gdesc}
		)
	</insert>
    <delete id="remove">
		delete from Goods where gid = #{value}
	</delete>
    <update id="update">
        update Goods set gprice = #{gprice} where gid = #{gid}
    </update>
</mapper>

控制层代码

@RestController
public class IndexController {
    @Autowired
    private GoodsServer goodsServer;
    @RequestMapping("/get")
    public String get(int id) {
        GoodsDo goodDo=goodsServer.get(id);
        //System.out.println(goodDo);
        return goodDo.toString();
    }
    @RequestMapping("/list")
    public List<GoodsDo> list() {
        List<GoodsDo> goodsDo=goodsServer.list();
        return goodsDo;
    }

    @RequestMapping("/delete")
    public String deldete(int id) {
        goodsServer.remove(id);
        return "it's ok!";
    }
    @RequestMapping("/update")
    public String update(int id,double price) {
        goodsServer.update(id,price);
        return "it's ok!";
    }
    @RequestMapping("/insert")
    public String insert(int id,String desc,String name,double price) {
        GoodsDo goodsDo=new GoodsDo();
        goodsDo.setGid(id);
        goodsDo.setGdesc(desc);
        goodsDo.setGname(name);
        goodsDo.setGprice(price);
        goodsServer.save(goodsDo);
        return "it's ok!";
    }
}

服务层goodsserver代码

public interface GoodsServer {
    GoodsDo get(int id);
    List<GoodsDo> list();
    void save(GoodsDo GoodsContent);
    void update(@Param("gid") int gid, @Param("gprice") double gprice);
    int remove(int id);
}

服务层goodsserverimpl代码

@Service
@ComponentScan
public  class GoodsServerImpl implements GoodsServer {
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public GoodsDo get(int id){
        return goodsDao.get(id);
    }
    @Override
    public void update(int id,double gprice){
        goodsDao.update(id,gprice); }
    @Override
    public int remove(int id){
        goodsDao.remove(id);
        return 1; }
    @Override
    public void save(GoodsDo goodsDo){
        goodsDao.save(goodsDo); }
    @Override
    public List<GoodsDo> list(){
        List<GoodsDo> goodsDo=goodsDao.list();
        System.out.println(goodsDo);
        return goodsDo; }
}

dao层googsDao代码

@Mapper
public interface GoodsDao {
    GoodsDo get(int id);
    List<GoodsDo> list();
    int save(GoodsDo GoodsContent);
    void update(@Param("gid") int gid, @Param("gprice") double gprice);
    int remove(int id);
}

Do层goodsDo代码

@Getter
@Setter
public class GoodsDo {
    private int gid;
    private String gname;
    private double gprice;
    private String gdesc;
    public String toString() {
        return "{gid='" + this.gid + '\'' + ", gname='" + this.gname + '\'' + ", gprice='" + this.gprice + '\'' + ", gdesc='" + this.gdesc +'}';
    }
}

启动类,记得写上@MapperScan(“com.example.first.dao”)

@SpringBootApplication
@MapperScan("com.example.first.dao")
public class FirstApplication {
   //将springboot应用启动
    public static void main(String[] args) {
        SpringApplication.run(FirstApplication.class, args);
    }
}

运行结果截图如下
在这里插入图片描述
好了,springboot的代码讲解就到这了,对于新手来说,这个简单的项目可以帮助你们更好的理解springboot框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值