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

机械设备企业网站源码张家港阿里网站建设

机械设备企业网站源码,张家港阿里网站建设,短故事网站模板,自适应网站设计案例文章目录 准备工作Mybatis-Plus使用Wrapper自定义SQL注意事项目录结构如下所示domain层Controller层Service层ServiceImplMapper层UserMapper.xml 结果如下所示#xff1a;单表查询条件构造器单表查询#xff0c;Mybatis-Plus使用Wrapper自定义SQL联表查询不用#xff0c;My… 文章目录 准备工作Mybatis-Plus使用Wrapper自定义SQL注意事项目录结构如下所示domain层Controller层Service层ServiceImplMapper层UserMapper.xml 结果如下所示单表查询条件构造器单表查询Mybatis-Plus使用Wrapper自定义SQL联表查询不用Mybatis-Plus的条件构造器时联表查询Mybatis-Plus使用Wrapper自定义SQL总结 简要说明Mybatis-Plus使用Wrapper自定义SQL主要的代码说明详情可以往后看。 假设有三张表这三张表在 SpringBoot整合mybatis-plus-CSDN博客有 的关系如图所示 对应的UserMapper.java的主要代码如下 public interface UserMapper extends BaseMapperUser {// 下面的currentPage, pageSizeuserId, roleName都是Controller层传入或者自己看着填写这里只是说明一下UserMapper.java的参数来源// LambdaQueryWrapperUser lqw new LambdaQueryWrapper();// 动态SQL这是真的方便// lqw.like(StringUtils.isNotEmpty(name), User::getName, name);//lqw.eq(age ! null age ! 0, User::getAge, age);/*** 假设联表查询语句是这样的。* # 如根据用户姓名、年龄查询用户具有的角色列表。* select sr.role_name* from sys_user su* left join sys_user_role sur on su.id sur.user_id* left join sys_role sr on sur.role_id sr.id* where su.name like %张% and su.age 98;QueryWrapper qw new QueryWrapper();// 动态SQLqw.like(StringUtils.isNotEmpty(name), su.name, name);qw.eq(age ! null age ! 0, su.age, age);*//*** 单表查询Mybatis-Plus使用Wrapper自定义SQL* 这个是为了下面联表查询铺垫这个主要是了解* Mybatis-Plus使用Wrapper自定义SQL如何使用*/Select(select * from sys_user ${ew.customSqlSegment})// 这里需要注意了Param(ew)这里面只能是ewListUser getAllWrapperSql(Param(ew) LambdaQueryWrapperUser wrapper);// 使用下面这个方法也行使用Mp内置的枚举类Constants.WRAPPER 这个就是 ew// ListUser getAllWrapperSql(Param(Constants.WRAPPER) LambdaQueryWrapper wrapper);/*** 联表查询Mybatis-Plus使用Wrapper自定义SQL在xml中使用* 根据用户姓名、年龄获取对应用户具有的角色列表*/// 注意这里只能是ewListRoleVo getRoleListByUserNameMulTable(Param(Constants.WRAPPER) QueryWrapper qw);// 像下面这样写也可以// ListRoleVo getRoleListByUserNameMulTable(Param(ew) LambdaQueryWrapper lqw); } 对应的UserMapper.xml如下所示 ?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.ygy.mapper.UserMapper!--联表查询Mybatis-Plus使用Wrapper自定义SQL在xml中使用根据用户姓名、年龄获取对应用户具有的角色列表--select idgetRoleListByUserNameMulTable resultTypecom.ygy.domain.vo.RoleVoselect sr.role_namefrom sys_user suleft join sys_user_role sur on su.id sur.user_idleft join sys_role sr on sur.role_id sr.id ${ew.customSqlSegment}/select /mapper 大概结果如下所示 联表查询Mybatis-Plus使用Wrapper自定义SQL结果如下所示 IDEA控制台中打印的SQL语句为 下面有详细的说明感兴趣的可以往下看。下面有详细的说明感兴趣的可以往下看。下面有详细的说明感兴趣的可以往下看。 准备工作 SpringBoot2.7.10 JDK17 MySQL8…30社区版 Mybatis-Plus3.5.3SpringBoot整合Mybatis-Plus看这篇文章SpringBoot整合mybatis-plus-CSDN博客 这里有说明SpringBoot Mybatis-Plus的整合和下文需要用到的数据库表数据这里需要用到这篇文章中的三张表实现联表查询以及对应的Maven依赖都有说明。直接从这篇文章中导入即可。 Mybatis-Plus使用Wrapper自定义SQL 注意事项 具体看官网条件构造器 | MyBatis-Plus (baomidou.com) 需要mybatis-plus版本 3.0.7 param 参数名要么叫ew,要么加上注解Param(Constants.WRAPPER) 使用${ew.customSqlSegment} 不支持 Wrapper 内的entity生成where语句 对于单表查询操作Mybatis-Plus的Wrapper条件构造器很方便。特别是LambdaQueryWrapper我用着感觉很好用这个动态SQL条件构造还是很好用的。但是在联表查询的时候如果还想用LambdaQueryWrapper这条件构造器就需要使用Mybatis-Plus使用Wrapper自定义SQL了。 目录结构如下所示 可以直接复制下面的代码或者参考SpringBoot整合mybatis-plus-CSDN博客中代码生成器生成对应的文件然后在对应的文件中写代码方法。 domain层 User.java package com.ygy.domain;import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Getter; import lombok.Setter;/*** p* 用户表* /p** author ygy* since 2023-11-05*/ Getter Setter TableName(sys_user) ApiModel(value User对象, description 用户表) public class User implements Serializable {private static final long serialVersionUID 1L;ApiModelProperty(用户ID)TableId(value id, type IdType.AUTO)private Long id;ApiModelProperty(姓名)private String name;ApiModelProperty(年龄)private Integer age;ApiModelProperty(性别0女1男)private Integer sex; } RoleVo.java package com.ygy.domain.vo;import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data;Data ApiModel(value RoleVo对象, description 角色表) public class RoleVo {ApiModelProperty(角色名称)private String RoleName; } Controller层 UserController.java package com.ygy.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ygy.domain.User; import com.ygy.domain.vo.PageVo; import com.ygy.domain.vo.RoleVo; import com.ygy.service.IUserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** p* 用户表 前端控制器* /p*其中 ApiParam(当前页码数)、ApiParam(每页显示条数)、ApiOperation(分页获取用户列表)是swagger的注解* author ygy* since 2023-11-05*/ Api(tags 用户管理模块) // 这个是swagger的注解 RestController RequestMapping(/user) public class UserController {Autowiredprivate IUserService userService;GetMappingApiOperation(单表查询条件构造器)public ListUser getAll(ApiParam(姓名) String name, ApiParam(年龄) Integer age) {// 从这里可以看出单表查询结合LambdaQueryWrapper这条件构造器还是挺好用的// 什么都不用写直接调用MP内置的方法简单方便LambdaQueryWrapperUser lqw new LambdaQueryWrapper();// 动态SQL这是真的方便lqw.like(StringUtils.isNotEmpty(name), User::getName, name);lqw.eq(age ! null age ! 0, User::getAge, age);ListUser userList userService.list(lqw);return userList;}/*** 单表查询Mybatis-Plus使用Wrapper自定义SQL* 这个是为了下面联表查询铺垫这个主要是了解* Mybatis-Plus使用Wrapper自定义SQL如何使用*/GetMapping(/getAllWrapperSql)ApiOperation(单表查询Mybatis-Plus使用Wrapper自定义SQL)public ListUser getAllWrapperSql(ApiParam(姓名) String name, ApiParam(年龄) Integer age) {LambdaQueryWrapperUser lqw new LambdaQueryWrapper();lqw.like(StringUtils.isNotEmpty(name), User::getName, name);lqw.eq(age ! null age ! 0, User::getAge, age);ListUser userList userService.getAllWrapperSql(lqw);return userList;}/*** 联表查询不用Mybatis-Plus的条件构造器时* 根据用户姓名、年龄获取对应用户具有的角色列表*/GetMapping(/getRoleListByUserName)ApiOperation(联表查询不用Mybatis-Plus的条件构造器时)public ListRoleVo getRoleListByUserName(ApiParam(姓名) String name, ApiParam(年龄) Integer age) {ListRoleVo roleVoList userService.getRoleListByUserName(name, age);return roleVoList;}/*** 联表查询Mybatis-Plus使用Wrapper自定义SQL* 根据用户姓名、年龄获取对应用户具有的角色列表*/GetMapping(/getRoleListByUserNameMulTable)ApiOperation(联表查询Mybatis-Plus使用Wrapper自定义SQL)public ListRoleVo getRoleListByUserNameMulTable(ApiParam(姓名) String name, ApiParam(年龄) Integer age) {/*** 假设联表查询语句是这样的。* # 如根据用户姓名、年龄查询用户具有的角色列表。* select sr.role_name* from sys_user su* left join sys_user_role sur on su.id sur.user_id* left join sys_role sr on sur.role_id sr.id* where su.name like %张% and su.age 98;*/QueryWrapper qw new QueryWrapper();// 动态SQLqw.like(StringUtils.isNotEmpty(name), su.name, name);qw.eq(age ! null age ! 0, su.age, age);ListRoleVo roleVoList userService.getRoleListByUserNameMulTable(qw);return roleVoList;} } Service层 IUserService.java package com.ygy.service;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.ygy.domain.User; import com.baomidou.mybatisplus.extension.service.IService; import com.ygy.domain.vo.RoleVo;import java.util.List;/*** p* 用户表 服务类* /p** author ygy* since 2023-11-05*/ public interface IUserService extends IServiceUser {/*** 单表查询Mybatis-Plus使用Wrapper自定义SQL* 这个是为了下面联表查询铺垫这个主要是了解* Mybatis-Plus使用Wrapper自定义SQL如何使用*/ListUser getAllWrapperSql(LambdaQueryWrapperUser lqw);/*** 联表查询不用Mybatis-Plus的条件构造器时* 根据用户姓名、年龄获取对应用户具有的角色列表*/ListRoleVo getRoleListByUserName(String name, Integer age);/*** 联表查询Mybatis-Plus使用Wrapper自定义SQL* 根据用户姓名、年龄获取对应用户具有的角色列表*/ListRoleVo getRoleListByUserNameMulTable(QueryWrapper qw); } ServiceImpl UserServiceImpl.java package com.ygy.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.ygy.domain.User; import com.ygy.domain.vo.RoleVo; import com.ygy.mapper.UserMapper; import com.ygy.service.IUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;/*** p* 用户表 服务实现类* /p** author ygy* since 2023-11-05*/ Service public class UserServiceImpl extends ServiceImplUserMapper, User implements IUserService {Autowiredprivate UserMapper userMapper;/*** 单表查询Mybatis-Plus使用Wrapper自定义SQL* 这个是为了下面联表查询铺垫这个主要是了解* Mybatis-Plus使用Wrapper自定义SQL如何使用*/Overridepublic ListUser getAllWrapperSql(LambdaQueryWrapperUser lqw) {ListUser userList userMapper.getAllWrapperSql(lqw);return userList;}/*** 联表查询不用Mybatis-Plus的条件构造器时* 根据用户姓名、年龄获取对应用户具有的角色列表*/Overridepublic ListRoleVo getRoleListByUserName(String name, Integer age) {ListRoleVo roleVoList userMapper.getRoleListByUserName(name, age);return roleVoList;}/*** 联表查询Mybatis-Plus使用Wrapper自定义SQL* 根据用户姓名、年龄获取对应用户具有的角色列表*/Overridepublic ListRoleVo getRoleListByUserNameMulTable(QueryWrapper qw) {ListRoleVo roleVoList userMapper.getRoleListByUserNameMulTable(qw);return roleVoList;} } Mapper层 UserMapper.java package com.ygy.mapper;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.ygy.domain.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ygy.domain.vo.RoleVo; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select;import java.util.List;/*** p* 用户表 Mapper 接口* /p** author ygy* since 2023-11-05*/ public interface UserMapper extends BaseMapperUser {/*** 单表查询Mybatis-Plus使用Wrapper自定义SQL* 这个是为了下面联表查询铺垫这个主要是了解* Mybatis-Plus使用Wrapper自定义SQL如何使用*/Select(select * from sys_user ${ew.customSqlSegment})// 这里需要注意了Param(ew)这里面只能是ewListUser getAllWrapperSql(Param(ew) LambdaQueryWrapperUser wrapper);// 使用下面这个方法也行使用Mp内置的枚举类Constants.WRAPPER 这个就是 ew// ListUser getAllWrapperSql(Param(Constants.WRAPPER) LambdaQueryWrapper wrapper);/*** 联表查询不用Mybatis-Plus的条件构造器时在xml中使用* 根据用户姓名、年龄获取对应用户具有的角色列表*/ListRoleVo getRoleListByUserName(String name, Integer age);/*** 联表查询Mybatis-Plus使用Wrapper自定义SQL在xml中使用* 根据用户姓名、年龄获取对应用户具有的角色列表*/// 注意这里只能是ewListRoleVo getRoleListByUserNameMulTable(Param(Constants.WRAPPER) QueryWrapper qw);// 像下面这样写也可以// ListRoleVo getRoleListByUserNameMulTable(Param(ew) LambdaQueryWrapper lqw); } UserMapper.xml ?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.ygy.mapper.UserMapper!--联表查询不用Mybatis-Plus的条件构造器时根据用户姓名、年龄获取对应用户具有的角色列表--select idgetRoleListByUserName resultTypecom.ygy.domain.vo.RoleVoselect sr.role_namefrom sys_user suleft join sys_user_role sur on su.id sur.user_idleft join sys_role sr on sur.role_id sr.idwhereif testname ! null and name ! AND su.name like concat(%, #{name}, %)/ifif testage ! null and age ! AND su.age #{age}/if/where/select!--联表查询Mybatis-Plus使用Wrapper自定义SQL在xml中使用根据用户姓名、年龄获取对应用户具有的角色列表--select idgetRoleListByUserNameMulTable resultTypecom.ygy.domain.vo.RoleVoselect sr.role_namefrom sys_user suleft join sys_user_role sur on su.id sur.user_idleft join sys_role sr on sur.role_id sr.id ${ew.customSqlSegment}/select/mapper 结果如下所示 单表查询条件构造器 单表查询条件构造器结果如下所示 IDEA控制台中打印的SQL语句为 单表查询Mybatis-Plus使用Wrapper自定义SQL 单表查询Mybatis-Plus使用Wrapper自定义SQL的结果如下所示 IDEA控制台中打印的SQL语句为 联表查询不用Mybatis-Plus的条件构造器时 联表查询不用Mybatis-Plus的条件构造器时结果如下所示 IDEA控制台中打印的SQL语句为 联表查询Mybatis-Plus使用Wrapper自定义SQL 联表查询Mybatis-Plus使用Wrapper自定义SQL结果如下所示 IDEA控制台中打印的SQL语句为 总结 这么一看还不如像联表查询不用Mybatis-Plus的条件构造器时这里的方法呢可读性也不是很好。不过也有它的用处它的动态SQL强啊类似与select * from sys_user where id in(1,2,3); 语句当然不是单表联表使用或者复杂的SQL查询语句这个或许会方便一些如果使用Mybatis的写法在xml需要使用foreach标签等但是使用这个Mp的自定义SQL可以直接传入选择in方法然后根据对应的要求传入参数即可。
http://www.hkea.cn/news/14594215/

