首页 > 其他分享 >mybatis使用foreach批量插入

mybatis使用foreach批量插入

时间:2024-07-22 17:08:42浏览次数:9  
标签:return String 批量 void item foreach mybatis public BigDecimal

创建表

create table public."match" ( match_date date not null , match_name character varying(20) not null , match_season character varying(10) not null , match_round numeric(2) not null , home_team character varying(30) not null , away_team character varying(30) not null , handicap numeric(3, 2) not null , handicap_greater numeric(4, 3) not null , handicap_less numeric(4, 3) not null , goals numeric(3, 2) not null , goals_greater numeric(4, 3) not null , goals_less numeric(4, 3) not null , corner numeric(4, 2) not null , corner_greater numeric(4, 3) not null , corner_less numeric(4, 3) not null , half_score character varying(20) not null , full_score character varying(20) not null )

创建pom.xml

<?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.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.micro</groupId>
<artifactId>springcloud-database</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-database</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

创建controller

package com.micro.springclouddatabase.controller;

import com.micro.springclouddatabase.mapper.MatchMapper;
import com.micro.springclouddatabase.model.Match;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataBaseController {

private MatchMapper matchMapper;

@Autowired
public DataBaseController(MatchMapper matchMapper) {
this.matchMapper = matchMapper;
}

@GetMapping("/match/init")
public HttpStatus insert() {
List<Match> list = TestData.getTestData();
matchMapper.insertMatch(list);
return HttpStatus.OK;
}
@GetMapping("/match/update")
public HttpStatus update() {
List<Match> list = TestData.getTestData();
matchMapper.updateMatch(list);
return HttpStatus.OK;
}
}

创建mapper 

package com.micro.springclouddatabase.mapper;

import com.micro.springclouddatabase.model.Match;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface MatchMapper {

public void insertMatch(List<Match> args);

public void updateMatch(List<Match> args);
}

创建对象类

package com.micro.springclouddatabase.model;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class Match {
private LocalDate matchDate;

private String matchName;

private String matchSeason;

private String matchRound;

private String homeTeam;

private String awayTeam;

private BigDecimal handicap;

private BigDecimal handicapGreater;

private BigDecimal handicapLess;

private BigDecimal goals;

private BigDecimal goalsGreater;

private BigDecimal goalsLess;

private BigDecimal corner;

private BigDecimal cornerGreater;

private BigDecimal cornerLess;

private String halfScore;

private String fullScore;

public Match() {

}

public Match(LocalDate matchDate, String matchName, String matchSeason, String matchRound,
String homeTeam, String awayTeam, BigDecimal handicap, BigDecimal handicapGreater,
BigDecimal handicapLess, BigDecimal goals, BigDecimal goalsGreater, BigDecimal goalsLess,
BigDecimal corner, BigDecimal cornerGreater, BigDecimal cornerLess, String halfScore, String fullScore) {
this.matchDate = matchDate;
this.matchName = matchName;
this.matchSeason = matchSeason;
this.matchRound = matchRound;
this.homeTeam = homeTeam;
this.awayTeam = awayTeam;
this.handicap = handicap;
this.handicapGreater = handicapGreater;
this.handicapLess = handicapLess;
this.goals = goals;
this.goalsGreater = goalsGreater;
this.goalsLess = goalsLess;
this.corner = corner;
this.cornerGreater = cornerGreater;
this.cornerLess = cornerLess;
this.halfScore = halfScore;
this.fullScore = fullScore;
}


public String getMatchName() {
return matchName;
}

public void setMatchName(String matchName) {
this.matchName = matchName;
}

public String getMatchSeason() {
return matchSeason;
}

public void setMatchSeason(String matchSeason) {
this.matchSeason = matchSeason;
}

public String getMatchRound() {
return matchRound;
}

public void setMatchRound(String matchRound) {
this.matchRound = matchRound;
}

public String getHomeTeam() {
return homeTeam;
}

public void setHomeTeam(String homeTeam) {
this.homeTeam = homeTeam;
}

public String getAwayTeam() {
return awayTeam;
}

public void setAwayTeam(String awayTeam) {
this.awayTeam = awayTeam;
}

public BigDecimal getHandicap() {
return handicap;
}

public void setHandicap(BigDecimal handicap) {
this.handicap = handicap;
}

public BigDecimal getHandicapGreater() {
return handicapGreater;
}

public void setHandicapGreater(BigDecimal handicapGreater) {
this.handicapGreater = handicapGreater;
}

public BigDecimal getHandicapLess() {
return handicapLess;
}

public void setHandicapLess(BigDecimal handicapLess) {
this.handicapLess = handicapLess;
}

public BigDecimal getGoals() {
return goals;
}

public void setGoals(BigDecimal goals) {
this.goals = goals;
}

public BigDecimal getGoalsGreater() {
return goalsGreater;
}

public void setGoalsGreater(BigDecimal goalsGreater) {
this.goalsGreater = goalsGreater;
}

public BigDecimal getGoalsLess() {
return goalsLess;
}

public void setGoalsLess(BigDecimal goalsLess) {
this.goalsLess = goalsLess;
}

public BigDecimal getCorner() {
return corner;
}

public void setCorner(BigDecimal corner) {
this.corner = corner;
}

public BigDecimal getCornerGreater() {
return cornerGreater;
}

public void setCornerGreater(BigDecimal cornerGreater) {
this.cornerGreater = cornerGreater;
}

public BigDecimal getCornerLess() {
return cornerLess;
}

public void setCornerLess(BigDecimal cornerLess) {
this.cornerLess = cornerLess;
}

public String getHalfScore() {
return halfScore;
}

public void setHalfScore(String halfScore) {
this.halfScore = halfScore;
}

public String getFullScore() {
return fullScore;
}

public void setFullScore(String fullScore) {
this.fullScore = fullScore;
}
}

