一、依赖
<dependencies> ......//其他依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.github.jeffreyning</groupId> <artifactId>mybatisplus-plus</artifactId> <version>1.5.1-RELEASE</version> </dependency> </dependencies>
二、表结构语句
CREATE TABLE `SCORE` ( `STUDENT_ID` INT NOT NULL COMMENT '学号', `COURSE_NO` INT DEFAULT 0 COMMENT'课程号', `SCORE` INT DEFAULT 0 COMMENT '分数', PRIMARY KEY (`STUDENT_ID`,`COURSE_NO`) ) ENGINE=INNODB DEFAULT CHARSET=UTF8;
三、启动类
启动类上加上@EnableMPP注解
package com.chenly.mpp; import com.github.jeffreyning.mybatisplus.conf.EnableMPP; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author: chenly * @date: 2022-11-18 16:55 * @description: * @version: 1.0 */ @SpringBootApplication @MapperScan("com.chenly.mpp.mapper") @EnableMPP public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
四、实体类
在主键字段上加上@MppMultiId注解
package com.chenly.mpp.entity; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.activerecord.Model; import com.github.jeffreyning.mybatisplus.anno.MppMultiId; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import java.io.Serializable; /** * <p> * 成绩单实体类 * </p> * @author chenly * @since 2022-11-18 */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("SCORE") public class Score extends Model<Score> { private static final long serialVersionUID = 1L; /** "学号" */ @MppMultiId @TableField("STUDENT_ID") private Integer studentId; /** "课程号" */ @MppMultiId @TableField("COURSE_NO") private Integer courseNo; /** "分数" */ @TableField("SCORE") private Integer score; @Override protected Serializable pkVal() { return this.studentId; } }
五、Mapper
继承MppBaseMapper类
package com.chenly.mpp.mapper; import com.chenly.mpp.entity.Score; import com.github.jeffreyning.mybatisplus.base.MppBaseMapper; /** * <p> * Mapper 接口 * </p> * @author chenly * @since 2022-11-18 */ public interface ScoreMapper extends MppBaseMapper<Score> { }
六、Service
service层接口继承IMppService
package com.chenly.mpp.service; import com.chenly.mpp.entity.Score; import com.github.jeffreyning.mybatisplus.service.IMppService; /** * <p> * 服务接口 * </p> * * @author chenly * @since 2022-11-18 */ public interface ScoreService extends IMppService<Score> { }
service层实现类继承MppServiceImpl
package com.chenly.mpp.service.impl; import com.chenly.mpp.entity.Score; import com.chenly.mpp.mapper.ScoreMapper; import com.chenly.mpp.service.ScoreService; import com.github.jeffreyning.mybatisplus.service.MppServiceImpl; import org.springframework.stereotype.Service; /** * <p> * 服务实现类 * </p> * @author chenly * @since 2022-11-18 */ @Service public class ScoreServiceImpl extends MppServiceImpl<ScoreMapper, Score> implements ScoreService { }
七、controller类
调用saveOrUpdateBatchByMultiId()方法
package com.chenly.mpp.controller; import com.chenly.mpp.entity.Score; import com.chenly.mpp.service.ScoreService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author: chenly * @date: 2022-11-18 17:44 * @description: * @version: 1.0 */ @RestController @RequestMapping("/score") public class ScoreController { @Autowired private ScoreService scoreService; @PostMapping("/save") public List<Score> querySchoolStudent2(@RequestBody List<Score> product){ scoreService.saveOrUpdateBatchByMultiId(product); return product; } }
八、测试
用jmeter发送请求,连续发送2次
第1次请求,控制台输出:
第2次请求,控制台输出:
标签:mybatisplus,org,plus,mpp,chenly,import,com,主键 From: https://www.cnblogs.com/kiko2014551511/p/16921690.html