后端开发基础:Mybatis Plus详解

后台-系统设置-扩展变量-手机广告位-内容正文顶部

Mapper类

  写在javashop-core工程中

包名

类名

  Model+Mapper.java

  如:GoodsMapper.java、TradeMapper.java

MappperXml

文件名

  Model+Mapper.xml

  如:GoodsMapper.xml、TradeMapper.xml

MybatisPlus升级指南

MybatisPlus基础

领域模型

  publicclassUser{

  @TableId(type=)

  privateLongid;

  privateStringname;

  privateIntegerage;

  privateStringemail;

  //gettersetter略

  }

Mapper

  publicinterfaceUserMapperextendsBaseMapper{

  }

示例

增加

  @Autowired

  privateUserMapperuserMapper;

  @Test

  publicvoidtestAdd(){

  //构造用户模型

  Useruser=newUser();

  user.setAge(10);

  user.setEmail("kingapex@163.com");

  user.setName("kingapex");

  //调用mapper的insert方法插入数据

  userMapper.insert(user);

  }

删除

  @Test

  publicvoidtestDelete(){

  userMapper.deleteById(1L);

  //构造查询条件,按条件删除

  QueryWrapperwrapper=buildWrapper();

  userMapper.delete(wrapper);

  }

更新

  @Test

  publicvoidtestUpdate(){

  //构造要更新的用户模型

  Useruser=newUser();

  user.setId(2L);

  user.setEmail("kingapex@javashop.cn");

  user.setName("111");

  userMapper.updateById(user);

  }

列表查询

  @Test

  publicvoidtestSelect(){

  //构造查询条件

  QueryWrapperwrapper=buildWrapper();

  //通过mapper查询

  ListuserList=userMapper.selectList(wrapper);

  userList.forEach(System.out::println);

  }

分页查询(返回map)

  @Test

  publicvoidtestPageForMap(){

  //构造查询条件

  QueryWrapperwrapper=buildWrapper();

  //构造分页条件:第一页,页大小为2

  Pagepage=newPage<>(1,2);

  //调用mapper进行分页的Map式查询

  IPage>userIPage=

  userMapper.selectMapsPage(page,wrapper);

  Longtotal=userIPage.getTotal();

  System.out.println(total);

  ListuserList=userIPage.getRecords();

  System.out.println(userList);

  }

分页查询(返回Model)

  @Test

  publicvoidtestPageForModel(){

  //构造查询条件

  QueryWrapperwrapper=buildWrapper();

  //构造分页条件:第一页,页大小为2

  Pagepage=newPage<>(1,2);

  //调用mapper进行分页的Map式查询

  IPageuserIPage=

  userMapper.selectPage(page,wrapper);

  Longtotal=userIPage.getTotal();

  System.out.println(total);

  ListuserList=userIPage.getRecords();

  System.out.println(userList);

  }

构造查询条件

常见条件

  whereage=?andnamelike?orderbyid

嵌套条件

  WHERE(nameLIKE?AND(age=?ORage=?))ORDERBYidASC

  QueryWrapperbuildWrapper(){

  //构造查询条件

  QueryWrapperwrapper=newQueryWrapper<>();

  wrapper.orderByAsc("id");

  //namelike%wf%

  wrapper.like("name","wf");

  wrapper.and(wq->wq.eq("age",19).or().eq("age",18));

  returnwrapper;

  }

嵌套+动态条件

  QueryWrapperbuildWrapper(){

  //构造查询条件

  QueryWrapperwrapper=newQueryWrapper<>();

  wrapper.orderByAsc("id");

  //namelike%wf%

  wrapper.like("name","wf");

  wrapper.and(wq->{

  wq.eq("age",19);

  if(some){

  wq.or().eq("age",18);

  }

  });

  returnwrapper;

  }

自定义sql

传递wapper

  src/main/resources/mapper/UserMapper.xml

  <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.enation.app.javashop.mapper.UserMapper">
  <selectid="customerSqlSegment"resultType="com.enation.app.javashop.mapper.User">
  selectu.*
  fromUSERu
  ${ew.sqlSegment}
  </select></mapper> 

UserMapper

  publicinterfaceUserMapperextendsBaseMapper{

  ListcustomerSqlSegment(@Param("ew")Wrapperew);

  }

