springboot+mybatis实现简单的增、删、查、改

这篇文章主要针对java初学者,详细介绍怎么创建一个基本的springboot项目来对数据库进行crud操作。

第一步:创建springboot项目

在IntelliJ IDEA中通过spring initilizer创建springboot项目如图,依次点击左上角的菜单栏中的File >> New >> Project...

然后在打开的窗口左侧选择Spring Initilizer,在右侧面板:

  • 修改项目名为springboot-crud
  • 修改包名为com.example.springboot
  • 修改java版本为8

然后点击Next按钮

接着点击Finish按钮,等待idea创建并下载springboot项目。

第二步:修改pom.xml

修改pom.xml配置文件,修改springboot的版本为2.5.9,并添加和必要的依赖,删除test依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.9</version>
        <relativePath />
    </parent>

    <groupId>com.example</groupId>
    <artifactId>springboot-crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-crud</name>
    <description>springboot+mybatis实现增删查改的入门项目</description>

    <properties>
        <java.version>1.8</java.version>
        <mysql.version>8.0.28</mysql.version>
        <druid.version>1.1.21</druid.version>
        <lombok.version>1.18.22</lombok.version>
        <mybatis.version>2.2.2</mybatis.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--lombok:自动生成getter、setter工具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!--druid数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--spring boot整合mybatis的依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

第三步:准备数据库

创建一个数据库springboot-crud,然后执行项目doc/sql目录下的springboot-crud.sql

第四步:修改配置文件

springboot的配置文件有两种格式,即application.properties或application.yml。

推荐使用yml文件格式,重命名application.properties为application.yml。然后添加数据库的数据源设置,并修改项目的启动端口号为8083,然后设置mybatis的mapper.xml文件的位置以及日志隔离级别为debug(默认info)。

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/springboot-crud
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

server:
  port: 8083

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

logging:
  level:
    springfox: error
    com.example.springboot: debug

第五步:创建数据库对应的实体类

在项目的根目录com.example.springboot下创建entity包,然后在entity包下面创建一个实体类Song.java,并实现序列化接口Serializable。

package com.example.springboot.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 歌曲
 * @author heyunlin
 * @version 1.0
 */
@Data
public class Song implements Serializable {
    private static final long serialVersionUID = 18L;

    private String id;

    /**
     * 歌曲名
     */
    private String name;

    /**
     * 歌手
     */
    private String singer;

    /**
     * 描述信息
     */
    private String note;

    /**
     * 最后一次修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime lastUpdateTime;
}

第六步:创建持久层接口

同样的,在项目根路径下创建mapper包,在mapper包下创建一个接口SongMapper,在接口上添加Spring组件注解@Repository。

package com.example.springboot.mapper;

import org.springframework.stereotype.Repository;

/**
 * @author heyunlin
 * @version 1.0
 */
@Repository
public interface SongMapper {

}

第七步:创建Mapper.xml文件

在src\main\resources下创建mapper包,然后在mapper包下面创建一个xml文件SongMapper.xml。其中namespace属性的值为SongMapper接口的全限定名

<?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.springboot.mapper.SongMapper">

</mapper>

第八步:开启mapper包扫描

因为我们的持久层接口没有使用@Mapper注解,需要通过@MapperScan来配置mapper的包路径,通常我们会在一个单独的配置类上添加@MapperScan注解。

在项目根路径下创建config包,在config包下创建MybatisConfig.java,MybatisConfig上使用@Configuration将该类声明为配置类,通过@MapperScan注解指定mapper包扫描路径。

package com.example.springboot.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

/**
 * Mybatis配置文件
 * @author heyunlin
 * @version 1.0
 */
@Configuration
@MapperScan(basePackages = "com.example.springboot.mapper")
public class MybatisConfig {

}

第九步:创建业务层接口

在项目根路径下创建service包,在service包下创建接口SongService.java

package com.example.springboot.service;

/**
 * @author heyunlin
 * @version 1.0
 */
public interface SongService {

}

然后在service包下创建impl子包,创建一个SongService接口的实现类,并在实现类上使用@Service注解。

package com.example.springboot.service.impl;

import com.example.springboot.service.SongService;
import org.springframework.stereotype.Service;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

}

第十步:创建控制器类 

在项目根路径下创建controller包,在controller包下创建SongController.java类。

