spring boot + gradle + mybatis


使用intelliJ創建 spring boot + gradle + mybatis站點

 

Spring boot作為快速入門是不錯的選擇,現在似乎沒有看到大家寫過spring boot + gradle + mybatis在intellij下的入門文章,碰巧做.Net的同學問到,我想我也可以寫這樣一篇。
入門Java的人最大的問題是步驟繁瑣,不知道如何開始,之前我也建議過我的一個小弟用BlueJ去學Java,可是這個東西學得他很迷茫,根本無法獲得成就感,不知道能搞出什么有意思的東西,那我們就來點正式的吧,看看怎么從零開始建一個你自己的Java網站。

軟件安裝

作為入門版,當然我們要從軟件安裝開始咯。
要安裝的軟件有

JDK安裝很簡單,下載最新版,按提示安裝即可。
IntelliJ IDEA的安裝我想也是不必說了,如果這個軟件安裝也不會,那么學編程的路,可路漫漫其修遠兮了。
Gradle也是需要安裝的,在windows下,可以用scoop來安裝。
要安裝Scoop,打開windows的PowerShell,輸入

set-executionpolicy remotesigned -s cu

然后使用這條命令安裝Scoop:

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

詳細可以參考:http://scoop.sh/

Mac下面,可以使用Homebrew安裝,只需要一條命令:

brew update && brew install gradle

創建項目

打開intelliJ, 注意,這軟件需要注冊,但現在是有在線激活的服務器的,反正,你自己可以想辦法的 。
點擊Create New Project

Create New Project

選擇Spring Initializr

給你的項目填寫一點信息,Group和Artifact你隨便填(填的格式不對的話,IntelliJ會提示你的),Type選Gradle Project,Packaging選War,然后點下一步。

項目信息

分別在Web下面選擇Web,Template下選擇Freemarker,SQL下面選擇MyBatis,選擇后的依賴會出現在最右邊,如圖所示,我就選擇了這三個:

選擇依賴

然后點擊下一步,就會出現確認的畫面,直接點擊Finish按鈕。

在接下來這個畫面里,需要gradle的路徑,可以用這樣的方式把它找出來。
新建一個文件,叫build.gradle,寫入如下的內容

task getHomeDir << {
    println gradle.gradleHomeDir
}

然后運行

gradle getHomeDir

最后,使用這條命令就能獲得路徑:

gradle getHomeDir

如下圖:

gradle homedir

得到了gradle路徑,填入到intelliJ IDEA中。

其余的設置跟我一樣就行,然后點擊OK。
這時候,你會進到下一個畫面,這是左上方顯示的內容:

這時候,你需要做的是耐心的等待,也許你還需要一個VPN才能完成下載依賴,這是你的第一個挑戰,這是網絡的原因,我很難幫到你。
不過你可以改改build.gradle文件來加速maven依賴的下載,改成這樣:

這部分的下載速度能得到很大的提高,還是得感謝馬首富啊。

總之,所有的內容加載完成后,你會看到這樣的界面:

你看到的畫面可能跟我的不一樣,因為我多開了一些窗口,但項目的結構就是這樣的。

數據庫的創建

在你的mysql里,新加一個數據庫,就叫 life_master吧,然后加入這樣一張表:

create table life_master.users ( id int(10) auto_increment primary key, name varchar(45) not null, password varchar(20) not null );

再加入兩條數據:

INSERT INTO users VALUES (1,'Tom','no_pass'); INSERT INTO users VALUES (2,'Jerry','no_pass');

雙擊打開application.properties,加入下面內容:

spring.datasource.url=jdbc:mysql://192.168.88.3:3306/life_master spring.datasource.username=root spring.datasource.password=p@ssw0rd spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

以上內容根據你的實際情況修改,我的mysql放在虛擬機的192.168.88.3上面,用戶名和密碼都以你的實際情況填寫。

開始編碼

好了,你是否開始在疑惑,你經歷了重重困難,現在弄了個什么東西出來?
別急,我們先加點代碼進去。
在com.onefew.springboot上右鍵,選擇New->Java Class

然后給他取個名字,比如叫HomeController,核心內容:

@Controller @RequestMapping("/") public class HomeController { @Autowired UserDao userDao; @GetMapping("/") public String index(Model model){ model.addAttribute("name","world"); return "home"; } @GetMapping("/{id}") public String findById(Model model, @PathVariable(value = "id") int id){ User u = userDao.findById(id); model.addAttribute("name",u.getName()); return "home"; } }

新增User的Entity,名字就叫User,內容:

public class User { private int id; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

新建UserDao的接口,內容:

@Mapper public interface UserDao { @Select("SELECT * FROM users where id = #{id}") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "password", column = "password") }) User findById(@Param("id") int id); }

在templates目錄下面,右鍵新建文件home.ftl,內容如下:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh"> <head> <title>我的第一個Spring Boot 程序</title> </head> <body> Hello ${name} </body> </html>

修改一下build.gradle,強dependencies下面,增加

compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'

測試運行

測試之前,先暫時注釋掉build.gradle內的這個內容:

providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')

在右上角點小箭頭,點edit configurations

新增Spring boot,如圖所示

如果一切順利的話,你點擊右上部的綠色小箭頭,就能運行起來了
窗口內會輸出spring相關的信息

這時候,打開瀏覽器,輸出 http://127.0.0.1:8080
忙活了半天,現在是見證奇跡的時候到了:

http://127.0.0.1:8080看到 hello world,這是我們HomeController中index內的內容。
http://127.0.0.1:8080/1 和http://127.0.0.1:8080/2 是讀取到我們mysql中保存的人物的名字。

到這里,編碼內容也基本告一段落。

tomcat部署

之前提到過tomcat,但這個是可選的,如果你安裝了tomcat,那如何在tomcat里調試呢?
點擊菜單里的Run->EditConfigurations,在彈出的菜單的左上角點擊加號,然后選擇tomcat server->local,做如下圖中的配置:

注意deployment標簽內的設置

完成以后,就可以從tomcat中調試了。

結語

這是入門篇,用了大量的截圖,對初學者還是有一些挑戰,但是熟悉以后,這一切都非常的順理成章。
本篇的內容不但涵蓋了建立項目的基礎知識,還涉及到了freemarker和mybatis,雖然只是點到即止,但是從這里開始擴展,相信能寫出有點成就感的程序來。

本文中涉及到的源碼地址:https://github.com/syler/Fun/tree/master/demo-spring-boot-1few

小春微信
本文地址:http://www.cnblogs.com/asis/p/spring-boot-freemarker-mybatis-for-beginner.html
https://1few.com/spring-boot-freemarker-mybatis-for-beginner/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM