[Spring从零单排-1] Spring-boot + MVC+Mybatis环境搭建 (Gradle+IDEA)

一、 创建spring-boot项目

前置条件

idea
java环境
tomcat (解决坑用)
mysql

使用官方初始化工具

1、直接使用默认default,也可以搭建私服模板

2、 使用选择gradle,其他可以保持默认

image.png

3、 勾选依赖


image.png
image.png

4、配置好路径,Finish

image.png

5、 选择图上两项,自动创建一些目录,以及使用默认gradle wrapper

image.png

6、 如果网速好,此处可以无视,安装时候卡在下载gradle这一步

image.png
解决方法

1、手动下载对应版本gradle,放到tomcat目录里面 webapps\ROOT,启动tomcat
2、找到gradle-wrapper.properties文件,修改到本地路径

image.png

3、重启idea,右侧边栏出现Gradle图标,说明安装成功了
image.png

7、点刷新按钮下载依赖,如果下载不了,可以替换maven仓库


image.png
image.png

8、到此位置,依赖环境搭建完成,可以愉快的写代码了 —— so easy,妈妈再也......

二、使用Mybatis+MVC

1、数据库建表及填充数据


image.png

2、 建立如下目录结构 —— 推荐这种目录结构,毕竟官方包也是这么玩的


image.png

controller – Controller 层
dao – 数据操作层 DAO
domain – 实体类
mapper – 注解方式的配置文件
service – 业务逻辑层
Application – 应用启动类

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我采取配置的方式,因为以后可以用工具生成 [捂脸]

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配置数据源

我用mysql5.7需要带上useSSL防止报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起来

点这个


image.png

卧槽......


image.png

加依赖
image.png

刷新再run,用自带的工具测试,Tools -> Test RESTful Web Service(这个碉堡,省了postman)


image.png

完美!!!!

四、小节

  1. 构建工具使用了Gradle,扩展性强,可以满足项目后期需要。
  2. 整个构建基于最新的组件搭建,选用Spring-Boot,Spring-MVC 和
    Mybatis 也是使用starter来构建完成,基本实现了开箱即用,随官方更新迭代风险小
  3. 代码及目录结构采取规范形式,方便后续扩展,大型项目,在example下一级增加子项目目录即可划分工程。
  4. mybatis采取xml配置形式,可以利用现有mybatsi-generator工具进行代码生成,方便后续开发
  5. 使用@autowired 和 @Resource 方式自动注入,充分利用了框架的简化优势,开发更容易

后记:新手第一次搭建这套开发环境,踩了好多坑,陆陆续续搞了两天,记录过程防止遗忘,也希望对同行新手有帮助~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容