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

手表网站上没有价格成都制作网页设计

手表网站上没有价格,成都制作网页设计,小制作小发明手工图片,ss网站模板免费下载MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。以下是 MyBatis-Plus 中常用复杂查询(如 LIMIT、ORDER BY、GROUP BY、HAVING、LIKE 等)的引例: 1. 环境准备…

MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。以下是 MyBatis-Plus 中常用复杂查询(如 LIMITORDER BYGROUP BYHAVINGLIKE 等)的引例:

1. 环境准备

首先,确保你已经在项目中添加了 MyBatis-Plus 的依赖。以 Maven 为例:

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version>
</dependency>

2. 实体类和 Mapper 接口

假设我们有一个 User 实体类和对应的 UserMapper 接口:

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("user")
public class User {@TableIdprivate Long id;private String name;private Integer age;private String email;
}
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserMapper extends BaseMapper<User> {
}

3. 常用复杂查询示例

3.1 LIMIT 查询

LIMIT 用于限制查询结果的数量。在 MyBatis-Plus 中,可以使用 Page 类来实现类似 LIMIT 的功能。

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> getUsersWithLimit(int pageNum, int pageSize) {Page<User> page = new Page<>(pageNum, pageSize);IPage<User> userPage = userMapper.selectPage(page, null);return userPage.getRecords();}
}
3.2 ORDER BY 查询

ORDER BY 用于对查询结果进行排序。在 MyBatis-Plus 中,可以使用 QueryWrapper 来实现排序。

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> getUsersOrderByAgeDesc() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.orderByDesc("age");return userMapper.selectList(wrapper);}
}
3.3 GROUP BY 和 HAVING 查询

GROUP BY 用于对查询结果进行分组,HAVING 用于筛选分组后的结果。在 MyBatis-Plus 中,可以使用 QueryWrapper 结合 groupBy 和 having 方法来实现。

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
import java.util.Map;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<Map<String, Object>> getUsersGroupByAgeHavingCount() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.select("age", "COUNT(*) as count").groupBy("age").having("COUNT(*) > 1");return userMapper.selectMaps(wrapper);}
}
3.4 LIKE 查询

LIKE 用于模糊查询。在 MyBatis-Plus 中,可以使用 QueryWrapper 的 like 方法来实现。

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> getUsersLikeName(String keyword) {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.like("name", keyword);return userMapper.selectList(wrapper);}
}

4. 测试代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import java.util.List;
import java.util.Map;@SpringBootApplication
public class Application implements CommandLineRunner {@Autowiredprivate UserService userService;public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Overridepublic void run(String... args) throws Exception {// LIMIT 查询List<User> usersWithLimit = userService.getUsersWithLimit(1, 10);System.out.println("LIMIT 查询结果:" + usersWithLimit);// ORDER BY 查询List<User> usersOrderByAgeDesc = userService.getUsersOrderByAgeDesc();System.out.println("ORDER BY 查询结果:" + usersOrderByAgeDesc);// GROUP BY 和 HAVING 查询List<Map<String, Object>> usersGroupByAgeHavingCount = userService.getUsersGroupByAgeHavingCount();System.out.println("GROUP BY 和 HAVING 查询结果:" + usersGroupByAgeHavingCount);// LIKE 查询List<User> usersLikeName = userService.getUsersLikeName("张");System.out.println("LIKE 查询结果:" + usersLikeName);}
}

以上示例展示了 MyBatis-Plus 中常用复杂查询的使用方法,你可以根据实际需求进行调整。

http://www.yayakq.cn/news/707388/

相关文章:

  • 自己建网站数据怎么做seo优化师培训
  • 餐饮品牌设计网站百度seo关键词排名技术
  • 商城类网站模板建筑网片厂家货源平台
  • 网站搜索框用ps怎么做上海市虹口市容建设公司网站
  • 辽宁鞍山网站建设公司公众号 上传wordpress
  • 内网 做 网站一个公司能备案多个网站吗
  • 建网站需要哪些文件夹做网站需要准备哪些
  • 触摸屏网站如何做做一个手机网站多少钱
  • 淘客cms建站室内装饰网站模板
  • 鄂州网站建设哪家专业网络域名多少钱一年
  • 如何搭建个人博客网站罗湖高端网站设计
  • 网站服务器ipv6网站开发培训费多少钱
  • 网站meta网页描述引流推广什么意思
  • 网站介绍的ppt怎么做服装网站建设任务表
  • 网站编辑软件有哪些一级a做爰片图片免费观看网站
  • 南冒网站建设制作推广公司wordpress 百度主题
  • 用腾讯云做网站wordpress 加分类
  • 住房和城乡建设部网站村镇建设找人建设网站
  • 免费网站建设企业邯郸营销网站建设公司
  • 无锡市住房和城乡建设部网站网站403错误
  • 淘宝客手机网站wordpress文章行距
  • 商务网站建设与规划华硕固件做网站6
  • 自媒体网站建设论文房产网站排名
  • 四川住房和城乡建设厅网站设置网站人数
  • 恩做网站动态页面好深圳坪山站
  • 怎么做网站域名指向台州卫浴网站建设
  • 网上作业网站怎么做的html5博客网站源码
  • 图书馆网站建设的作用短期网页设计师培训
  • 汕头建站模板如何做购物网站的后台
  • 建设网站 关于竣工结算的期限企业级网站欣赏