当前位置:网站首页>Use tkmapper to add, delete, modify and query
Use tkmapper to add, delete, modify and query
2022-07-16 06:06:00 【Kuo-Teng】
Use tkMapper Add, delete, modify, etc
0. tkMapper introduce
tkMapperIt's just one.MyBatisplug-in unit , Is inMyBatisProvides a lot of tools based on , Make development simple , Improve development efficiency .- Introduce dependencies :
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${tk.mybatis.version}</version> </dependency> - Create an entity class that matches the database table , Such as :
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class StudentInfo { @Id private Long id; /** * full name */ private String name; /** * cell-phone number */ private String mobile; /** * Student number */ private String studentId; /** * Class number */ private String classId; /** * Creation time */ private Date ctime; /** * Update time */ private Date mtime; /** * Delete logo 0: Not delete 1: Delete */ private Integer deleted_status; } - establish
mapperInterface :public interface StudentInfoMapper<StudentInfo> extends Mapper<StudentInfo>, MySqlMapper<StudentInfo>, BatchMapper<StudentInfo>, GroupMapper<StudentInfo> { } - Next, I will briefly introduce several commonly used
tkMapperHow to add, delete, modify, and search , There are many things that have not been introduced , You can movetkMapperOfficial documents for more detailed information ~
1. insert data
1.1. insert
Use insert, If the value of a certain item of data is null, Will be inserted directly into the corresponding column in the database table null, It is not recommended to use .
StudentInfo info = StudentInfo.builder()
.name("Zhangsan")
.mobile("12312341234")
.build();
studentInfoMapper.insert(info);
For example, the above writing , The other fields of this record inserted into the database will be null;
1.2. insertSelective
Use insertSelective, If the value of a certain item of data is null, The default value set by the database table will be directly inserted into the corresponding column in the database table , Recommended .
StudentInfo info = StudentInfo.builder()
.name("Zhangsan")
.mobile("12312341234")
.build();
studentInfoMapper.insertSelective(info);
For example, the above writing , The other fields of this record inserted into the database will be the default values of the database table ;
1.3. insertList
Use insertList, You can insert multiple records into the database table at one time .
List<StudentInfo> infoList = XXXX;
studentInfoMapper.insertList(infoList);
2. Query data
2.1. selectAll
Use selectAll, Directly query all data .
List<StudentInfo> infoList = studentInfoMapper.selectAll();
2.2 selectByPrimaryKey
Use selectByPrimaryKey, Query according to the primary key .
StudentInfo info = studentInfoMapper.selectByPrimaryKey(15);
2.3. Use selectByExample / selectOneByExample Query conditions
See another article :tkMapper The use of Weekend Join conditions to query conditions
2.4. Paging query
See another article :MyBatis The use of PageHelper The plug-in performs paging queries
3. Update data
3.1. updateByExampleSelective
If we want to update the student information according to the student number , Then we can use the following methods :
Weekend<StudentInfo> weekend = Weekend.of(StudentInfo.class);
WeekendCriteria<StudentInfo, Object> weekendCriteria = weekend.weekendCriteria();
weekendCriteria.andEqualTo(StudentInfo::getStudentId, studentId);
weekendCriteria.andEqualTo(StudentInfo::getDeletedStatus, 0);
StudentInfo info = studentInfoMapper.selectOneByExample(weekend);
if (Objects.isNull(info)) {
throw new Here you can customize the exception throw ;
}
StudentInfo infoNeedToUpdate = Data that needs to be updated ;
studentInfoMapper.updateByExampleSelective(infoNeedToUpdate, weekend);
Check first , Ensure that the data to be updated exists , Update again ;
4. Delete data
Generally in business , Physical deletion is prohibited , All of our commonly used logical deletions are often just updates deletedStatus Status as “ deleted ” that will do , The method of updating data should be used here , I won't repeat ;
边栏推荐
- 网络通信安全部分笔记一
- ES6 -- Interview Questions
- MSF基础
- The technology once selected in the top meeting completed the commercialization of ant chain and launched the copyright AI computing engine
- 网络通信安全部分笔记二
- 内网渗透笔记——注册表自启动与msi提权
- 消息转发机制--拯救你的程序崩溃
- unity实验-重力撞墙
- PHP docking Alipay web payment -tp5.1 framework
- For some problems encountered in using crontab, errors are reported /var/spool/cron: permission denied and bash: /usr/bin/chattr: permission denied
猜你喜欢

Unity experiment - simulating the motion of stars in the solar system

Mysql 主从服务器配置实验 centos7

网络通信安全部分笔记二
Graphic and image programming practice course report

NFTScan 开发者平台推出多链 NFT 数据 Pro API 服务

内网渗透笔记——二层发现

ES6 -- arrow function

MSF Foundation

Es6--string (string)

Network security emergency response - common tools
随机推荐
Notes 2 of network communication security
Idea send email
North tour project notes
Math object in JS
PHP docking wechat payment native TP5 framework
关于 Visual Studio 2022的安装与使用
欧拉Talk | 开发者社区体验坦白局7月14日19:30约起
文件上传-解析漏洞
For some problems encountered in using crontab, errors are reported /var/spool/cron: permission denied and bash: /usr/bin/chattr: permission denied
内网渗透笔记——:)一个笑脸
40.js -- the same name identifier promotion problem
NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
unity实验-模拟太阳系星体运动
sniffer Pro對ARP協議的分析、捕獲與模擬攻擊
MySQL-函数-字符串函数/数值函数/日期函数/流程控制函数
Tree structure tool -treeutil use
Network security emergency response terminal detection and response technology
网络通信安全部分笔记一
Euler talk | developer community experience bureau starts at about 19:30 on July 14
MySQL索引简介 - InnoDB和MyISAM索引模型