mybatis plus的3种查询方式(小结)
本文是基于springboot框架下的查询。
一:基本配置:
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模板
<?xml version="1.0" encoding="UTF-8"?> <!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); }
到此这篇关于mybatis plus的3种查询方式(小结)的文章就介绍到这了,更多相关mybatis plus 查询方式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)