当前位置: 首页 > wzjs >正文

牡丹江0453免费信息网站前端开发包括哪些内容

牡丹江0453免费信息网站,前端开发包括哪些内容,什么是sem营销,重庆 建站 价格优化MyBatis-Plus批量插入策略 优化MyBatis-Plus批量插入策略一、用Mybatis-plus中的saveBatch方法二、InsertBatchSomeColumn插件1.使用前配置2.代码示例1.配置类 MybatisPlusConfig2).实体类 User3).Mapper 接口 UserMapper4).测试类 InsertBatchTest 优化MyBatis-Plus批量插…

优化MyBatis-Plus批量插入策略

  • 优化MyBatis-Plus批量插入策略
    • 一、用Mybatis-plus中的saveBatch方法
    • 二、InsertBatchSomeColumn插件
      • 1.使用前配置
      • 2.代码示例
        • 1.配置类 `MybatisPlusConfig`
        • 2).实体类 User
        • 3).Mapper 接口 UserMapper
        • 4).测试类 InsertBatchTest

优化MyBatis-Plus批量插入策略

一、用Mybatis-plus中的saveBatch方法

MyBatis-Plus 的 Service 层提供了 saveBatch 方法,可以方便地实现批量插入操作。但查阅源码发现,saveBatch的底层还是逐条插入的,这就会导致当数据量大的时候程序运行效率变慢

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;// 假设实体类为 User
class User {private Long id;private String name;// 构造函数、getter 和 setter 方法public User() {}public User(Long id, String name) {this.id = id;this.name = name;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}// 假设 UserMapper 继承自 BaseMapper
interface UserMapper extends com.baomidou.mybatisplus.core.mapper.BaseMapper<User> {}// UserService 继承自 ServiceImpl
@Service
class UserService extends ServiceImpl<UserMapper, User> {public boolean saveUserBatch(List<User> userList) {return this.saveBatch(userList);}
}// 调用示例
public class Main {public static void main(String[] args) {UserService userService = new UserService();java.util.List<User> userList = new java.util.ArrayList<>();userList.add(new User(1L, "Alice"));userList.add(new User(2L, "Bob"));boolean result = userService.saveUserBatch(userList);if (result) {System.out.println("批量插入成功");} else {System.out.println("批量插入失败");}}
}

二、InsertBatchSomeColumn插件

它继承了Abstractmethod类,在 MyBatis-Plus 中,InsertBatchSomeColumn 是一种用于批量插入部分列数据的插入策略,它可以帮助我们在批量插入数据时只插入部分需要的列,而不是插入所有列。

1.使用前配置

首先,确保你的项目中已经添加了 MyBatis-Plus 和 MySQL 驱动的依赖。如果你使用的是 Maven 项目,可以在 pom.xml 中添加以下依赖:

<dependencies><!-- MyBatis-Plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency><!-- MySQL 驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version></dependency>
</dependencies>

2.代码示例

在这里插入图片描述

1.配置类 MybatisPlusConfig
  • 创建一个自定义的 DefaultSqlInjector,并在 getMethodList 方法中添加 InsertBatchSomeColumn 方法,将其注入到 MyBatis-Plus 中。
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.List;@Configuration
public class MybatisPlusConfig {@Beanpublic DefaultSqlInjector sqlInjector() {return new DefaultSqlInjector() {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass) {List<AbstractMethod> methodList = super.getMethodList(mapperClass);methodList.add(new InsertBatchSomeColumn());return methodList;}};}
}    
2).实体类 User

使用 @TableName 注解指定该实体类对应的数据库表名。
为每个属性提供了对应的 getter 和 setter 方法,这里使用了 Lombok 的 @Data 注解来自动生成这些方法

ximport com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("user")
public class User {private Long id;private String name;@TableField("age")private Integer age;private String email;
}    
3).Mapper 接口 UserMapper

继承自 BaseMapper,继承了 MyBatis-Plus 提供的基本 CRUD 方法。
使用 @InsertProvider 注解指定插入方法的 SQL 提供者为 InsertBatchSomeColumn,并调用其 sql 方法生成 SQL 语句。
定义了 insertBatchSomeColumn 方法,用于批量插入部分列数据。

import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.plugins.inner.SqlParserInner;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;import java.util.List;public interface UserMapper extends BaseMapper<User>, ProviderMethodResolver {@InsertProvider(type = InsertBatchSomeColumn.class, method = "sql")int insertBatchSomeColumn(@Param("list") List<User> list);
}    
4).测试类 InsertBatchTest

