springboot专题学习之hello world

spring boot使用自动化配置的方式,使得可以很方便、快捷得构建一个spring web应用。作为学习spring boot 的第一课,还是先基于spring boot构建一个简单的包含控制层、服务层以及数据层的hello world应用。
首先确保本机上安装了mysql,本应用会直接访问mysql自带的数据库world。
mysql下载地址:www.mysql.com/downloads/
world数据库:

city表结构:

系统要求:

(1)Intellij Idea 2016.03
注册码传送门:idea.lanyus.com/
(2)Mysql 5.7
(3)Java 1.7以上

构建指南:

(1)在Intellij Idea里面新建HelloWorld project,并在pom.xml文件中添加mysql-connector以及mybatis依赖。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

(2)配置/resources/application.properties数据库参数

spring.datasource.url = jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=UTF-8&autocommit=false&autoReconnect=true&useSSL=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver

(3)根据表city的结构创建city对象

public class City {
    private int id;
    private String name;
    private String countryCode;
    private String district;
    private int population;

    public City(int id ,String name, String countryCode,String district,int population) {
        this.id = id;
        this.name = name;
        this.countryCode=countryCode;
        this.district = district;
        this.population = population;
    }
    
    //setter & getter
}

(4)创建控制层CityController

@RestController
public class CityController {
    @Autowired
    private CityService cityService;

    @RequestMapping(value="/city/{cityName}",method= RequestMethod.GET)
    public City gitCity(@PathVariable("cityName") String cityName) {
        return cityService.getCityByCityName(cityName);
    }
}

(5)创建服务层接口CityService 以及实现类CityServiceImpl
CityService:

public interface CityService {
    /**
     * get City by its name
     * @param cityName
     * @return
     */
    City getCityByCityName(String cityName);
}

CityServiceImpl:

@Service
public class CityServiceImpl implements CityService{

    @Autowired
    private CityMapper cityMapper;

    @Override
    public City getCityByCityName(String cityName) {
        return cityMapper.findCityByCityName(cityName);
    }
}

(6)创建CityMapper,用于读写数据库表City

@Mapper
public interface CityMapper {
    @Select("Select * from city where name=#{cityName}")
    City findCityByCityName(String cityName);
}

运行结果

运行主application,在浏览器里面输入http://localhost:8080/city/shanghai
结果显示如下:

结果显示.png

代码地址:https://github.com/yhguodu/helloworld

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,288评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,695评论 19 139
  • 《Spring Boot开发:从0到1》 大纲结构v2.0 第一部分Spring Boot基础 第1章 Sprin...
    光剑书架上的书阅读 11,088评论 1 70
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,840评论 18 399
  • 不知道这是第几次因为睿睿吵架了。其实我也明白,她是最无辜的那一个,可内心那股执拗的劲就是过不去。 我老公有一个舅舅...
    古月xu阅读 236评论 1 0

友情链接更多精彩内容