温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用springboot实现查询mybatis plus

发布时间:2020-11-05 17:27:34 来源:亿速云 阅读:342 作者:Leah 栏目:开发技术

本篇文章为大家展示了使用springboot实现查询mybatis plus,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

 一:基本配置:

1.仓库依赖

 <repositories>
  <repository>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>

<pluginRepositories>
  <pluginRepository>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </pluginRepository>
</pluginRepositories>

2.springboot框架依赖

   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  
  <!--添加thymeleaf依赖-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>

  <!--mybatis持久层org映射框架-->
  <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.1.tmp</version>
  </dependency>

3.数据库依赖

   <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>

二. 三种查询方式

1.like对象查询 (Dept为数据库表,return index为返回的前端页面)

public String index(
      String name,
      Model model) {
    QueryWrapper<Dept> queryWrapper= new QueryWrapper<>();    
    if (name!=null && name.trim().length()>0){    
      queryWrapper.like("name", name.trim());
    }
    List<Dept> list = deptService.list(queryWrapper);
    model.addAttribute("list",list);
    model.addAttribute("name",name);
    return "index";
  }

1.1 Dao层注解控制台输出sql语句

 @Select("select * from dept where name like #{name}");

2.mybatis注解查询

public String index(
    String name,
    Model model) {
    List<Dept> depts=null;
  if (name!=null && name.trim().length()>0){
    depts = deptService.list2like("%" + name + "%");
  }else{
    depts=deptService.list();
  }
   model.addAttribute("list", depts);
   model.addAttribute("name", name);
   return "index";
}

3.mybatis xml查询

3.1 配置扫描xml文件

 mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml 

3.2定义mapper模板

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="包对应的Dao类">


  <!--
  list2likeXml 方法名
  resultType 返回结果的类型
  -->
  <select id="对应Dao类的方法名l" resultType="com.kede.springbootdemo4dept.entity.Dept">
    select * from dept
    <where>
      <if test="name !=null and name != ''">
        and name like concat('%',#{name},'%')
      </if>
    </where>
  </select>

</mapper>

3.3controller层代码

public String index(
    String name,
    Model model) {
  List<Dept> depts= deptService.list2likeXml(name);
     model.addAttribute("list", depts);
     model.addAttribute("name", name);
     return "index";
}

4.Dao层的方法

public interface DeptDao extends BaseMapper<Dept> {

  //org.apache.ibatis.annotations.Param 类似于springmvc里面的@RequestParam
  //#{name} 和@Param("name") 对应
  @Select("select * from dept where name like #{name}")//sql语句,从部门表搜素相关

  List<Dept> list2like(@Param("name") String name);

  List<Dept> list2likeXml(String name);
}

上述内容就是使用springboot实现查询mybatis plus,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI