您当前的位置:首页 >> 新能源
新能源

学会了MybatisPlus,代码整合效率提高了10倍

发布时间:2025-10-25

lic Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}粘贴文档

5.变更 application.yml

server: port: 8082 servlet: context-path: /mybatisplus_demo# 数据格式可用spring: datasource: username: root password: 12345678 url: jdbc:mysql://localhost:3306/ssm?allowPublicKeyRetrieval=trueBelluseSSL=false driver-class-name: com.mysql.cj.jdbc.Driver粘贴文档

6.增建 Usermapper 应用程序

引:

(1)因为 mybatis 规定:mapper.xml 文档的英文名字要和应用程序英文名字一样,所以很多人习惯将 Dao 应用程序重新命名为 xxxMapper。

(2)BaseMapper 是 MybatisPlus 移动设备的应用程序,它包含理论上的 CRUD 方法有。

7.重启类添加 @MapperScan 原文

8.的测试

@SpringBootTestpublic class MybatisPlusDemoApplicationTests { @Resource private UserMapper userMapper; @Test void testMybatisPlus() { for (int i = 18; i < 20; i++) { User user = new User("王小波" + i, i); userMapper.insert(user); } }}粘贴文档

9.总结

我们注意到只要继承 MybatisPlus 的BaseMapper,就能完成理论上的梗概改查操作,更为便捷。

4. 理论上梗概改查

1.增设

User user = new User("王小波", 19);userMapper.insert(user);粘贴文档

2.撰稿

根据 id 系统升级数据

int rows = userMapper.updateById(user);if(rows>0){ System.out.println("系统升级事与愿违"); }粘贴文档

3.写下入

根据字段写下入反馈

userMapper.deleteById("152635612");粘贴文档

根据 map 必须写下入反馈

Map param = new HashMap<>();param.put("age", 18);int rows = userMapper.deleteByMap(param);if (rows> 0) { System.out.println("写下入事与愿违!");}粘贴文档

根据 id 子集批量写下入

List ids = Stream.of(110, 112, 113, 115).collect(Collectors.toList());int rows = userMapper.deleteBatchIds(ids);if (rows> 0) { System.out.println("写下入事与愿违!");}粘贴文档

4.检索

根据 id 检索

User user = userMapper.selectById(152382374);粘贴文档

根据 map 必须检索

Map param = new HashMap<>();param.put("age", 18);List userList = userMapper.selectByMap(param);粘贴文档

根据 id 子集批量检索

List ids = Stream.of(110, 112, 113, 115).collect(Collectors.toList());List userList = userMapper.selectBatchIds(ids);粘贴文档5. 结构上装置

MybatisPlus 提供了检索结构上装置和系统升级结构上装置用来分解带有 where 必须的 sql 操作符。

(1)烧录检索必须的结构上装置:

QueryWrapper粘贴文档

常见检索必须:

等于:eq

QueryWrapper userWrapper = new QueryWrapper<>();// 检索英文名字是张三的软件userWrapper.eq("name","张三");List userList = userMapper.selectList(userWrapper);粘贴文档

不等于:ne

QueryWrapper userWrapper = new QueryWrapper<>();userWrapper.ne("name","张三");// 检索英文名字不是张三的软件List userList = userMapper.selectList(userWrapper);粘贴文档

模糊检索:like

QueryWrapper userWrapper = new QueryWrapper<>();// 模糊检索userWrapper.like("name","张");List userList = userMapper.selectList(userWrapper);粘贴文档

每颗:orderByDesc

QueryWrapper userWrapper = new QueryWrapper<>();// 模糊检索并根据 number 倒序userWrapper.like("name","张").orderByDesc("number");List userList = userMapper.selectList(userWrapper);粘贴文档

升序:orderByAsc

QueryWrapper userWrapper = new QueryWrapper<>();// 模糊检索并根据 number 每颗userWrapper.like("name","张").orderByAsc("number");List userList = userMapper.selectList(userWrapper);粘贴文档

其他常见的必须可以去主页查看特别文档,这里便过多赘述:

#in粘贴文档

(2)烧录系统升级必须的结构上装置:

UpdateWrapper粘贴文档

UpdateWrapper 的 where 必须和 QueryWrapper 的一样,只不过须要 set 取值。

UpdateWrapper userWrapper = new UpdateWrapper<>();userWrapper.set("name","王小波").set("age",22) .eq("name","张三");粘贴文档6. 常用 Service

MybatisPlus 除此以外一个常用的应用程序 Iservice 和实现类,烧录了常见的梗概改查等操作。

1.增建 service 和 实现类

UserService

/** * @Desc: * @Author: 政府部门号:知否新技术 * @date: 之前午9:57 2022/5/11 */public interface UserService extends IService {}粘贴文档

UserServiceImpl

/** * @Desc: * @Author: 政府部门号:知否新技术 * @date: 之前午9:57 2022/5/11 */@Servicepublic class UserServiceImpl extends ServiceImpl implements UserService {}粘贴文档

2.的测试

我们注意到该 IService 应用程序烧录了一些常见的方法有,极大地降低了我们的开发设计经济性。

7. 常见原文

1.@TableId

MybatisPlus 则会预设将实体类之前的 id 作为字段。

@TableId 详见示 id 的分解策略,常见的有两种:

(1) 基于检索的自增策略

(2) 用于色彩鲜艳算法策略随机分解

2.@TableName

