mybatis plus的3種查詢方式(小結(jié))
本文是基于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.數(shù)據(jù)庫依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
二. 三種查詢方式
1.like對象查詢 (Dept為數(shù)據(jù)庫表,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='包對應(yīng)的Dao類'> <!-- list2likeXml 方法名 resultType 返回結(jié)果的類型 --> <select 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') 對應(yīng) @Select('select * from dept where name like #{name}')//sql語句,從部門表搜素相關(guān) List<Dept> list2like(@Param('name') String name); List<Dept> list2likeXml(String name);}
到此這篇關(guān)于mybatis plus的3種查詢方式(小結(jié))的文章就介紹到這了,更多相關(guān)mybatis plus 查詢方式內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
