您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用springboot實現(xiàn)查詢mybatis plus,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
一:基本配置:
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 id="對應(yīng)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") 對應(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); }
上述內(nèi)容就是使用springboot實現(xiàn)查詢mybatis plus,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。