idea 搭建 SpringBoot 整合 mybatis
編譯器:IDEA2018.2.3
環境:win10,jdk1.8,maven3.4
資料庫:mysql 5.7
備註:截圖較大,如果看不清,可以在圖片上右鍵=》在新標籤頁中開啟 檢視高清大圖哦╮(╯▽╰)╭
一、開啟IDEA新建專案
1. 如果你是第一次使用IDEA,那麼你需要配置你本地的maven,點選右下角 Configure,如已配置請忽略此步驟
在下拉框中選擇setting,然後如下圖操作,選擇自己本地的maven路徑與maven配置檔案
點選OK
2.新建專案
點選Create New Project 後,彈出如下介面,選擇Spring Initializer,然後可以使用編譯器自帶的JDK,也可以點選New,新建並使用自己本地目錄下的JDK環境
當然你也可以選擇Maven,使用Maven搭建自己的環境,但相信我,前者更為便捷
完成上述步驟,選擇JDK之後,點選next,如下圖
這裡會提示我們輸入一些專案資訊,那麼作為初學者,顯然我們沒有必要去較勁,請直接next,之後如下圖
這裡會為你準備許多開發時你需要用到的元件供你挑選,你儘管挑選你可能會用到的元件,然後打勾✔,編譯器會在幫你建立專案時,在pom檔案中替你寫好這些元件需要用到的jar包,很貼心,有點小感動
如果你只是構建一個SpringBoot,你可以什麼都不選直接跳過這一步
由於後期我們要整合mybatis,所以我們勾選mybatis
由於我們的資料庫是5.7,那麼我們要勾選mysql
勾選完成後點選next,如下圖
此處提示我們輸入一些工程資訊,那麼,作為初學者,點選next就好,不要在意這些細節...
點選之後效果如圖,請點選右下角Enable Auto-Import ,允許編譯器在你改變pom檔案後自動匯入包,另外,左側顯示的三處不必要的檔案和資料夾可以刪除,如圖所示
完成上述步驟之後,專案結構及pom檔案如下圖
至此一個SpringBoot專案構建完成,我們可以編寫一個小小的demo來測試SpringBoot
3.SpringBoot 測試
首先在pom檔案中新增如下依賴(非常重要的一個依賴)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
由於我們暫時沒有實體類與jdbc連線,所以我們必須要將pom檔案中有關mysql與mybatis的pom依賴註釋掉,如圖
在demo包下,新建controller包,並新建一個類GirlFriendController,程式設計師有物件真的很容易啊,隨手就能new一個...程式碼如下
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GirlFriendController { @RequestMapping("/getgirlfriend") public String getGirlFriend(){ return "No way ..."; } }
如圖
然後我們找到編譯器為我們生成的主類,或者叫入口類,然後點選執行main方法,如圖
專案成功啟動後,我們在瀏覽器輸入 http://localhost:8080/getgirlfriend ,就能返回結果(此處不需要輸入專案名稱)
至此,SpringBoot框架搭建成功,下一步就是整合mybatis
4.整合mybatis
將我們之前註釋掉的pom檔案中的依賴放開,將注意力轉至mysql資料庫與mybatis上
首先,使用navcat開啟mysql資料庫並建立一張表girlfriend
我們插入一條測試資料
女神艾莉絲,19歲好吧,各位紳士,建表及測試資料指令碼如下
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for girlfriend -- ---------------------------- DROP TABLE IF EXISTS `girlfriend`; CREATE TABLE `girlfriend`( `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of girlfriend -- ---------------------------- INSERT INTO `girlfriend` VALUES ('1', 'Alice', '19'); SET FOREIGN_KEY_CHECKS = 1;
好的,那麼表資料準備完成,下一步準備根據mysql的表實現mybatis相關內容
我們使用工具來生成
工具下載地址 https://pan.baidu.com/s/1RvwKlsmpKJQ_PJkuNjiPdw
下載後解壓,解壓後進入 Mybatis\mybatis-generator-core-1.3.2\lib 目錄,如圖所示
點選進入src目錄,刪除src目錄下的全部檔案(這是上次使用產生的實體類與xml,我們不需要)
編輯 generatorConfig.xml
編輯完成generatorConfig.xml後,開啟啟動命令.txt,複製其中第一行或第二行命令,反正都一樣...
然後點選cmd.exe
然後輸入命令,如圖,執行後提示成功資訊
開啟cmd.exe同目錄下的src目錄,我們會發現下面多了一些東西,如圖
沒錯,他們就是這款工具幫我們生成的實體類,dao類,與xml檔案
接下來我們在專案目錄中新建相應的包、資料夾,並將工具幫我們生成的類與檔案拷貝至IDEA新建的包或資料夾中
拷貝完成之後的專案結構如下圖所示
然後,我們刪除IDEA幫我們建立的 application.properties ,新建 application.yml,如圖
配置程式碼如下
mybatis: typeAliasesPackage: com.xdd.entity mapperLocations: classpath:mapping/*.xml spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver 我們新建一個service包與service類,如圖
程式碼如下
package com.example.demo.service; import com.example.demo.dao.GirlfriendMapper; import com.example.demo.entity.Girlfriend; import org.springframework.beans.factory.annotation.Autowired; @Service public class GirlFriendService { @Autowired private GirlfriendMapper girlfriendMapper; public Girlfriend getGirlFriendById(String id){ return girlfriendMapper.selectByPrimaryKey(id); } }
然後我們再改造一下 GirlFriendController ,如圖
程式碼如下
package com.example.demo.controller; import com.example.demo.entity.Girlfriend; import com.example.demo.service.GirlFriendService; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GirlFriendController { @Autowired private GirlFriendService girlFriendService; @RequestMapping("/getgirlfriend") public Girlfriend getGirlFriend(@Param("id") String id){ Girlfriend girlfriend = girlFriendService.getGirlFriendById(id); return girlfriend; } }
這時我們看到注入的 girlFriendService 在報錯,我們開啟編譯器 file -》Project Structure -》Facets -》Spring ,然後將Spring(demo) 直接右鍵刪除,確定,報錯就解決了
然後我們再編輯入口類 DemoApplication ,新增掃描路徑,程式碼如下
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.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
至此大功告成,我們啟動專案,在瀏覽器中輸入 http://localhost:8080/getgirlfriend?id=1 ,就在此時,不料報錯。。。。。。
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_25] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_25] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_25] at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0_25]
百度才知道這是mysql時區設定問題啊,美帝太壞了...
開啟mysql命令列輸入
show variables like '%time_zone%' set global time_zone='+8:00';
如圖 ,搞定
再次在瀏覽器中輸入 http://localhost:8080/getgirlfriend?id=1 ,效果如圖
啊哈~大功告成
至此SpringBoot+mybatis框架搭建完成,希望大家多多點贊多多評論
純手打,也希望轉載能註明出處,感激不盡
由於本人實在困得不行...所以刪除、新增與修改的重任,交給各位紳士....good night o(* ̄▽ ̄*)ブ