本文是基于springboot框架下的查询。
一:基本配置:
1.仓库依赖
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
true
false
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
true
false
2.springboot框架依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
com.baomidou
mybatis-plus-boot-starter
3.3.1.tmp
3.数据库依赖
mysql
mysql-connector-java
二. 三种查询方式
1.like对象查询 (Dept为数据库表,return index为返回的前端页面)
public String index(
String name,
Model model) {
QueryWrapper queryWrapper= new QueryWrapper<>();
if (name!=null && name.trim().length()>0){
queryWrapper.like("name", name.trim());
}
List 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 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模板
select * from dept
and name like concat("%",#{name},"%")
3.3controller层代码
public String index(
String name,
Model model) {
List depts= deptService.list2likeXml(name);
model.addAttribute("list", depts);
model.addAttribute("name", name);
return "index";
}
4.Dao层的方法
public interface DeptDao extends BaseMapper {
//org.apache.ibatis.annotations.Param 类似于springmvc里面的@RequestParam
//#{name} 和@Param("name") 对应
@Select("select * from dept where name like #{name}")//sql语句,从部门表搜素相关
List list2like(@Param("name") String name);
List list2likeXml(String name);
}
到此这篇关于mybatis plus的3种查询方式(小结)的文章就介绍到这了,更多相关mybatis plus 查询方式内容请搜索云海天教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持云海天教程!
原文链接:https://blog.youkuaiyun.com/Mrs_weixinle/article/details/104346263