SpringBoot 集成Mybatis

文章目錄

  • 一、創建SpringBoot項目
  • 二、添加Mybatis相關依賴
  • 三、數據源配置
  • 四、創建事務的模型實體類
  • 五、創建和數據庫交互聯系的映射關系類
  • 六、創建業務接口和實現類
  • 七、創建控制器類
  • 八、請求驗證

一、創建SpringBoot項目

如何創建詳見:IDEA 創建 SpringBoot 項目


二、添加Mybatis相關依賴

以前開發Web項目我們都知道要想把數據添加到數據庫,不僅必須要數據庫的驅動程序,還要有各種各樣的配置文件,像java Bean配置,數據源配置,對象和數據庫字段的映射配置等等。使用SpringBoot開發,我們只需要加入依賴文件就可以了,SpringBoot已經都幫我配置好了。配置如下圖所示:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.1</version>
</dependency>

三、數據源配置

在application.properties中配置數據庫連接的相關信息:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:18103/db_test?characterEncoding=GBK
spring.datasource.username=root
spring.datasource.password=root

四、創建事務的模型實體類

編程是利用面向對象的思想把自然界中的事物抽象成模型,利用模型來解決實際中的問題。如下圖:

package com.springboottest.bean;public class StudentBean {private int id;private String name;public StudentBean() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

注:這里的字段名稱與數據庫表字段名稱一致。


五、創建和數據庫交互聯系的映射關系類

這個類主要是和數據進行交互聯系的,需要配置好實體類和數據庫字段的映射關系。由于SpringBoot已經做了大量的工作,我們只需要做好相關注解就可以使用了。如下圖所示:

package com.springboottest.sql.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface StudentMapper {@Select("select * from tb_student where name=#{name}")StudentBean getStudentInfoByName(String name);
}

@Mapper 表明該類是一個Mapper接口,使用@Select@Insert等注解我們可以直接在類中書寫sql語句來實現我們的目的。


六、創建業務接口和實現類

我們在接口類里定義要實現的業務功能接口,在它的實現類里實現接口。接口類如下圖:

package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;public interface StudentService {StudentBean getStudentInfoByName(String name);
}

實現類如下圖:

package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentMapper studentMapper;@Override@Transactionalpublic StudentBean getStudentInfoByName(String name) {return studentMapper.getStudentInfoByName(name);}
}

@Service注解表明它是一個服務類Bean,可以被SpringBoot識別使用,相當于以前在xml里配置的bean。


七、創建控制器類

Web項目的請求經過映射找到控制器類里對應的方法,然后再實現完業務返回響應信息。如下圖:

package com.springboottest.controller;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.MySQLProcessor;
import com.springboottest.sql.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/mysql")
public class SqlController {@Autowiredprivate StudentService studentService;@RequestMapping(value = "/student")public String studentSelect(@RequestParam String name){StudentBean bean = studentService.getStudentInfoByName(name);if(bean != null){return "Name = " + bean.getName();} else {return "null";}}
}

八、請求驗證

請求地址:http://localhost:8991/mysql/student?name=tom
在這里插入圖片描述



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

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

相關文章

C++primer 第 3 章 字符串、向量和數組 3.1 命名空間的using聲明 3.2標準庫類型string

引言 除了第2章介紹的內置類型之外,C語言還定義了 -個內容豐富的抽象數據類型庫。其中,string和 vector是兩種最重耍的標準庫類型&#xff0c;前者支持可變長字符串&#xff0c;后者則 表示可變長的集合。還有…種標準庫類型是迭代器&#xff0c;它是string和vector的配套類型…

ClickHouse 四舍五入函數

文章目錄一、round(x[,N])二、floor(x[,N])三、ceil(x[,N]),ceiling(x[,N])四、trunc(x[, N]), truncate(x[, N])一、round(x[,N]) 說明&#xff1a;將值取整到指定的小數位數&#xff0c;該函數按順序返回最近的數字。 語法&#xff1a; round(expression [, decimal_place…

codeforces 59A-C語言解題報告

59A題目網址 題目解析 1.輸入字符串,如果大寫字母最多,則全部輸出為大寫;如果小寫字母多或大小寫字母一樣多,則全部輸出為小寫 舉例: 輸入: maTRIx 輸出: matrix 2.使用a,b兩個變量去記錄大小寫字母的數量 代碼 #include<stdio.h> #include<stdlib.h> #includ…

C++primer 第 3 章 字符串、向量和數組 3 . 3 標準庫類型vector

標準庫類型vector表示對象的集合&#xff0c;其中所有對象的類型都相同。集合中的每個對象都有一個與之對應的索引&#xff0c;索引用于訪問對象。因為vector"容納著”其他對象&#xff0c;所以它也常被稱作容器(container).第 II部將對容器進行更為詳細的介紹。 要想使用…

SpringBoot AOP切面實現

文章目錄一、AOP簡介二、AOP體系與概念三、AOP實例1、創建SpringBoot工程2、添加依賴3、AOP相關注解3.1、Aspect3.2、Pointcut3.2.1、execution()3.2.2、annotation()3.3、Around3.4、Before3.5、After3.6、AfterReturning3.7、AfterThrowing一、AOP簡介 AOP&#xff08;Aspec…

英語口語-文章朗讀Week8 Friday

文章 It is a phenomenon that people are losing trust in each other in today’s society. Some people become selfish,and for interest, they are likely to betray their colleagues,friends, and even their relatives. They tend to cater to those who can benefit …

C++primer 第 3 章 字符串、向量和數組 3 . 4 迭代器介紹