其中,@RestController注解是一个复合注解,由两个注解@RespondBody和@Controller组成,@RespondBody表示类下面的方法的返回值会被转换为json格式的字符串返回给调用者。

package com.example.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces="application/json;charset=utf-8")
public class SongController {
    
}

至此,项目所需的所有类都创建好了,项目完整目录结构如下图:

第十一步:开始开发业务功能

完成以上步骤之后,springboot项目的基本开发环境就搭建好了,接下来开始实现对歌曲表song的增删查改功能。

在此之前,需要把service层的SongService注入到SongController中,在这里使用《阿里巴巴开发规范》推荐的构造器注入方式。

package com.example.springboot.controller;

import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces="application/json;charset=utf-8")
public class SongController {

    private final SongService songService;

    @Autowired
    public SongController(SongService songService) {
        this.songService = songService;
    }

}

1、添加歌曲

controller层

首先,在controller层创建一个方法,取名为insert,在方法内调用service层的对应方法,为了方便同样取名为insert

package com.example.springboot.controller;

import com.example.springboot.entity.Song;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces="application/json;charset=utf-8")
public class SongController {

    private final SongService songService;

    @Autowired
    public SongController(SongService songService) {
        this.songService = songService;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public void insert(Song song) {
        songService.insert(song);
    }

}

service层(业务层)

这时候,songService.insert(song);这行代码会报错,因为我们SongService接口没有这个方法,把鼠标移到红色的地方,键盘按alt+enter(回车键),选择第一个选项,在SongService创建insert()方法。

package com.example.springboot.service;

import com.example.springboot.entity.Song;

/**
 * @author heyunlin
 * @version 1.0
 */
public interface SongService {

    void insert(Song song);
}

然后这时候发现SongService这个类也有红色的提示,因为SongServiceImpl实现了SongService接口,但是没有实现刚刚添加的insert()方法。

 如上图,点击红色的提示信息,会自动打开SongServiceImpl

我们把鼠标移到类上面,会提示实现方法,点击蓝色implement methods自动生成insert()方法。

package com.example.springboot.service.impl;

import com.example.springboot.entity.Song;
import com.example.springboot.service.SongService;
import org.springframework.stereotype.Service;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

    @Override
    public void insert(Song song) {
        
    }

}

然后,为了能操作数据库,需要注入持久层的Mapper接口SongMapper,并调用持久层接口的方法来操作数据库。

因为表song的主键是非自增的,需要手动设置,使用当前时间生成uuid作为Id字段的值。

package com.example.springboot.service.impl;

import com.example.springboot.entity.Song;
import com.example.springboot.mapper.SongMapper;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

    private final SongMapper songMapper;

    @Autowired
    public SongServiceImpl(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    @Override
    public void insert(Song song) {
        song.setId(uuid());
        
        songMapper.insert(song);
    }

    /**
     * 根据当前时间生成UUID
     * @return String
     */
    private static String uuid() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.now();

        return localDate.format(formatter);
    }

}

mapper层(持久层)

同样的,在SongMapper接口上创建insert()方法,如果安装了mybatisx插件,也会有红色的提示,让你在SongMapper.xml上创建对应的方法。

package com.example.springboot.mapper;

import com.example.springboot.entity.Song;
import org.springframework.stereotype.Repository;

/**
 * @author heyunlin
 * @version 1.0
 */
@Repository
public interface SongMapper {

    void insert(Song song);
}

 mapper.xml中添加一个insert标签,id属性值为方法名

<?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.springboot.mapper.SongMapper">

    <insert id="insert">
        insert into song(id, name, singer, note, last_update_time)
        values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})
    </insert>
</mapper>

当然也可以在接口上使用注解来绑定SQL语句(推荐在xml文件中编写sql)

package com.example.springboot.mapper;

import com.example.springboot.entity.Song;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Repository;

/**
 * @author heyunlin
 * @version 1.0
 */
@Repository
public interface SongMapper {

    @Insert("insert into song(id, name, singer, note, last_update_time) values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})")
    void insert(Song song);
}

注意:只能通过注解或mapper.xml其中一个方法来绑定SQL语句,否则运行项目会报错。

2、删除歌曲

同样的,依次创建controller、service和mapper层的方法,然后在controller调用service,service调用mapper。

