赞
踩
这篇文章主要针对java初学者,详细介绍怎么创建一个基本的springboot项目来对数据库进行crud操作。
在IntelliJ IDEA中通过spring initilizer创建springboot项目如图,依次点击左上角的菜单栏中的File >> New >> Project...
然后在打开的窗口左侧选择Spring Initilizer,在右侧面板:
然后点击Next按钮
接着点击Finish按钮,等待idea创建并下载springboot项目。
修改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 {
-
- }
在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注解,需要通过@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;
- }
-
- }
首先,在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);
- }
-
- }
这时候,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);
- }
-
- }
同样的,在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语句,否则运行项目会报错。
同样的,依次创建controller、service和mapper层的方法,然后在controller调用service,service调用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>
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);
- }
-
- }
在这里使用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);
- }
-
- }
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>
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);
- }
-
- }
- 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);
- }
-
- }
- 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);
- }
-
- }
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);
- }
-
- }
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进行测试,可通过以下网盘链接下载
下载postmanhttps://pan.baidu.com/s/1ViOqK6pp_Yj0Wfi2vAJsnA?pwd=yr5a好了,文章就分享到这里了,代码已开源,按需获取,如果这篇文章对你有所帮助,不要忘了点赞+收藏哦~
springboot+mybatis实现简单的增删查改案例项目https://gitee.com/he-yunlin/springboot-crud.git
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。