編譯器:IDEA2018.2.3java
環境:win10,jdk1.8,maven3.4mysql
數據庫:mysql 5.7程序員
備註:截圖較大,若是看不清,能夠在圖片上右鍵=》在新標籤頁中打開 查看高清大圖哦╮(╯▽╰)╭web
1、打開IDEA新建項目spring
1. 若是你是第一次使用IDEA,那麼你須要配置你本地的maven,點擊右下角 Configure,如已配置請忽略此步驟sql
在下拉框中選擇setting,而後以下圖操做,選擇本身本地的maven路徑與maven配置文件數據庫
點擊OKapache
2.新建項目瀏覽器
點擊Create New Project 後,彈出以下界面,選擇Spring Initializer,而後可使用編譯器自帶的JDK,也能夠點擊New,新建並使用本身本地目錄下的JDK環境mybatis
固然你也能夠選擇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(* ̄▽ ̄*)ブ