如果实体类和检索的详见名不完全一致,可以用于这个原文动手同构

例如:

3.@TableField

当详见一般来话说和实体类之前一般来话说名不完全一致时,可以用于这个原文动手同构:

8. 分页

MybatisPlus 实际上烧录了分页应用程序,先用简单可用一下就能实现分页功用。

1.可用类

/** * @Desc: * @Author: 政府部门号:知否新技术 * @date: 之前午9:31 2022/5/11 */@Configuration@MapperScan("com.zhifou.mapper")public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; }}粘贴文档

2.的测试

@Testvoid testMybatisPlus() { int current = 1; int size = 10; Page userPage = new Page<>(current, size); //给予分页数据 List list = userPage.getRecords(); list.forEach(user->{ System.out.println(user); }); Page page = userMapper.selectPage(userPage, null); System.out.println("当前页:" + page.getCurrent()); System.out.println("书页条数:" + page.getSize()); System.out.println("总摘要数:" + page.getTotal()); System.out.println("总页数:" + page.getPages());}粘贴文档9. 文档分解装置

MybatisPlus 可以借助我们系统则会分解 controller、service、dao、model、mapper.xml 等文档,极大地降低了我们的开发设计经济性。

1.应运而生倚赖

com.baomidou mybatis-plus-generator 3.5.1 org.freemarker freemarker粘贴文档

2.文档分解装置工具类

public class CodeGenerator {public static void main(String[] args) {// 连接检索FastAutoGenerator.create("jdbc:mysql://localhost:3306/ssm?allowPublicKeyRetrieval=trueBelluseSSL=falseBelluseUnicode=trueBellcharacterEncoding=UTF-8BellserverTimezone=UTC", "root", "123456") .globalConfig(builder -> { builder.author("知否新技术") // 设立所作 .fileOverride() // 构成已分解文档 // 设立小时表小时 .dateType(DateType.ONLY_DATE) .outputDir("D:\WorkSpace\idea\mybatisplus_demo\src\main\java"); // 指定输出附录 }) .packageConfig(builder -> { builder.parent("com.zhifou") // 设立父包名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\WorkSpace\idea\mybatisplus_demo\src\main\resources\mapper")); // 设立mapperXml分解路径 }) .strategyConfig(builder -> { builder.addInclude("t_user") // 设立须要分解的详见名 .addTablePrefix("t_"); // 设立过滤详见前 // 增设数据,系统则会为创始小时赋取值 IFill createFill = new Column("created_date", FieldFill.INSERT); IFill updateFill = new Column("updated_date", FieldFill.UPDATE); builder.entityBuilder() // 设立id一般来话说 .idType(IdType.ASSIGN_ID) // 开启 Lombok .enableLombok() // 开启连续设立方式而 .enableChainModel() // 驼峰重新命名方式而 .naming(NamingStrategy.underline_to_camel) .columnNaming(NamingStrategy.underline_to_camel) // 系统则会为创始小时、变更小时赋取值 .addTableFills(createFill).addTableFills(updateFill) // 逻辑学写下入字段 .logicDeleteColumnName("is_deleted"); // Restful 风格 builder.controllerBuilder().enableRestStyle(); // 去除 Service 前缀的 I builder.serviceBuilder().formatServiceFileName("%sService"); // mapper 设立 builder.mapperBuilder() .enableBaseResultMap() .enableBaseColumnList(); }) // 固定 .templateEngine(new FreemarkerTemplateEngine()) // 用于Freemarker增压器codice_,预设的是Velocity增压器codice_ .execute(); }}粘贴文档

关键点:

(1)可用检索连接反馈。

系统则会分解文档须要连接检索

(2)指定输出附录,这里单独设立你项目的附录,到时候不必赋取值粘贴了。

(3)设立父包名。

(4)设立详见名

然后右键调试,文档就则会系统则会分解。

10. application.yml 可用# MybatisPlusmybatis-plus: global-config: db-config: column-underline: true # 驼峰基本概念 logic-delete-field: isDeleted # 全局逻辑学写下入的实体字段名 logic-delete-value: 1 # 逻辑学已写下入取值(预设为 1) logic-not-delete-value: 0 # 逻辑学未写下入取值(预设为 0) db-type: mysql id-type: assign_id # id策略 table-prefix: t_ # 可用详见的预设前缀 mapper-locations: classpath*:/mapper/**Mapper.xml # mapper 文档右边 type-aliases-package: com.zhifou.entity # 实体类别名 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 摘要:打印sql 操作符粘贴文档11. 非常简单文档元数据: 提取码: 9un7 粘贴文档12. 遇到的壁上

1.传参为 0 时,检索操作符回退。

例如传递的 age 为 0,检索就则会回退

select id,name,age,sex from user age = #{age} 粘贴文档

因素:判断 int 前提为空只要 !=null 就行了,如果加上 type != '',0 则会被变为 null。

2.MybatisPlus 系统升级字段为 null 不甘心

解决办法:

@TableField(updateStrategy = FieldStrategy.IGNORED)private String name;粘贴文档

该原文则会忽略为空的判断

所作:知否新技术元数据:

直肠手术后吃什么好
宝宝积食
肩膀酸疼

上一篇: 作家谷传民发文警告大衣哥:了事我的版税打给我,否则只能法庭见了

下一篇: 何炅有多不待见杜海涛?嘲讽他得学给人颁奖,台下也很少进行碰触

友情链接