mapper层(持久层)

package com.example.springboot.mapper;

import com.example.springboot.entity.Song;
import org.springframework.stereotype.Repository;

/**
 * @author heyunlin
 * @version 1.0
 */
@Repository
public interface SongMapper {

    //@Insert("insert into song(id, name, singer, note, last_update_time) values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})")
    void insert(Song song);

    //@Delete("delete from song where id = #{id}")
    void deleteById(String id);
}
<?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.springboot.mapper.SongMapper">

    <insert id="insert">
        insert into song(id, name, singer, note, last_update_time)
        values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})
    </insert>

    <delete id="deleteById">
        delete from song where id = #{id}
    </delete>
</mapper>

service层(业务层)

SongService.java

package com.example.springboot.service;

import com.example.springboot.entity.Song;

/**
 * @author heyunlin
 * @version 1.0
 */
public interface SongService {

    void insert(Song song);

    void deleteById(String id);
}

 SongServiceImpl

package com.example.springboot.service.impl;

import com.example.springboot.entity.Song;
import com.example.springboot.mapper.SongMapper;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

    private final SongMapper songMapper;

    @Autowired
    public SongServiceImpl(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    @Override
    public void insert(Song song) {
        song.setId(uuid());

        songMapper.insert(song);
    }

    @Override
    public void deleteById(String id) {
        songMapper.deleteById(id);
    }

    /**
     * 根据当前时间生成UUID
     * @return String
     */
    private static String uuid() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.now();

        return localDate.format(formatter);
    }

}

controller层

在这里使用rest风格的请求方式,通过@PathVariable注解绑定路径参数,也就是下面歌曲编号(id)对应的字符串值

请求url格式

http://localhost:8083/song/deleteById/歌曲编号

例如,这时候id获取到的值为10011

http://localhost:8083/song/deleteById/10011

package com.example.springboot.controller;

import com.example.springboot.entity.Song;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces="application/json;charset=utf-8")
public class SongController {

    private final SongService songService;

    @Autowired
    public SongController(SongService songService) {
        this.songService = songService;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public void insert(Song song) {
        songService.insert(song);
    }

    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET)
    public void deleteById(@PathVariable("id") String id) {
        songService.deleteById(id);
    }

}

3、修改歌曲信息

mapper层(持久层)

SongMapper.java

package com.example.springboot.mapper;

import com.example.springboot.entity.Song;
import org.springframework.stereotype.Repository;

/**
 * @author heyunlin
 * @version 1.0
 */
@Repository
public interface SongMapper {

    //@Insert("insert into song(id, name, singer, note, last_update_time) values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})")
    void insert(Song song);

    //@Delete("delete from song where id = #{id}")
    void deleteById(String id);

    // @Update("update song set name = #{name}, singer = #{singer}, note = #{note}, last_update_time = #{lastUpdateTime} where id = #{id}")
    void updateById(Song song);
}

 SongMapper.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.springboot.mapper.SongMapper">

    <insert id="insert">
        insert into song(id, name, singer, note, last_update_time)
        values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})
    </insert>

    <delete id="deleteById">
        delete from song where id = #{id}
    </delete>

    <update id="updateById">
        update song
        set
            name = #{name}, 
            singer = #{singer},
            note = #{note},
            last_update_time = #{lastUpdateTime}
        where id = #{id}
    </update>
</mapper>

service层(业务层)

SongService.java

package com.example.springboot.service;

import com.example.springboot.entity.Song;

/**
 * @author heyunlin
 * @version 1.0
 */
public interface SongService {

    void insert(Song song);

    void deleteById(String id);

    void updateById(Song song);
}

SongServiceImpl.java

package com.example.springboot.service.impl;

import com.example.springboot.entity.Song;
import com.example.springboot.mapper.SongMapper;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

    private final SongMapper songMapper;

    @Autowired
    public SongServiceImpl(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    @Override
    public void insert(Song song) {
        song.setId(uuid());

        songMapper.insert(song);
    }

    @Override
    public void deleteById(String id) {
        songMapper.deleteById(id);
    }

    @Override
    public void updateById(Song song) {
        song.setLastUpdateTime(LocalDateTime.now());

        songMapper.updateById(song);
    }

    /**
     * 根据当前时间生成UUID
     * @return String
     */
    private static String uuid() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.now();

        return localDate.format(formatter);
    }

}

