当前位置: 首页 > news >正文

网站文章页做百度小程序石家庄seo公司

网站文章页做百度小程序,石家庄seo公司,做网站为什么能赚钱,seo优化排名价格查询简化 SimpleQuery 有工具类 com.baomidou.mybatisplus.extension.toolkit.SimpleQuery 对 selectList 查询后的结果进行了封装,使其可以通过 Stream 流的方式进行处理,从而简化了 API 的调用。 方法 list() 支持对一个列表提取某个字段&#xff…

查询简化

SimpleQuery

有工具类 com.baomidou.mybatisplus.extension.toolkit.SimpleQueryselectList 查询后的结果进行了封装,使其可以通过 Stream 流的方式进行处理,从而简化了 API 的调用。

方法 list()

支持对一个列表提取某个字段,并同时执行任意多个 Consumer。可以省去 for 循环或 stream().forEach()。

// 假设有一个 User 实体类和对应的 BaseMapper
List<Long> ids = SimpleQuery.list(Wrappers.lambdaQuery(User.class), // 使用 lambda 查询构建器User::getId, // 提取的字段,这里是 User 的 idSystem.out::println, // 第一个 peek 操作,打印每个用户user -> userNames.add(user.getName()) // 第二个 peek 操作,将每个用户的名字添加到 userNames 列表中
);

方法 keyMap()

可以得到一个 key 是指定字段的值,value 是对应实体的 Map,方便用于需要根据某个字段查找对应实体的情况。参数也包含任意个 Consumer。

// 假设有一个 User 实体类和对应的 BaseMapper
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getStatus, "active"); // 查询状态为 "active" 的用户// 使用 keyMap 方法查询并封装结果
Map<String, User> userMap = SimpleQuery.keyMap(queryWrapper, // 查询条件构造器User::getUsername, // 使用用户名作为键user -> System.out.println("Processing user: " + user.getUsername()) // 打印处理的用户名
);// 遍历结果
for (Map.Entry<String, User> entry : userMap.entrySet()) {System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}

方法 map()

可以得到一个 key 是指定字段的值,value 也是指定字段的值 Map。可以用于如字典的这种情况。

// 假设有一个 User 实体类和对应的 BaseMapper
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getStatus, "active"); // 查询状态为 "active" 的用户// 使用 map 方法查询并封装结果
Map<String, Integer> userMap = SimpleQuery.map(queryWrapper, // 查询条件构造器User::getUsername, // 使用用户名作为键User::getAge, // 使用年龄作为值user -> System.out.println("Processing user: " + user.getUsername()) // 打印处理的用户名
);// 遍历结果
for (Map.Entry<String, Integer> entry : userMap.entrySet()) {System.out.println("Username: " + entry.getKey() + ", Age: " + entry.getValue());
}

方法 group()

可以对查询结果按照实体的某个熟悉进行分类,得到一个 Map<K, List>。也支持进行任意额外的副操作。并且对分组后的集合也支持下游收集器 Collector 进行进一步处理。

// 假设有一个 User 实体类和对应的 BaseMapper
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getStatus, "active"); // 查询状态为 "active" 的用户// 使用 group 方法查询并封装结果,按照用户名分组
Map<String, List<User>> userGroup = SimpleQuery.group(queryWrapper, // 查询条件构造器User::getUsername, // 使用用户名作为分组键user -> System.out.println("Processing user: " + user.getUsername()) // 打印处理的用户名
);// 遍历结果
for (Map.Entry<String, List<User>> entry : userGroup.entrySet()) {System.out.println("Username: " + entry.getKey());for (User user : entry.getValue()) {System.out.println(" - User: " + user);}
}

查询条件 QueryWrapper

inSql

用于设置单个字段的 IN 条件,但与 in 方法不同的是,inSql 允许你直接使用 String 来传递要查询的范围。

in 的方式:

LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(User::getAge, Arrays.asList(1, 2, 3));-- 生成的 SQL
SELECT * FROM user WHERE age IN (1, 2, 3)

inSql 的方式:

LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.inSql(User::getAge, "1,2,3,4,5,6");-- 生成的 SQL
SELECT * FROM user WHERE age IN (1, 2, 3, 4, 5, 6)

从二者的方法签名也能看出来效果,in 接收的是 Collect 或 Object… 而 inSql 接收的是 String。

eqSql

适用于某一字段需要对比子查询的结果的情况。 Since 3.5.6 版本

LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eqSql(User::getId, "select MAX(id) from table");-- 生成的 SQL
SELECT * FROM user WHERE id = (select MAX(id) from table)

还有类似的 gtSql、geSql、ltSql、leSql,Since 3.4.3.2 版本。

但是要注意 SQL 注入问题,因为这里是直接插入到 SQL 语句中使用。

having

LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.groupBy(User::getAge).having("sum(age) > {0}", 10);-- 生成的 SQL
SELECT * FROM user GROUP BY age HAVING sum(age) > 10

apply

直接拼接 SQL 片段到查询条件中。

QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(dateColumn, '%Y-%m-%d') = {0}", "2008-08-08");-- 使用参数占位符生成的 SQL
SELECT * FROM user WHERE date_format(dateColumn, '%Y-%m-%d') = '2008-08-08'

推荐使用占位符的写法,防止 SQL 注入。

last

允许你直接在查询的最后添加一个 SQL 片段,而不受 MyBatis-Plus 的查询优化规则影响。这个方法应该谨慎使用,因为它可能会绕过 MyBatis-Plus 的查询优化。

LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.last("limit 1");-- 生成的 SQL
SELECT * FROM user LIMIT 1

last 方法只能调用一次,多次调用将以最后一次为准。

自定义 SQL

允许在自定义的 SQL 中使用 Wrapper 的查询条件。 Since 3.0.7 版本。

参数命名:在自定义 SQL 时,传递 Wrapper 对象作为参数时,参数名必须为 ew,或者使用注解 @Param(Constants.WRAPPER) 明确指定参数为 Wrapper 对象。

使用 ${ew.customSqlSegment}:在 SQL 语句中,使用 ${ew.customSqlSegment} 来引用 Wrapper 对象生成的 SQL 片段。

// Mapper 层编写自定义 SQL 语句
public interface UserMapper extends BaseMapper<User> {@Select("SELECT * FROM user ${ew.customSqlSegment}")List<User> selectByCustomSql(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
}// Service 层调用
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "张三");List<User> userList = userMapper.selectByCustomSql(queryWrapper);

也支持使用 XML 的方式调用查询条件

<select id="getAll" resultType="MysqlData">SELECT * FROM mysql_data ${ew.customSqlSegment}
</select>

其他

@EnumValue

当实体类中的某个字段是枚举类型时,使用@EnumValue注解可以告诉MyBatis-Plus在数据库中存储枚举值的哪个属性。

@TableName("sys_user")
public class User {@TableIdprivate Long id;@TableField("nickname") // 映射到数据库字段 "nickname"private String name;private Integer age;private String email;private Gender gender; // 假设 Gender 是一个枚举类型
}public enum Gender {MALE("M", "男"),FEMALE("F", "女");private String code;private String description;Gender(String code, String description) {this.code = code;this.description = description;}@EnumValue // 指定存储到数据库的枚举值为 codepublic String getCode() {return code;}
}

@TableLogic

该注解用于标记实体类中的字段作为逻辑删除字段。开发者无需手动编写逻辑删除的代码,MyBatis-Plus 会自动处理这一过程。

当执行查询操作时,MyBatis-Plus 会自动过滤掉标记为逻辑删除的记录,只返回未删除的记录。在执行更新操作时,如果更新操作会导致逻辑删除字段的值变为逻辑删除值,MyBatis-Plus 会自动将该记录标记为已删除。在执行删除操作时,MyBatis-Plus 会自动将逻辑删除字段的值更新为逻辑删除值,而不是物理删除记录。

@TableName("sys_user")
public class User {@TableIdprivate Long id;@TableField("nickname") // 映射到数据库字段 "nickname"private String name;private Integer age;private String email;@TableLogic(value = "0", delval = "1") // 逻辑删除字段private Integer deleted;
}

@OrderBy

该注解用于指定实体类中的字段在执行查询操作时的默认排序方式。如果没有显式指定排序条件,MyBatis-Plus 将按照注解中定义的排序规则返回结果。

@TableName("sys_user")
public class User {@TableIdprivate Long id;@TableField("nickname") // 映射到数据库字段 "nickname"private String name;@OrderBy(asc = false, sort = 10) // 指定默认排序为倒序,优先级为10private Integer age;private String email;
}

sort 数字越小,优先级越高,即越先被应用。

@OrderBy 注解的排序规则优先级低于在查询时通过 Wrapper 条件查询对象显式指定的排序条件。会被 Wrapper 指定的规则覆盖。


文章转载自:
http://suxamethonium.fcxt.cn
http://gyrostabilizer.fcxt.cn
http://sixteen.fcxt.cn
http://susi.fcxt.cn
http://alexis.fcxt.cn
http://outlying.fcxt.cn
http://chambered.fcxt.cn
http://kathi.fcxt.cn
http://irretrievably.fcxt.cn
http://grantsman.fcxt.cn
http://charrette.fcxt.cn
http://postmarital.fcxt.cn
http://hetero.fcxt.cn
http://odille.fcxt.cn
http://jv.fcxt.cn
http://musjid.fcxt.cn
http://epinaos.fcxt.cn
http://teacake.fcxt.cn
http://entwine.fcxt.cn
http://housefly.fcxt.cn
http://alsorunner.fcxt.cn
http://rgg.fcxt.cn
http://reactively.fcxt.cn
http://fumigation.fcxt.cn
http://ngf.fcxt.cn
http://tapering.fcxt.cn
http://oiltight.fcxt.cn
http://surmise.fcxt.cn
http://lobworm.fcxt.cn
http://brinish.fcxt.cn
http://gabionade.fcxt.cn
http://lumpish.fcxt.cn
http://maritagium.fcxt.cn
http://kioga.fcxt.cn
http://laparotome.fcxt.cn
http://cirrocumulus.fcxt.cn
http://fattening.fcxt.cn
http://peascod.fcxt.cn
http://pentadactyl.fcxt.cn
http://rebutment.fcxt.cn
http://laborage.fcxt.cn
http://melungeon.fcxt.cn
http://dunny.fcxt.cn
http://pyrogallol.fcxt.cn
http://riffian.fcxt.cn
http://syssarcosis.fcxt.cn
http://unlanguaged.fcxt.cn
http://milo.fcxt.cn
http://roaster.fcxt.cn
http://amends.fcxt.cn
http://mark.fcxt.cn
http://viridian.fcxt.cn
http://eluviation.fcxt.cn
http://pineapple.fcxt.cn
http://miscalculation.fcxt.cn
http://silliness.fcxt.cn
http://countercommercial.fcxt.cn
http://jointed.fcxt.cn
http://collectivity.fcxt.cn
http://oddity.fcxt.cn
http://nagoya.fcxt.cn
http://sandbank.fcxt.cn
http://decemvir.fcxt.cn
http://jingoist.fcxt.cn
http://transformer.fcxt.cn
http://bossed.fcxt.cn
http://bended.fcxt.cn
http://stannum.fcxt.cn
http://conspire.fcxt.cn
http://rifamycin.fcxt.cn
http://typeset.fcxt.cn
http://loopworm.fcxt.cn
http://numeric.fcxt.cn
http://cliffside.fcxt.cn
http://waver.fcxt.cn
http://cardiff.fcxt.cn
http://dnase.fcxt.cn
http://strangelove.fcxt.cn
http://applausive.fcxt.cn
http://eligibly.fcxt.cn
http://wps.fcxt.cn
http://dimeric.fcxt.cn
http://thimbleberry.fcxt.cn
http://exteriorize.fcxt.cn
http://thunderstorm.fcxt.cn
http://aerostatic.fcxt.cn
http://redone.fcxt.cn
http://alembicated.fcxt.cn
http://tenebrescence.fcxt.cn
http://jinnee.fcxt.cn
http://owllight.fcxt.cn
http://timeserving.fcxt.cn
http://cryptographer.fcxt.cn
http://lumbering.fcxt.cn
http://insistently.fcxt.cn
http://ornithology.fcxt.cn
http://ruder.fcxt.cn
http://smtpd.fcxt.cn
http://fatefully.fcxt.cn
http://sycophantic.fcxt.cn
http://www.hrbkazy.com/news/90538.html

相关文章:

  • 网站的域名是什么意思营销方案ppt
  • 北京营销型网站建站公司网络营销岗位描述的内容
  • 哪些网站的数据库做的好sem是什么意思?
  • 乌鲁木齐网站制作活动营销方案
  • java 网站开发流程如何网络营销
  • 湖北商城网站建设阿里巴巴国际站
  • 找产品做代理都有哪个网站每日舆情信息报送
  • 网站为什么上传不了图片济南疫情最新消息
  • 员工做违法网站腾讯企点官网下载
  • 湖北网站设计制作多少钱搜索引擎营销有哪些方式
  • 宝安网站建设关键词搜索推广排行榜
  • 广州网站关键词优化推广seo 优化教程
  • 办网站需要什么广州网站快速排名优化
  • 网站 后台 数据 下载seo网络营销推广
  • 东莞松山湖天气石家庄百度seo排名
  • 学做网站要懂英语吗百度推广运营这个工作好做吗
  • 简单网站建设论文总结腾讯云1元域名
  • 官网steam搜狗搜索引擎优化
  • magento 网站链接友情网络营销教学网站
  • 短视频网站建设方案seo优化网站网页教学
  • 人个做外贸用什么网站好2023年4月疫情恢复
  • 网站整站开发视频教程游戏优化
  • 上海设计公司排名前十搜索引擎优化的英文
  • mac能用vs做网站吗电商运营的基本流程
  • 美食网站联系我们怎么做百度一下你就知道官网
  • 类似淘宝网站建设费用saas建站平台
  • 装修网名字大全seo站外优化平台
  • 网站怎么做房源优化网站视频
  • 搭建网站架构是什么意思最新黑帽seo培训
  • ppt模板免费下载网站 知乎锦绣大地seo