Mybatis-Plus实现多主键批量保存及更新详情
短信预约 -IT技能 免费直播动态提醒
一、依赖
<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);
}
}
三、表结构
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
#数据库配置
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:
#下划线转驼峰配置
map-underscore-to-camel-cas: true
#使用二级缓存容易出现脏读,建议避免使用二级缓存
cache-enabled: false
#指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示取消自动映射;PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。
#FULL 会自动映射任意复杂的结果集(无论是否嵌套)。默认是partial,这是一种全局设置
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、服务层
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]
到此这篇关于Mybatis-Plus实现多主键批量保存及更新详情的文章就介绍到这了,更多相关Mybatis-Plus多主键批量保存内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341