注:本文讲述的全部代码都可在微信公众号“最高权限比特流”中回复4获取
SpringBoot是一种微服务框架。
何为微服务框架?
在解释微服务框架前,咱们须要先拆分一下SpringBoot这个单词。Spring做为java程序猿的咱们,应当是颇为熟悉了。Spring意为春天,程序猿的春天。其强大的IOC容器,为咱们的开发提供了便捷。而在开发过程当中,咱们一般使用SSM框架整合,即经典的MVC模型。
使用过SSM开发的小伙伴必定知道,SSM整合须要配置各类各样的XML,虽然不用刻意去记忆,可是很繁琐,若是配置文件有问题,就会出现各类稀奇古怪的问题。
因此有了boot,提供快速的应用开发。使用Springboot,咱们能够省去繁琐的配置文件,只须要简单的开箱便可使用,配合Maven使用更佳。
如今再来讲微服务框架。微服务并无肯定的定义,咱们只须要知道Springboot是由一个个模块(组件)组合而成的便可,它具备极强的灵活性。html
首先要声明一点,开发工具全凭我的习惯,不管是idea仍是eclipse均可以,这点没必要过度纠结。只是对于我而言,IDEA用起来更为习惯。
好了,废话很少说,开始正式配置。
1.建立Maven项目
Maven用于构建项目。Gradle也能够。
流程以下:File->new->project
选择Maven中的quickstart,以下图所示。
而后一路next输入相关的信息。最后点击finish,等待Maven项目的构建。
删除无用的包,建立缺乏的包,构建后的目录结构以下图所示。
前端
1.添加构建Springboot所须要的依赖,因为咱们是使用Maven构建的项目,因此打开pom.xml,在xml结构根目录中添加parent结点。java
<parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.0.6.RELEASE</version> </parent>
这段是直接声明springboot以及相关全部依赖的版本,这样就不会因各包版本冲突而致使的错误。
2.接着添加springboot的mvc依赖,以及前端thymeleaf的依赖。mysql
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> //这里注意,必定要引入springboot的thymeleaf,而不是直接引thymeleaf
3.编写sprinboot的启动类MainApplication,因为springboot直接内置了Tomcat以及jetty,就无需咱们手动配置Tomcat了。web
@SpringBootApplication public class MainApplication { public static void main(String[] args) throws Exception { SpringApplication.run(MainApplication.class,args); } }
4.建立controller类SampleControllerredis
@Controller @RequestMapping("/demo") public class SampleController { @RequestMapping("/thymeleaf") public String thymeleaf(Model model) { model.addAttribute("name","roobtyan"); return "hello"; } }
5.在resources目录中新建application.properties,即Springboot配置文件
配置thymeleafspring
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html
6.在resources目录中建立模板目录template,并建立html页面:sql
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>hello</title> </head> <body> <p th:text="'hello:'+${name}"></p> </body> </html>
7.启动MainApplication,在浏览器中输入localhost:8080/demo/thymeleaf,若是输出hello:roobtyan,则证实springboot+thymeleaf整合成功,以下图。
数据库
springboot整合mybatis至关简单,不必像整合SSM那么复杂。咱们只须要在application.properties中添加以下信息。json
#mybatis配置 #实体类配置 mybatis.type-aliases-package=com.roobtyan.project.pojo #驼峰式支持 mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.default-fetch-size=100 #链接延迟 mybatis.configuration.default-statement-timeout=3000 #mapper.xml所在位置 mybatis.mapper-locations=classpath:com/roobtyan/dao/*.xml
此外,为了高效实用mysql数据库,因此咱们须要使用数据库链接池。
如今流行的数据库链接池是druid,由阿里巴巴开发的十分强大的数据库链接池。固然你也能够用C3P0或者DBCP。
spring.datasource.url=jdbc:mysql://localhost:3306/template?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
测试:在数据库中建立数据库template,在template数据库中建立数据表user,user中的对象为id(int)、username(varchar)、password(varchar)
在pojo包中建立User对象:
public class User { private int id; private String username; private String password; 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 String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
在dao中建立UserMapper
@Mapper public interface UserMapper { @Select("select * from user where id = #{id}") User getUserById(@Param("id") int id); @Insert("insert into user (id,username,password) value( #{id}, #{username},#{password})") void insertUser(User user); }
在service中建立UserService
@Service public class UserService { private UserMapper userMapper; @Autowired public UserService(UserMapper userMapper) { this.userMapper = userMapper; } public User getUserById(int id) { return userMapper.getUserById(id); } }
在SampleController中编写:
// 数据库测试 /** * 从数据库中获取内容 * 声明@ResponseBody的缘由是咱们须要接收输出的json数据 * * @return */ @RequestMapping("/dbGet") @ResponseBody public Result<User> dbGet() { User userById = userService.getUserById(1); return Result.success(userById); }
启动MainApplication,调用Controller,在浏览器访问:localhost:8080/demo/dbGet
若是出现数据库中的数据的json对象,即证实整合成功。
咱们在实际的开发过程当中,常常会用到redis,若是你须要redis的整合,就给我留言吧!
最后,若是你喜欢这篇文章,请关注微信公众号:“最高权限比特流”。 若是你须要整合的完整代码,请在公众号中回复“4”获取。