3.4迭代器介紹 我們已經知道可以使用下標運算符來訪問string對象的字符或vector對象的元素&#xff0c;還有另外一種更通用的機制也可以實現同樣的目的&#xff0c;這就是迭代器&#xff08;iterator&#xff09;。在第II部分中將要介紹&#xff0c;除了vector之外&#xff0c…

ClickHouse 函數

文章目錄一、日期函數1、時間或日期截取函數&#xff08;返回非日期&#xff09;2、時間或日期截取函數&#xff08;返回日期&#xff09;3、日期或時間日期生成函數二、類型轉化類函數1、精度保留&#xff08;非四舍五入&#xff09;2、字符串轉化為整數&#xff08;非整數的字…

英語口語-文章朗讀Week9 TuesDay

朗讀文章 People living in ancient times had no alternative but to do housework manually. They fire the wood when they cook,they hand wash clothes with hands; they sweep the floor with brooms. Now, modern inventions come as a great relief to people. We co…

SpringBoot @Value注解

目錄一、非配置文件注入1、注入普通字符串2、注入JAVA系統變量3、注入表達式4、注入其他Bean屬性5、注入文件資源6、注入URL資源二、通過配置文件注入1、注入普通字符串2、注入基本類型3、注入數組類型4、注入List類型5、注入Map類型一、非配置文件注入 1、注入普通字符串 直…

C++primer 第 3 章 字符串、向量和數組 3 . 5 數組

3.5數組 數組是一種類似于標準庫類型vector&#xff08;參見3.3節&#xff0c;第86頁&#xff09;的數據結構&#xff0c;但是在性能和靈活性的權衡上又與vector有所不同。與vector相似的地方是&#xff0c;數組也是存放類型相同的對象的容器&#xff0c;這些對象本身沒有名字…

codeforces 122A-C語言解題報告

122A題目網址 題目解析 1.輸入數字(在1000以內),若能被4,7幸運數整除或只含4,7則輸出YES,否則輸出NO 舉例: 輸入: 107 輸出: NO 2.解題關鍵: 1)使用列舉法,把所有符合的幸運數列出來(int number[]) 1—2 2–224 3–22*28 24814個 2)若n是幸運數中的一個或n%幸運數0,則為YES…

SpringBoot @Value給靜態變量注入值

文章目錄一、簡介二、Value給靜態變量注入值方案一&#xff1a;set()方法設置方案二&#xff1a;PostConstruct注解修飾的方法中進行賦值三、總結一、簡介 SpringBoot 中給普通變量注入值只需在變量上添加 Value 注解即可。 application.properties 配置文件有如下配置&#…

C++primer 第 4 章 表達式 4.1基礎 4 . 2 算術運算符 4 .3 邏輯和關系運算符 4 . 4 賦值運算符 4 .5 遞增和遞減運算符 4.6成員訪問運算符

表達式由一個或多個運算對象(operand)組成&#xff0c;對表達式求值將得到一個結果(result)字面值和變量是最簡單的表達式(expression),其結果就是字面值和變量的值。把一個運算符(operator)和一個或多個運算對象組合起來可以生成較復雜的表達式 4.1基礎 有幾個基礎概念對表達…

codeforces 266B-C語言解題報告

266B題目網址 題目解析 輸入n,t,排隊情況s,輸出第t次循環后,排隊情況 舉例: 輸入: 5 1 BGGBG 輸出: GBGGB 2.輸入的n代表排隊的人數,t代表整個循環t次之后再輸出結果 3.注意點: 使用while()大循環去控制t次的循環,使用for()內層循環去遍歷整個字符串 如果if(s[j]‘B’&…

Nginx Location配置詳解

目錄一、語法二、匹配順序三、root 與 alias 的區別四、server 和 location 中的 root一、語法 Location 是 Nginx 中一個非常核心的配置&#xff0c;關于Location&#xff0c;舉個簡單的配置例子&#xff1a; server {listen 80;server_name 10.0.7.115;location / {root /d…

英語口語-文章朗讀Week9 Wednesday

英語文章 Birds of the same species flock together&#xff0c; People tend to look for someone like themselves to be friends. But having the same interests is not the only standard when we are seeking friends. In most cases, especially for adults, people l…

C++primer 第 4 章 表達式 4.7條件運算符 4.8位運算符 4.9 sizeof運算符 4.10逗號運算符 4.11類型轉換 4 . 1 2 運算符優先級表

4.7條件運算符 條件運算符(?&#xff1a;)允許我們把簡單的if else邏輯嵌入到單個表達式當中&#xff0c;條件運算符按照如下形式使用&#xff1a;cond ? expr1 : expr2;其中cond是判斷條件的表達式&#xff0c;而expr1和expr2是兩個類型相同或可能轉換為某個公共類型的表達…

Git 之 git tag標簽使用

目錄一、簡介二、本地tag操作1、創建tag標簽&#xff08;1&#xff09;創建輕量標簽&#xff08;2&#xff09;創建附注標簽2、查看tag標簽&#xff08;1&#xff09;查看標簽列表&#xff08;2&#xff09;查看標簽提交信息&#xff08;3&#xff09;在提交歷史中查看標簽3、刪…

codeforces 110A-C語言解題報告

110A題目網址 題目解析 1.輸入一個數字,如果數字中包含的4,7的數量是4或7的倍數,則輸出YES,否則輸出NO 舉例: 輸入: 40047 輸出: NO 2.注意點: 1)由于數字很長,所以使用long long int類型,使用scanf("%lld",&n)接收輸入 2)整型轉字符串,使用sprintf(字符串,“…