Mybatis-Plus實現(xiàn)多主鍵批量保存及更新詳情
更新時間:2022年09月21日 11:02:52 作者:李長淵哦
這篇文章主要介紹了Mybatis-Plus實現(xiàn)多主鍵批量保存及更新詳情,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
一、依賴
<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>二、啟動類注解
@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
#實體掃描,多個package用逗號或者分號分隔
type-aliases-package: com.example.demo.entity.base,com.example.demo.entity.integration
configuration:
#下劃線轉(zhuǎn)駝峰配置
map-underscore-to-camel-cas: true
#使用二級緩存容易出現(xiàn)臟讀,建議避免使用二級緩存
cache-enabled: false
#指定 MyBatis 應(yīng)如何自動映射列到字段或?qū)傩浴?NONE 表示取消自動映射;PARTIAL 只會自動映射沒有定義嵌套結(jié)果集映射的結(jié)果集。
#FULL 會自動映射任意復(fù)雜的結(jié)果集(無論是否嵌套)。默認(rèn)是partial,這是一種全局設(shè)置
auto-mapping-behavior: full
#控制臺輸出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
五、代碼
1、實體類
@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;
}
}六、測試

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實現(xiàn)多主鍵批量保存及更新詳情的文章就介紹到這了,更多相關(guān)Mybatis-Plus多主鍵批量保存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java編程刪除鏈表中重復(fù)的節(jié)點問題解決思路及源碼分享
這篇文章主要介紹了Java編程刪除鏈表中重復(fù)的節(jié)點問題解決思路及源碼分享,具有一定參考價值,這里分享給大家,供需要的朋友了解。2017-10-10
springboot利用@Aspect實現(xiàn)日志工具類的詳細(xì)代碼
這篇文章主要介紹了springboot利用@Aspect實現(xiàn)日志工具類,通過實例代碼介紹了導(dǎo)包及在啟動類上進(jìn)行注解自動掃描的方法,需要的朋友可以參考下2022-03-03
Java結(jié)合Kotlin實現(xiàn)寶寶年齡計算
這篇文章主要為大家介紹了Java結(jié)合Kotlin實現(xiàn)寶寶年齡計算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

