美文网首页
[Spring从零单排-1] Spring-boot + MVC

[Spring从零单排-1] Spring-boot + MVC

作者: sirocco | 来源:发表于2017-05-05 10:53 被阅读0次

一、 创建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 方式自动注入,充分利用了框架的简化优势,开发更容易

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

相关文章

网友评论

      本文标题:[Spring从零单排-1] Spring-boot + MVC

      本文链接:https://www.haomeiwen.com/subject/rsujtxtx.html