创建插入SQL 和 更新SQL<?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.micro.springclouddatabase.mapper.MatchMapper">
<insert id="insertMatch" parameterType="com.micro.springclouddatabase.model.Match">
INSERT INTO match (
match_date
, match_name
, match_season
, match_round
, home_team
, away_team
, handicap
, handicap_greater
, handicap_less
, goals
, goals_greater
, goals_less
, corner
, corner_greater
, corner_less
, half_score
, full_score
) VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.matchDate}
, #{item.matchName}
, #{item.matchSeason}
, to_number(#{item.matchRound}, '99')
, #{item.homeTeam}
, #{item.awayTeam}
, #{item.handicap}
, #{item.handicapGreater}
, #{item.handicapLess}
, #{item.goals}
, #{item.goalsGreater}
, #{item.goalsLess}
, #{item.corner}
, #{item.cornerGreater}
, #{item.cornerLess}
, #{item.halfScore}
, #{item.fullScore}
)
</foreach>
</insert>

<update id="updateMatch" parameterType="java.util.List">
<foreach collection="list" item="item" separator=";">
update match
<set>
half_score = #{item.halfScore}
, full_score = #{item.fullScore}
</set>
WHERE
match_date = #{item.matchDate}
AND match_name = #{item.matchName}
AND home_team = #{item.homeTeam}
AND away_team = #{item.awayTeam}
</foreach>
</update>
</mapper>
 

标签:return,String,批量,void,item,foreach,mybatis,public,BigDecimal
From: https://www.cnblogs.com/michaelShao/p/18316441

相关文章

  • 如何批量上传到Remini?
    因此,我必须使用Android上的Remini应用程序来增强上千张不同的图像。我尝试手动处理这些图像,但每张图像都花费了我30秒的时间和大量的精力。问题是我总是需要从图库中选择不同的图像,然后等到它得到增强,然后我可以将其保存到我的图库中。遗憾的是,Remini不允许您批量上传......
  • 用SqlBulkCopy批量插入数据 遇到的错误
    原文链接:https://www.cnblogs.com/wz327/archive/2011/07/05/2098356.html错误一:来自数据源的String类型的给定值不能转换为指定目标列的类型nvarchar。还有其他的错误如:AddTime不能为DBNull(这个应该是目标表中AddTime要求不许为null) 可能的原因有两种可能是有"'"(单引......
  • linux-批量修改文件内容
    1.批量修改文件内容$find.-typef-execsed-i's/oldname/newname/g'{}+#此命令含义:在当前目录及其所有子目录中查找所有文件,并对这些文件执行sed命令,将文件内容中的所有oldname字符串替换为newname。#find.:从当前目录(.)开始查找文件。#typef:指定查找的类型为文......
  • 以块的形式处理大型 Spark DataFrame 的推荐方法是什么:“toPandas()”或“RDD.foreach
    我正在使用PySpark处理大型数据集,并且需要以每个500条记录的块的形式处理数据。我正在考虑使用toPandas()将我的SparkDataFrames转换为PandasDataFrames以方便分块或坚持使用SparkRDD,并使用foreachPartition()手动处理分块。以下是我正在考虑的示例方......
  • SpringBoot利用MyBatis连接Mysql数据库时常见启动报错
    目录报错情况报错情况一:​编辑报错情况二:解决步骤一、解决命名问题1.mapper层的id是否和Dao层的方法名字相同2.检查namespace与Dao层的文件地址相同二、解决注解问题1.检查Controller层的注解是否正确和完整2.Dao层或者Mapper层的注解3.pojo层:实体类层Data注解(用来......
  • JavaWeb MyBatis案例
    JAVAWEBMyBatis视频学习笔记MyBatis案例1环境准备1.1数据库准备1.2准备一个Brand实体类1.3准备测试用例1.4安装MyBatisX插件2编写接口2.1编写查询所有2.1.1编写Mapper接口2.1.2编写SQL映射文件2.1.3编写测试语句完成测试2.1.4BugFix2.2查看详情2.2.1编......
  • 腾讯课堂视频批量下载
    腾讯课堂视频下载重要通知腾讯课堂将于2024年8月1日起停止所有在线课程的访问服务,用户将无法访问新的课程内容,但可继续观看【课程表】中的历史免费课程内容。腾讯课堂将于2024年10月1日停止运营,届时全面停止所有平台服务,感谢各位用户多年以来的支持与陪伴。腾讯......
  • TooBox也盒-图片批量裁剪器
    TooBox也盒v0.2.0版本上线,支持图片批量裁剪。在进行Lora训练时,通常需要这三种尺寸,但有时我们的素材并不符合这些规格,这时就需要进行裁剪。首先启动TooBox也盒,选择“图片裁剪”功能,然后选择包含待裁剪图片的文件夹。选定文件夹后,图片显示区域将展示该文件夹中的所有图片。......
  • 如何在 python 脚本中调用 robocopy 来批量复制多个文件夹?
    我正在尝试在网络驱动器之间移动多个大文件夹(>10Gb、>100个子文件夹、>2000个文件)。我尝试过在python中使用Shutil.copytree命令,它工作得很好,只是由于不同的原因它无法复制一小部分(<1%的文件)。我相信robocopy对我来说是最好的选择,因为我可以创建一个记录传输过......
  • SQL批量插入测试数据的几种方法?
    在开发过程中我们不管是用来测试性能还是在生产环境中页面展示好看一点,又或者学习验证某一知识点经常需要一些测试数据,这个时候如果手敲的话,十行二十行还好,多了就很死亡了,接下来介绍两种常用的MySQL测试数据批量生成方式 在SQL中,批量插入测试数据通常有几种方......