SpringBoot入门教程(一)详解intellij idea搭建SpringBoot
最近公司有一个内部比赛(黑客马拉松),报名参加了这么一个赛事,在准备参赛作品的同时,由于参赛服务器需要自己搭建且比赛产生的代码不能外泄的,所以借着这个机会,本地先写了个测试的demo,来把tomcat部署相关的知识从0到1重新捋一遍。就当备忘录了。
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
vSpring Boot概念
从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用。简便起见,该框架也提供了命令行界面,它可以用来运行和测试Boot应用。框架的发布版本,包括集成的CLI(命令行界面),可以在Spring仓库中手动下载和安装。
- 创建独立的Spring应用程序
- 嵌入的Tomcat,无需部署WAR文件
- 简化Maven配置
- 自动配置Spring
- 提供生产就绪型功能,如指标,健康检查和外部配置
- 绝对没有代码生成并且对XML也没有配置要求
v搭建Spring Boot
可以在官网https://start.spring.io/生成spring boot的模板。如下图

然后用idea导入生成的模板,导入有疑问的可以看我另外一篇文章


添加注解 @ComponentScan(注解详情点这里) 然后运行

在看到"Compilation completed successfully in 3s 676ms"消息之后,打开任意浏览器,输入 http://localhost:8080/index 即可查看效果,如下图

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。
在项目对象模型pom.xml中插入mybatis的配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
创建数据库以及user表
use zuche;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`age` int(10) NOT NULL,
`phone` bigint NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
insert into users values(1,'赵',23,158,'3658561548@qq.com');
insert into users values(2,'钱',27,136,'3658561548@126.com');
insert into users values(3,'孙',31,159,'3658561548@163.com');
insert into users values(4,'李',35,130,'3658561548@sina.com'
分别创建三个包,分别是dao/pojo/service, 目录如下

添加User:
package com.athm.pojo; /**
* Created by toutou on 2018/9/15.
*/
public class User {
private int id;
private String username;
private Integer age;
private Integer phone;
private String email; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Integer getPhone() {
return phone;
} public void setPhone(Integer phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
}
添加UserMapper:
package com.athm.dao; import com.athm.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import java.util.List; /**
* Created by toutou on 2018/9/15.
*/
@Mapper
public interface UserMapper {
@Select("SELECT id,username,age,phone,email FROM USERS WHERE AGE=#{age}")
List<User> getUser(int age);
}
添加UserService:
package com.athm.service; import com.athm.pojo.User; import java.util.List; /**
* Created by toutou on 2018/9/15.
*/
public interface UserService {
List<User> getUser(int age);
}
添加UserServiceImpl
package com.athm.service; import com.athm.dao.UserMapper;
import com.athm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; /**
* Created by toutou on 2018/9/15.
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper; @Override
public List<User> getUser(int age){
return userMapper.getUser(age);
}
}
controller添加API方法
package com.athm.controller; import com.athm.pojo.User;
import com.athm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by toutou on 2018/9/15.
*/
@RestController
public class IndexController {
@Autowired
UserService userService;
@GetMapping("/show")
public List<User> getUser(int age){
return userService.getUser(age);
} @RequestMapping("/index")
public Map<String, String> Index(){
Map map = new HashMap<String, String>();
map.put("北京","北方城市");
map.put("深圳","南方城市");
return map;
}
}
修改租车ZucheApplication
package com.athm.zuche; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@ComponentScan(basePackages = {"com.athm.controller","com.athm.service"})
@MapperScan(basePackages = {"com.athm.dao"})
public class ZucheApplication { public static void main(String[] args) {
SpringApplication.run(ZucheApplication.class, args);
}
}
添加数据库连接相关配置,application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/zuche
spring.datasource.username=toutou
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
按如下提示运行

浏览器输入得到效果:

v博客总结
系统故障常常都是不可预测且难以避免的,因此作为系统设计师的我们,必须要提前预设各种措施,以应对随时可能的系统风险。
v源码地址
https://github.com/toutouge/javademosecond/tree/master/hellospringboot
作 者:请叫我头头哥
出 处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
SpringBoot入门教程(一)详解intellij idea搭建SpringBoot的更多相关文章
- 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...
- 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...
- Ubuntu 16.04安装Oracle 11gR2入门教程图文详解
概述 Ubuntu版本:ubuntu-16.04.3-desktop-amd64 Oracle版本:linux.x64_11gR2_database ------------------------- ...
- php单元测试入门教程phpunit详解
本文档提供了一些phpunit官方教程没有提到的信息,帮助初学者快速了解php单元测试,在phpunit官网提供了详细的中文教程,可选多种格式下载 phpunit官网地址:https://phpuni ...
- Git入门教程,详解Git文件的四大状态
大家好,欢迎来到周一git专题. git clone 在上一篇文章当中我们聊了怎么在github当中创建一个属于自己的项目(repository),简称repo.除了建立自己的repo之外,我们更多的 ...
- springboot入门系列(一):简单搭建springboot项目
Spring Boot 简单介绍 Spring Boot 本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架的应用程序.也就是说,它并不是用来替代S ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- SpringBoot入门教程(四)MyBatis generator 注解方式和xml方式
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...
- SpringBoot入门教程(三)通过properties实现多个数据库环境自动切换配置
前面的文章已经介绍了CentOS部署SpringBoot项目从0到1的详细过程,包括Linux安装ftp.Tomcat以及Java jdk的全部过程.这篇文章主要介绍关于springboot如何通过多 ...
随机推荐
- TortoiseSVN--clearup清理失败解决办法
工作中经常遇到update.commit 失败导致冲突问题,需要用clear up来清除问题,个别异常情况导致clear up失败,进入死循环!可以使用sqlite3.exe清理一下wc.db文件的队 ...
- 短网址API
http://tao.tf/open/ API简介 API允许第三方自由调用URL缩短,基于text/json/jsonp/js模式,支持post.get提交. 支持缩短网址: 淘宝网(*.taoba ...
- [Ubuntu]Firefox书签Ubuntu与Windows同步
Ubuntu默认使用Firefox国际版.其他平台访问官网下载到的都是中国版,而国际版和中国版使用两套账号体系,相互之间无法同步,导致Ubuntu的Firefox无法和其他平台的Firefox同步书签 ...
- javascript 数据类型 -- 检测
一.前言 在上一篇博文中 Javascript 数据类型 -- 分类 中,我们梳理了 javascript 的基本类型和引用类型,并提到了一些冷知识.大概的知识框架如下: 这篇博文就讲一下在写代码的过 ...
- android 第三次作业
android studio音乐播放器 一.实现功能: 1.读取本地SD中的所有音频文件 2.歌单列表展示,并显示音频具体信息 3.进度条显示当前播放进度,可滑动加速 4.点击歌单进行播放 5.实现暂 ...
- 五、JAVA反射、线程
第五节:Java反射.线程 线程 1.进程:进程是程序的基本执行实体,进程是线程的容器. 线程:被称为轻量进程,是程序执行流的最小单元.线程是进程中的一个实 ...
- 微信小程序开发---各代码文件简介
根据上一文,已建立QuickStart 项目,该项目系本人毕设部分内容,所以记录以便以后查阅 开发小程序就必须了解小程序项目目录结构和文件作用,接下来就根据我现在自学得到的知识把这些记录下来. 一.目 ...
- windows10下Kafka环境搭建
内容小白,包含JDK+Zookeeper+Kafka三部分.JDK:1) 安装包:Java SE Development Kit 9.0.1 下载地址:http://www.oracle ...
- layui layer弹框中表格的显示
场景描述:点击iframe里面的一个按钮,需要在父级弹出一个弹框表格. 问题描述:这个弹框的分页不能正常显示,如果把layer.open前面的parent去掉,就可以正常显示. 代码展示: paren ...
- 请输入一个大于7的整数,输出小于k并且至少满足下面2个条件中的1个条件的所有正整数
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 22:06 * @description ...