controller层

package com.example.springboot.controller;

import com.example.springboot.entity.Song;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces="application/json;charset=utf-8")
public class SongController {

    private final SongService songService;

    @Autowired
    public SongController(SongService songService) {
        this.songService = songService;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public void insert(Song song) {
        songService.insert(song);
    }

    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET)
    public void deleteById(@PathVariable("id") String id) {
        songService.deleteById(id);
    }

    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public void updateById(Song song) {
        songService.updateById(song);
    }

}

4、查询歌曲详情

controller层

package com.example.springboot.controller;

import com.example.springboot.entity.Song;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/song", produces="application/json;charset=utf-8")
public class SongController {

    private final SongService songService;

    @Autowired
    public SongController(SongService songService) {
        this.songService = songService;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public void insert(Song song) {
        songService.insert(song);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void delete(@PathVariable("id") String id) {
        songService.delete(id);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(Song song) {
        songService.update(song);
    }

    @RequestMapping(value = "/selectById/{id}", method = RequestMethod.GET)
    public Song selectById(@PathVariable("id") String id) {
        return songService.selectById(id);
    }

}

service层(业务层)

SongService.java

package com.example.springboot.service;

import com.example.springboot.entity.Song;

/**
 * @author heyunlin
 * @version 1.0
 */
public interface SongService {

    void insert(Song song);

    void deleteById(String id);

    void updateById(Song song);

    Song selectById(String id);
}

SongServiceImpl.java

package com.example.springboot.service.impl;

import com.example.springboot.entity.Song;
import com.example.springboot.mapper.SongMapper;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

    private final SongMapper songMapper;

    @Autowired
    public SongServiceImpl(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    @Override
    public void insert(Song song) {
        song.setId(uuid());

        songMapper.insert(song);
    }

    @Override
    public void deleteById(String id) {
        songMapper.deleteById(id);
    }

    @Override
    public void updateById(Song song) {
        song.setLastUpdateTime(LocalDateTime.now());

        songMapper.updateById(song);
    }

    @Override
    public Song selectById(String id) {
        return songMapper.selectById(id);
    }

    /**
     * 根据当前时间生成UUID
     * @return String
     */
    private static String uuid() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.now();

        return localDate.format(formatter);
    }

}

mapper层(持久层)

SongMapper.java

package com.example.springboot.mapper;

import com.example.springboot.entity.Song;
import org.springframework.stereotype.Repository;

/**
 * @author heyunlin
 * @version 1.0
 */
@Repository
public interface SongMapper {

    //@Insert("insert into song(id, name, singer, note, last_update_time) values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})")
    void insert(Song song);

    //@Delete("delete from song where id = #{id}")
    void deleteById(String id);

    // @Update("update song set name = #{name}, singer = #{singer}, note = #{note}, last_update_time = #{lastUpdateTime} where id = #{id}")
    void updateById(Song song);

    // @Select("select * from song where id = #{id})
    Song selectById(String id);
}

 SongMapper.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.springboot.mapper.SongMapper">

    <insert id="insert">
        insert into song(id, name, singer, note, last_update_time)
        values(#{id}, #{name}, #{singer}, #{note}, #{lastUpdateTime})
    </insert>

    <delete id="deleteById">
        delete from song where id = #{id}
    </delete>

    <update id="updateById">
        update song set name = #{name}, singer = #{singer}, note = #{note}, last_update_time = #{lastUpdateTime} where id = #{id}
    </update>

    <select id="selectById" resultType="com.example.springboot.entity.Song">
        select id, name, singer, note, last_update_time as lastUpdateTime from song where id = #{id}
    </select>
</mapper>

最后,我们简单测试一下写的4个接口,推荐使用postman进行测试,可通过以下网盘链接下载

下载postmanicon-default.png?t=N7T8https://pan.baidu.com/s/1ViOqK6pp_Yj0Wfi2vAJsnA?pwd=yr5a好了,文章就分享到这里了,代码已开源,按需获取,如果这篇文章对你有所帮助,不要忘了点赞+收藏哦~

springboot+mybatis实现简单的增删查改案例项目icon-default.png?t=N7T8https://gitee.com/he-yunlin/springboot-crud.git

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值