相关文章:

  • 精神文明网站建设内容昆山门户网站
  • 重庆主页网站建设西安关键词快速排名
  • 网站备案期间 权重站长统计
  • 用照片做视频的网站好h5个人网站源码
  • 企业做网站有用吗百度网站下载安装
  • 淄博建网站多少钱随州网站制作价格
  • 网站页头设计个人注册公司的详细步骤
  • 织梦网站怎么做优化移动互联网技术网站
  • 设计图片网站作品设计方案怎么写
  • 网站托管服务 重庆网站内容页面怎么做
  • 免费个人网站建站申请流程外贸网站推广上海
  • 移动建站价格传统网站模版
  • 潍坊网站公司外贸营销型网站建设平台
  • 兰州网站建设企业名录宣传片制作app
  • 手机扁平化网站模版制作购物网站需要多少钱
  • 泰州公司做网站网站建设 李奥贝纳
  • 个人做网站接装修活哪个网站好三类安全员证查询系统
  • 周村网站制作首选公司wordpress免签
  • 网站关键字优化技巧peise网站
  • 邢台专业做移动网站自己开发的app软件怎么申请专利
  • 泰州网站制作方案定制张家港安监站网址
  • 蓝田县住房与城乡建设局网站ps做网站的效果图
  • 页面网站缓存如何做广东智能网站建设质量保障
  • 重庆在线长治网站seo
  • 宁波网站制作服务桂林生活网官网首页
  • 武平县网站建设华为云软件开发平台
  • 自己做网站需要填税表吗免费申请163邮箱
  • 网站项目如何做需求分析报告城乡建设网站证件查询系统
  • 查域名网站企业注册资金100万变更10万
  • 重庆专业网站搭建公司网页界面设计中表单的组成部分有哪些提示信息