Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use mybatis-plus in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use mybatis-plus in Java", the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to use mybatis-plus in Java" this article bar.

1. Introduction

MyBatis-Plus is an enhanced version of MyBatis, extending other features on top of MyBatis without changing its basic functionality, and exists to simplify development submission efficiency.

2. Application

1. For single-table operations only, mybatis-plus code is much less than mybatis code, which greatly improves development efficiency.

2, for multi-table operations, mybatis is more recommended, because mybatis-plus method is more difficult to understand, not very convenient to use, not as clear as the logic of writing sql statements yourself

3. mybatis-plus preliminary preparation (the project will be demonstrated with H2 as the default database) 1. Use Spring Initializer to quickly initialize a Spring Boot project

2. Import mybatis-plus dependencies org.springframework.boot spring-boot-starter-parent spring-latest-version org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter Latest Version com.h3database h3 runtime 3. Add relevant configuration # DataSource Configspring: datasource: driver-class-name: org.h3.Driver schema: classpath:db/schema-h3.sql data: classpath:db/data-h3.sql url: jdbc:h3:mem:test username: root password: test4. Add @MapperScan annotation to Spring Boot startup class, scan Mapper folder @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")5. Write entity class and Mapper class//entity@Datapublic class User { private Long id; private String name; private Integer age; private String email;}//Mapperpublic interface UserMapper extends BaseMapper {}6. service inherits IServicepublic interface UserService extends IService7. serviceImpl inherits ServiceImplpublic class UserServiceImpl extends ServiceImpl implements UserService4. sql operation of mybatis-plus (Service layer) 1. Save: Insert//Insert a record boolean save(T entity);//insert (batch) boolean saveBatch(Collection entityList);//insert (batch) boolean saveBatch(Collection entityList, int batchSize);

2. SaveOrUpdate: modify insertion

// TableId annotation: update record exists, insert a record noboolean saveOrUpdate(T entity);//update according to updateWrapper, continue saveOrUpdate(T) method boolean saveOrUpdate(T entity, Wrapper updateWrapper);//batch modification insert boolean saveOrUpdateBatch(Collection entityList, int batchSize);//batch modification insert boolean saveOrUpdateBatch (Collection entityList, int batchSize);

3、Remove: delete//delete records according to entity condition boolean remove(Wrapper queryWrapper);//delete records according to ID boolean removeById(Serializable id);//delete records according to columnMap condition boolean removeByMap(Map columnMap);//delete (delete in bulk according to ID) boolean removeByIds(Collection

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report