实现 CommandLineRunner 接口,在 run 方法中进行测试。
创建多个 User 对象并添加到列表中。
调用 UserMapper 的 insertBatchSomeColumn 方法进行批量插入,并打印插入的行数。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import java.util.ArrayList;
import java.util.List;@SpringBootApplication
public class InsertBatchTest implements CommandLineRunner {@Autowiredprivate UserMapper userMapper;public static void main(String[] args) {SpringApplication.run(InsertBatchTest.class, args);}@Overridepublic void run(String... args) throws Exception {List<User> userList = new ArrayList<>();User user1 = new User();user1.setName("Alice");user1.setAge(20);userList.add(user1);User user2 = new User();user2.setName("Bob");user2.setAge(22);userList.add(user2);int rows = userMapper.insertBatchSomeColumn(userList);System.out.println("插入行数: " + rows);}
}    
    user2.setName("Bob");user2.setAge(22);userList.add(user2);int rows = userMapper.insertBatchSomeColumn(userList);System.out.println("插入行数: " + rows);
}

}



文章转载自:

http://guD4dybR.Lnmby.cn
http://7zdWjMRn.Lnmby.cn
http://evbymq32.Lnmby.cn
http://vCm1Q3UT.Lnmby.cn
http://i7lDmO7Q.Lnmby.cn
http://dysx5qNl.Lnmby.cn
http://7TSgIi8h.Lnmby.cn
http://Qv5lPoB1.Lnmby.cn
http://JGNmInD4.Lnmby.cn
http://uS58j1YT.Lnmby.cn
http://yefB5u4n.Lnmby.cn
http://hUukzj1R.Lnmby.cn
http://gWvRDbV9.Lnmby.cn
http://VmlkuEfO.Lnmby.cn
http://BaijpyGF.Lnmby.cn
http://ifWOUYlc.Lnmby.cn
http://gfhpuwJg.Lnmby.cn
http://B3d41ibn.Lnmby.cn
http://LDAxvgov.Lnmby.cn
http://kTxQ1pR7.Lnmby.cn
http://k6xx9Jif.Lnmby.cn
http://e1i1dpyf.Lnmby.cn
http://eeN0yy2b.Lnmby.cn
http://CmJMdeu4.Lnmby.cn
http://9OgUaAYq.Lnmby.cn
http://4TzE0e1A.Lnmby.cn
http://ewJ2tx9C.Lnmby.cn
http://2gCF1tYe.Lnmby.cn
http://ECr2egvG.Lnmby.cn
http://DOoQ26Lu.Lnmby.cn
http://www.dtcms.com/wzjs/626032.html

相关文章:

  • 免费公司介绍网站怎么做济南高新区网站建设公司
  • 手机网站前四川煤矿基本建设工程公司网站
  • 网站建设app开发 微信小程序 网站开发 自动脚本如何查询百度搜索关键词排名
  • 书城网站建设项目定义seo内部优化方式包括
  • 设计个网站需要怎么做武清网站开发tjniu
  • 重庆市建设领域农民工工资专户网站上海建设学院网站
  • 网站建设创业规划书广州网站制作公司多少钱
  • 新网站制作怎么样江苏建设通网站
  • 吉林省住房和城乡建设厅网站6机械加工制造网
  • 沧县网站建设泰安专业网站建设
  • 网站开发的阶段流程图给网站做数据分析
  • ps手机网站制作山西长治一企业
  • 杭州网站建设费用网站建设教程百度网盘
  • 做养生网站需要什么资质高级软件开发培训
  • 网站html优化大鹏新网站建设
  • 网站建设 商城网站搭建是哪个岗位做的事儿
  • 网站建设规划结构网站建设图片编辑
  • 资讯网站如何做聚合网站建设在淘宝怎么分类
  • 163手机移动网站网站开发的推荐
  • 长治网站建设哪家好辽阳网站建设公司
  • 国家住房和城乡建设局网站拼团手机网站开发
  • 网站建设荣茂企业宣传手册封面模板
  • WordPress找不到站点武当王也高清壁纸
  • 舒兰网站建设养老保险网站
  • 上海市建设工程安全质量监督总站网站深圳企业网站制作设计
  • 绵阳网站建设联系电话263企业邮箱注册申请
  • 网站建设的费用是多少钱梅州在建工程
  • 南通外贸网站制作泉州网页建站模板
  • 建立网站图片山东网站建设的方案
  • 适配移动网站三合一网站模板