SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建

之前搭建了@Select標簽來做SringBoot+Mybatis的集成。這次使用@SelectProvider標簽的方式搭建一次。

一、搭建SpringBoot的項目

  https://start.spring.io/自己配置SpringBoot的項目,點擊“Generate Project”按鈕就可以下載下來一個配置好的SpringBoot項目。

  

?

二、項目結構

  

?

三、項目代碼

  demo代碼實現的是對表數據的一個簡單查詢。

  1、pom中的mave配置

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>    

?

?

?

   2、Controller

package com.example.demo.Controller;import com.example.demo.Service.TeacherService;
import com.example.demo.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TeacherController {@Autowired(required = false)TeacherService userService;@RequestMapping("selectUser")public Teacher getUserOne(String id){Teacher tea = new Teacher();tea.setId(id);Teacher teacher1 = userService.findTeacherById(tea);return teacher1;}@RequestMapping("selectUserByName")public Teacher getUserOne(String id,String name){Teacher tea=new Teacher();tea.setId(id);tea.setName(name);Teacher teacher=userService.findTeacherByName(tea);return teacher;}
}

  

  3、Service

  一個interface接口,一個Impl實現

package com.example.demo.Service;
import com.example.demo.entity.Teacher;public interface TeacherService {Teacher findTeacherById(Teacher user);Teacher findTeacherByName(Teacher user);
}

?

  接口實現:

package com.example.demo.ServiceImpl;import com.example.demo.Mapper.TeacherMapper;
import com.example.demo.Service.TeacherService;
import com.example.demo.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;@Service
public class TeacherServiceImpl implements TeacherService {@Autowired(required = false)TeacherMapper userMapper;@Overridepublic Teacher findTeacherById(Teacher teacher) {return userMapper.findUserById(teacher);}@Overridepublic Teacher findTeacherByName(Teacher teacher) {Map<String,Object> maps=new HashMap<>();maps.put("id",teacher.getId());maps.put("name",teacher.getName());return userMapper.findUserByName(maps);}
}

  ?4、Mapper代碼

package com.example.demo.Mapper;import com.example.demo.entity.Teacher;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import java.util.Map;/*** The interface Teacher mapper.*/
@Mapper
public interface TeacherMapper {/*** The constant returnSql.*/String returnSql="id,name";/*** Find user by id teacher.** @param user the user* @return the teacher*/@SelectProvider(type = UserDaoProvider.class, method = "findTeacherById")Teacher findUserById(Teacher user);/*** Find user by name teacher.** @param map the map* @return the teacher*/@SelectProvider(type = UserDaoProvider.class, method = "findTeacherByName")Teacher findUserByName(Map<String, Object> map);/*** The type User dao provider.*/class UserDaoProvider {/*** Find teacher by id string.** @param teacher the teacher* @return the string*/public String findTeacherById(Teacher teacher) {String sql = "SELECT "+returnSql+" FROM Teacher";if(teacher.getId()!=null){sql += " where id = #{id}";}return sql;}/*** Find teacher by name string.** @param map the map* @return the string*/public String findTeacherByName(Map<String, Object> map) {String name = (String) map.get("name");return new SQL() {{SELECT(returnSql);FROM("Teacher");WHERE("name="+ name);}}.toString();}}
}

    在程序啟動時,會掃描Mapper文件,所以需要在Mapper文件里添加@Mapper注解。

    還可以在main文件中添加@MapperScan()注解:

package com.example.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.demo.Mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

   5、實體類

?

package com.example.demo.entity;public class Teacher {private String id;private String name;public String getId() { return id; }public void setId(String id) { this.id = id; }public String getName() { return name; }public void setName(String name) { this.name = name; }
}

?

  

?

轉載于:https://www.cnblogs.com/Lyh1997/p/10195183.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278560.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278560.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278560.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

程鑫峰:1.23日央行推行負利率政策,倫敦金后市行情解析

程鑫峰&#xff1a;1.23日央行推行負利率政策&#xff0c;倫敦金后市行情解析 QQ截圖20180123153028.png ??盡管美國政府關門鬧劇剛剛結束&#xff0c;但交易員、投資者和策略師對于美元的前景依然不太樂觀。美國貨幣政策對美元的影響力減弱可能是全球通貨再膨脹交易的另一個…

從購買域名到nginx,flask搭建自己的網站

搭建一個只屬于自己的網站? 一、注冊域名&#xff08;可選*&#xff09; 1.注冊阿里云賬號 網址&#xff1a;登錄&#xff08;注冊&#xff09; 2.購買域名&#xff1a;阿里云域名注冊 有一元域名、免費域名等。 購買過程中需要創建信息模板&#xff08;必須完成郵箱真實…

alexa語音實現_如何通過語音刪除Alexa錄音

alexa語音實現Amazon亞馬孫Amazon is rolling out new privacy features today for Alexa. In addition to an educational “privacy hub,” the company lets you delete your stored recordings by voice. But it’s off by default; you’ll need to flip a switch. 亞馬遜…

linux如何查看所有的用戶(user)、用戶組(group)、密碼(password/passwd)

linux如何查看所有的用戶和組信息_百度經驗https://jingyan.baidu.com/article/a681b0de159b093b184346a7.html linux添加用戶、用戶組、密碼_百度經驗https://jingyan.baidu.com/article/335530da8b7e0419cb41c3e5.html 給用戶開通sudo權限 xxx is not in the sudoers file.Th…

angular之兩種路由

安裝angular npm install -g angular/cli ng new myapp ng g component componentName 自帶路由 引入&#xff1a;angular-route.js <div ng-controllerctr1><a href#home>首頁</a> <a href#mine>我的</a> <div ng-view></div><d…

用scrapy框架寫爬蟲

爬蟲可以發送給引擎的兩種請求&#xff1a; # 1、url&#xff1a;# &#xff08;爬蟲&#xff09;yield scrapy.Request -> 引擎 -> 調度器&#xff08;發送給調度器入隊&#xff09; -> 引擎&#xff08;調度器出隊請求于引擎&#xff09;# -> 下載器&#xff08;…

audacity_如何在Audacity中快速編輯多個文件

audacityGot a bunch of files that need to be edited the same way? You can automate the process to save time and effort using Audacity’s Chain feature and modify tons of files at the same time. 有一堆需要以相同方式編輯的文件&#xff1f; 您可以使用Audacity…

通過api管理grafana

1. 生成api key 參考&#xff1a; http://docs.grafana.org/http_api/auth/ 2.點擊添加后&#xff0c;生成了個獲取一個deshboards的api樣例 3.放到linux上運行測試&#xff0c;結果成功返回。 4. 有些api并不支持使用api key 來連接&#xff0c;如下圖中的搜索用戶接口&#x…

NFS服務的配置過程

NFS服務的配置過程服務端:1)安裝nfs和rcp服務yum install nfs-utils rpcbind -y 因為NFS支持的功能多,不同的功能會使用不同的程序來啟動每啟動一個功能就會啟動一些端口來傳輸數據,默認NFS讀完啟動會產生多個進程,多個端口號信息,會隨機使用未被使用的端口重啟又會變化,所以…

vue項目將token存在(vuex)store和localstorage中

文章目錄一、準備工作和token1、準備工作2、介紹token用法二、創建storage&#xff0c;store&#xff0c;request1、src目錄&#xff1a;2、封裝storage&#xff08;可選&#xff09;3、創建store4、創建request三、配置代理&#xff0c;封裝路由router、設置路由守衛&#xff…

安卓手電筒_將價值10美元的手電筒砍入超高亮高級燈中

安卓手電筒If you’re looking for a bright flashlight without paying an arm and a leg this simple hack modifies a cheap $10 flashlight to be as bright as a $95 one. 如果您要尋找一個明亮的手電筒而又不用付胳膊和腿&#xff0c;這個簡單的技巧就可以將便宜的10美元…

初識 scrapy 框架 - 安裝

前面豆子學習了基本的urllib的模塊&#xff0c;通過這個模塊可以寫一些簡單的爬蟲文件。如果要處理大中型的爬蟲項目&#xff0c;urllib就顯得比較low了&#xff0c;這個時候可以使用scrapy框架來實現&#xff0c;很多基本的處理在scrapy里面已經做好了。 首先來安裝一下。推薦…

Vue使用Vuex一步步封裝并使用store

文章目錄一、安裝Vuex依賴二、一步步封裝store1. main.js中全局引入store倉庫&#xff08;下一步創建&#xff09;2. this.$store3. this.$store.state4. this.$store.getters&#xff08;this. $store.state的升級&#xff09;5. this.$store.commit(mutations)6. this.$store…

linux自學(四)之開始centos學習,網絡配置

上一篇&#xff1a;linux自學&#xff08;三&#xff09;之開啟虛擬機 安裝好鏡像之后&#xff0c;重啟之后需要登錄&#xff0c;我這里直接是root賬號直接登錄的&#xff0c;注意&#xff1a;輸入密碼的時候不顯示。 之后輸入ifconfig最常用的命令來查看網卡信息&#xff0c;出…

k8s extender_Windows Home Server的Drive Extender的9種選擇

k8s extenderNow that Microsoft has officially killed off the best part about Windows Home Server what can you do? Here are some alternatives for drive extender that you can use if you want to build a WHS of your own. 既然Microsoft正式取消了Windows Home Se…

為什么element的el-backtop會不管用,來看這里

<template>Scroll down to see the bottom-right button.<el-backtop target".page-component__scroll .el-scrollbar__wrap"></el-backtop> </template>把target指向你要產生“回到頂部”按鈕的組件&#xff0c; 這個組件一定要是產生滾動條…

如何創建一份springboot的docker鏡像

2019獨角獸企業重金招聘Python工程師標準>>> FROM centos:7 ENV JAVA_HOME /usr/java/jdk1.7.0_55 ENV MAC_PUBLISH_PATH /home/app ENV LOG_PATH /var/log ENV PATH $JAVA_HOME/bin:$PATH ENV TIME_ZONE Asia/Shanghai COPY jdk-7u55-linux-x64.rpm /opt/ RUN mkd…

Xamarin.Android 開發中遇到旋轉屏幕錯誤

錯誤信息 : System.NotSupportedException: Unable to find the default constructor on type App5.MyFragment. Please provide the missing constructor. 錯誤圖片&#xff1a; 解決方法&#xff1a;干脆不讓他旋轉屏幕&#xff0c;當下QQ、微信等app都沒有旋轉等功能&#…

原生js打印指定節點元素

很簡單&#xff08;可粘貼至txt文檔后改后綴為html打開看效果&#xff09;&#xff1a; <!doctype html> <html lang"en"> <head><meta charset"utf-8"><title>打印</title><meta name"viewport" conte…

Android社會化分享詳解

前言現如今app市場競爭激烈&#xff0c;做app不會放過任何推廣自己的app的渠道&#xff0c;如果app中沒有社會化分享功能&#xff0c;那真的是OUT了&#xff0c;我們先來看下一些app中的分享界面功能吧。現在主流的分享平臺&#xff0c;一般用的都是微信、QQ、微博&#xff0c;…