[Spring從零單排-1] Spring-boot + MVC+Mybatis環(huán)境搭建 (Gradle+IDEA)

一、 創(chuàng)建spring-boot項(xiàng)目

前置條件

idea
java環(huán)境
tomcat (解決坑用)
mysql

使用官方初始化工具

1、直接使用默認(rèn)default,也可以搭建私服模板

2、 使用選擇gradle,其他可以保持默認(rèn)

image.png

3、 勾選依賴


image.png
image.png

4、配置好路徑,F(xiàn)inish

image.png

5、 選擇圖上兩項(xiàng),自動(dòng)創(chuàng)建一些目錄,以及使用默認(rèn)gradle wrapper

image.png

6、 如果網(wǎng)速好,此處可以無視,安裝時(shí)候卡在下載gradle這一步

image.png
解決方法

1、手動(dòng)下載對(duì)應(yīng)版本gradle,放到tomcat目錄里面 webapps\ROOT,啟動(dòng)tomcat
2、找到gradle-wrapper.properties文件,修改到本地路徑

image.png

3、重啟idea,右側(cè)邊欄出現(xiàn)Gradle圖標(biāo),說明安裝成功了
image.png

7、點(diǎn)刷新按鈕下載依賴,如果下載不了,可以替換maven倉庫


image.png
image.png

8、到此位置,依賴環(huán)境搭建完成,可以愉快的寫代碼了 —— so easy,媽媽再也......

二、使用Mybatis+MVC

1、數(shù)據(jù)庫建表及填充數(shù)據(jù)


image.png

2、 建立如下目錄結(jié)構(gòu) —— 推薦這種目錄結(jié)構(gòu),畢竟官方包也是這么玩的


image.png

controller – Controller 層
dao – 數(shù)據(jù)操作層 DAO
domain – 實(shí)體類
mapper – 注解方式的配置文件
service – 業(yè)務(wù)邏輯層
Application – 應(yīng)用啟動(dòng)類

3、編寫domain、dao、application、service、controller文件

domian/City.java

package com.example.domain;

import java.io.Serializable;

/**
 * Created by user on 2017/5/5.
 */
public class City implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private String state;
    private String country;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return getId() + "," + getName() + "," + getState() + "," + getCountry();
    }

}

dao/CityDao.java

package com.example.dao;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import com.example.domain.City;

import javax.annotation.Resource;

/**
 * Created by user on 2017/5/5.
 */
@Component
public class CityDao {

    @Resource
    private  SqlSession sqlSession;

    public City selectCityById(long id) {
        return sqlSession.selectOne("selectCityById", id);
    }
}

service/CityService.java

package com.example.service;


import com.example.domain.City;
import com.example.dao.CityDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by user on 2017/5/5.
 */
@Service
public class CityService {

    @Autowired
    private CityDao cityDao;

    public City selectCityById(int id){
        return cityDao.selectCityById(id);
    }
}

controller/DemoController.java

package com.example.controller;

import com.example.domain.City;
import com.example.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by user on 2017/5/5.
 */
@Controller
public class DemoController {

    @Autowired
    private CityService cityService;

    @RequestMapping("/")
    @ResponseBody
    public City index() {
        return cityService.selectCityById(1);
    }
}

DemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

4、mapper我采取配置的方式,因?yàn)橐院罂梢杂霉ぞ呱?[捂臉]

image.png
<?xml version="1.0" encoding="UTF-8" ?>
<!--
       Copyright 2015-2016 the original author or authors.
       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
-->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.CityMapper">
    <select id="selectCityById" resultType="City">
        select * from city where id = #{id}
    </select>
</mapper>

5、在application.properties配置數(shù)據(jù)源

我用mysql5.7需要帶上useSSL防止報(bào)warn

spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username = work
spring.datasource.password = 123456

mybatis.config-location=classpath:mybatis-config.xml

6、 配置mybatis-config.xml,mapper文件在這里指定路徑

<?xml version="1.0" encoding="UTF-8" ?>
<!--
       Copyright 2015-2016 the original author or authors.
       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
-->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.example.domain"/>
    </typeAliases>
    <mappers>
        <mapper resource="com/example/mapper/CityMapper.xml"/>
    </mappers>
</configuration>

三、Run起來

點(diǎn)這個(gè)


image.png

臥槽......


image.png

加依賴
image.png

刷新再run,用自帶的工具測(cè)試,Tools -> Test RESTful Web Service(這個(gè)碉堡,省了postman)


image.png

完美!!!!

四、小節(jié)

  1. 構(gòu)建工具使用了Gradle,擴(kuò)展性強(qiáng),可以滿足項(xiàng)目后期需要。
  2. 整個(gè)構(gòu)建基于最新的組件搭建,選用Spring-Boot,Spring-MVC 和
    Mybatis 也是使用starter來構(gòu)建完成,基本實(shí)現(xiàn)了開箱即用,隨官方更新迭代風(fēng)險(xiǎn)小
  3. 代碼及目錄結(jié)構(gòu)采取規(guī)范形式,方便后續(xù)擴(kuò)展,大型項(xiàng)目,在example下一級(jí)增加子項(xiàng)目目錄即可劃分工程。
  4. mybatis采取xml配置形式,可以利用現(xiàn)有mybatsi-generator工具進(jìn)行代碼生成,方便后續(xù)開發(fā)
  5. 使用@autowired 和 @Resource 方式自動(dòng)注入,充分利用了框架的簡(jiǎn)化優(yōu)勢(shì),開發(fā)更容易

后記:新手第一次搭建這套開發(fā)環(huán)境,踩了好多坑,陸陸續(xù)續(xù)搞了兩天,記錄過程防止遺忘,也希望對(duì)同行新手有幫助~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,327評(píng)論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,996評(píng)論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,316評(píng)論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,406評(píng)論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,128評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,524評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,576評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,759評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,310評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,065評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,249評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,821評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,479評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,909評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,140評(píng)論 1 290
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,984評(píng)論 3 395
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,228評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容