首页 技术 正文
技术 2022年11月16日
0 收藏 700 点赞 3,012 浏览 5130 个字

最近公司有一个内部比赛(黑客马拉松),报名参加了这么一个赛事,在准备参赛作品的同时,由于参赛服务器需要自己搭建且比赛产生的代码不能外泄的,所以借着这个机会,本地先写了个测试的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

1. 生成模板

可以在官网https://start.spring.io/生成spring boot的模板。如下图

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

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

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

2. 创建Controller

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

3. 运行项目

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

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

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

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

4. 接入mybatis

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, 目录如下

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

添加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

按如下提示运行

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

浏览器输入得到效果:

SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

v博客总结

系统故障常常都是不可预测且难以避免的,因此作为系统设计师的我们,必须要提前预设各种措施,以应对随时可能的系统风险。

v源码地址

https://github.com/toutouge/javademosecond/tree/master/hellospringboot

作  者:请叫我头头哥

出  处:http://www.cnblogs.com/toutou/

关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!

版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信

声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

<!–
#comment_body_3242240 {
display: none;
}
–>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,955
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774