Mybatis-Plus實(shí)現(xiàn)多主鍵批量保存及更新詳情
一、依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<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>
<scope>compile</scope>
</dependency>二、啟動(dòng)類注解
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
@EnableMPP
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}三、表結(jié)構(gòu)
CREATE TABLE `product` ( `type` varchar(255) NOT NULL, `number` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`number`,`type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
四、配置文件
server:
port: 8888
#數(shù)據(jù)庫配置
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/avlicy?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true&rewriteBatchedstatements=true
username: root
password: a123456
# 連接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
#mybatis-plus
mybatis-plus:
#映射mapper.xml文件存放路徑
mapper-locations: classpath:/mapper/*Mapper.xml
#實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔
type-aliases-package: com.example.demo.entity.base,com.example.demo.entity.integration
configuration:
#下劃線轉(zhuǎn)駝峰配置
map-underscore-to-camel-cas: true
#使用二級(jí)緩存容易出現(xiàn)臟讀,建議避免使用二級(jí)緩存
cache-enabled: false
#指定 MyBatis 應(yīng)如何自動(dòng)映射列到字段或?qū)傩浴?NONE 表示取消自動(dòng)映射;PARTIAL 只會(huì)自動(dòng)映射沒有定義嵌套結(jié)果集映射的結(jié)果集。
#FULL 會(huì)自動(dòng)映射任意復(fù)雜的結(jié)果集(無論是否嵌套)。默認(rèn)是partial,這是一種全局設(shè)置
auto-mapping-behavior: full
#控制臺(tái)輸出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
五、代碼
1、實(shí)體類
@Data
public class Product implements Serializable {
@MppMultiId
@TableField(value = "type")
private String type;
@MppMultiId
@TableField(value = "number")
private Integer number;
@TableField(value = "name")
private String name;
}2、持久層
public interface ProductMapper extends MppBaseMapper<Product> {
}3、服務(wù)層
public interface ProductService extends IMppService<Product> {
}@Service
public class ProductServiceImpl extends MppServiceImpl<ProductMapper, Product> implements ProductService {
}4、邏輯層
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@PostMapping("/saveProduct")
public List<Product> querySchoolStudent2(@RequestBody List<Product> product){
productService.saveOrUpdateBatchByMultiId(product);
return product;
}
}六、測(cè)試

Creating a new SqlSession Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@391601d9] will be managed by Spring ==> Preparing: SELECT type,number,name FROM product WHERE number=? and type=? ==> Parameters: 1(Integer), 電腦(String) <== Columns: type, number, name <== Row: 電腦, 1, 紅米 <== Total: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@391601d9] will be managed by Spring ==> Preparing: UPDATE product SET type=?, number=?, name=? WHERE number=? and type=? ==> Parameters: 電腦(String), 1(Integer), 紅米(String), 1(Integer), 電腦(String) Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] from current transaction ==> Preparing: SELECT type,number,name FROM product WHERE number=? and type=? ==> Parameters: 1(Integer), 冰箱(String) <== Columns: type, number, name <== Row: 冰箱, 1, 美的 <== Total: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] ==> Parameters: 冰箱(String), 1(Integer), 美的(String), 1(Integer), 冰箱(String) Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
到此這篇關(guān)于Mybatis-Plus實(shí)現(xiàn)多主鍵批量保存及更新詳情的文章就介紹到這了,更多相關(guān)Mybatis-Plus多主鍵批量保存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
調(diào)用java.lang.Runtime.exec的正確姿勢(shì)分享
這篇文章主要介紹了調(diào)用java.lang.Runtime.exec的正確姿勢(shì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java Web項(xiàng)目中使用Socket通信多線程、長連接的方法
很多時(shí)候在javaweb項(xiàng)目中我們需要用到Socket通信來實(shí)現(xiàn)功能,在web中使用Socket我們需要建立一個(gè)監(jiān)聽程序,在程序啟動(dòng)時(shí),啟動(dòng)socket監(jiān)聽。接下來通過本文給大家介紹Java Web項(xiàng)目中使用Socket通信多線程、長連接的方法,感興趣的朋友一起學(xué)習(xí)2016-04-04
java編程創(chuàng)建型設(shè)計(jì)模式單例模式的七種示例
這篇文章主要為大家介紹了java編程中創(chuàng)建型設(shè)計(jì)模式之單例模式的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
詳解JavaEE 使用 Redis 數(shù)據(jù)庫進(jìn)行內(nèi)容緩存和高訪問負(fù)載
本篇文章主要介紹了JavaEE 使用 Redis 數(shù)據(jù)庫進(jìn)行內(nèi)容緩存和高訪問負(fù)載,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
java:程序包org.springframework.boot不存在的完美解決方法
最近項(xiàng)目中運(yùn)行的時(shí)候提示了"java: 程序包org.springframework.boot不存在",下面這篇文章主要給大家介紹了關(guān)于java:程序包org.springframework.boot不存在的完美解決方法,需要的朋友可以參考下2023-05-05