UserMapper注解方式(xml与注解方式可以二选一)

  publicinterfaceUserMapperextendsBaseMapper{

  @Select("selectu.*fromUSERu${ew.customSqlSegment}")

  ListcustomerSqlSegment(@Param("ew")Wrapperew);

  }

  注意:这里边${ew.customSqlSegment}不会有sql注入的问题,如果非wapper这种拼接的sql方式应该使用{},否则会有sql注入的安全问题。

Test

  @Test

  publicvoidtestCustomSqlSegment(){

  QueryWrapperew=newQueryWrapper<>();

  ew.like("u.name","Tom");

  Listlist=userMapper.customerSqlSegment(ew);

  list.forEach(System.out::print);

  }

联合查询+传递参数

  src/main/resources/mapper/ArticleMapper.xml

  <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.enation.app.javashop.mapper.ArticleMapper">
  
  <selectid="listDetail"resultType="com.enation.app.javashop.model.pagedata.vo.ArticleDetail">
  selecta.*,ac.namecategory_namefromes_articlealeftjoines_article_categoryacona.category_id=ac.id
  wherearticle_namelike{name}
  </select>
  </mapper> 

UserMapper

  publicinterfaceArticleMapperextendsBaseMapper

{

 

  @Select("selecta.*,ac.namecategory_name"+

  "fromes_articlealeftjoines_article_categoryac"+

  "ona.category_id=ac.id"+

  "wherearticle_namelikeCONCAT('%',{name},'%')")

  ListlistDetail(@Param("name")Stringname);

  }

UserMapper(xml与注解方式可以二选一)

  publicinterfaceArticleMapperextendsBaseMapper

{

 

  @Select("selecta.*,ac.namecategory_name"+

  "fromes_articlealeftjoines_article_categoryac"+

  "ona.category_id=ac.id"+

  "wherearticle_namelikeCONCAT('%',{name},'%')")

  ListlistDetail(@Param("name")Stringname);

  }

  注意:可以使用CONCAT('%',{name},'%')这种方式进行个别符号的拼接

Test

  @Test

  publicvoidarticleTest(){

  ListarticleDetailList=articleMapper.listDetail("%用户注册协议%");

  articleDetailList.forEach(System.out::print);

  }

  注意:这里使用了另外的model:ArticleDetail

动态sql

  mapper

 <!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.enation.app.javashop.mapper.ArticleMapper">
  
  <selectid="listDetail"resultType="com.enation.app.javashop.model.pagedata.vo.ArticleDetail">
  selecta.*,ac.namecategory_namefromes_articlealeftjoines_article_categoryacona.category_id=ac.id
  <where>
  <iftest="params.name!=nullandparams.name!=''">
  article_namelike{params.name}
  </if>
  <iftest="params.list!=null">
  anda.category_idin
  <foreachcollection="params.list"index="index"item="item"open="("separator=","close=")">
  {item}
  </foreach>
  </if>
  </where>
  </select>
  </mapper>

ArticleMapper

  publicinterfaceArticleMapperextendsBaseMapper<Article>{
  List<ArticleDetail>listDetail
  (@Param("params")Mapparams);
  }

ArticleMapper(xml与注解方式可以二选一)

  publicinterfaceArticleMapperextendsBaseMapper<Article>{
  
  @Select("selecta.*,ac.namecategory_namefromes_articlealeftjoines_article_categoryacona.category_id=ac.id"+
  "<where>"+
  "<iftest=\"params.name!=nullandparams.name!=''\">"+
  "article_namelike{params.name}"+
  "</if>"+
  "<iftest=\"params.list!=null\">"+
  "anda.category_idin"+
  "<foreachcollection=\"params.list\"index=\"index\"item=\"item\"open=\"(\"separator=\",\"close=\")\">"+
  "{item}"+
  "</foreach>"+
  "</if>"+
  "</where>")
  List<ArticleDetail>listDetail(@Param("params")Mapparams);
  }

Test

  @Test

  publicvoidarticleTest(){

  Listlist=newArrayList();

  list.add(1);

  list.add(492);

  Mapparams=newHashMap();

  params.put("name","%用户注册协议%");

  params.put("list",list);

  ListarticleDetailList=articleMapper.listDetail(params);

  articleDetailList.forEach(System.out::print);

  }

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

后台-系统设置-扩展变量-手机广告位-内容正文底部
留言与评论(共有 0 条评论)
   
验证码: