当前位置:   article > 正文

Spring boot微服务框架整合搭建与实战详解_springboot 加 微服务 实现统一集成平台

springboot 加 微服务 实现统一集成平台

本文旨在分享本人自己的一些经验,springboot快速搭建与实战测试。好了,希望能帮助到大家!!!希望大家遇到各种报错异常问题多多百度,然后做笔记记录。

声明:对于本文内容解析由本人理解而道出,希望对于有说得不对的地方大家指点指点。

1,本文本使用的开发环境:

java ee开发工具;

jdk1.8,环境配置:https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html(配置环境参考);

maven3.0;

PHPstudy集成的数据库MySQL5.5;

2,新建maven项目

环境搭建完成后打开java ee新建maven项目

3,引入Springboot依赖包

添加下面依赖到pom.xml文件

  1. <!-- Spring boot 父引用-->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>1.4.0.RELEASE</version>
  6. </parent>
  7. <!-- Spring boot 核心web-->
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. </dependencies>

如果pom.xml报错就引入maven插件:

  1. <plugins>
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-surefire-plugin</artifactId>
  5. <version>2.6</version>
  6. <configuration>
  7. <skipTests>true</skipTests>
  8. </configuration>
  9. </plugin>
  10. </plugins>

4,新建一个controller(控制器)

  1.     @RequestMapping("/helloworld")
  2.     public String helloworld() {
  3.         return "Spring boot 初步成果,heyhey";
  4.     }
  5. }

5,开始创建程序入口(Application)

新建一个SpringbootApplication.class类:

  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringbootApplication.class, args);
  5. }

新建完成后右击SpringbootApplication.class-->run as-->Java Application,运行后出现以下界面:


然后去浏览器输入:http://localhost:8080/helloworld,出现以下页面那就恭喜你,初步Springboot框架搭建完成了


注解解析:

@SpringBootApplication包括了@Configuration,@EnableAutoConfiguration,@ComponentScan这三个注解。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。所以写一个@SpringBootApplication就等于写了三个注解。下面来解析一下这三个注解的意思 。

@Configuration:对于我们很多开发来说这个都不陌生,它就是javaConfig形式的Spring Ioc容器的配置类使用的那个@Configuration:用于加载某个IoC容器的配置

@EnableAutoConfiguration:它导入了一个叫ImportSelector的接口的实现类,ImportSelector接口中的selectImports方法返回的类都会被Spring容器管理起来

@ComponentScan:功能就是自动扫描并加载符合条件的组件或者bean定义

这只是个小小的开始,But,我觉得学习是永远不会结束:

初步的Springboot微服务框架搭建完成了,接下来我们开始实战搭建springboot,实现对数据库进行查询的一个小小例子。首先我们整合mybatis。至于mybatis是什么,不了解的自行去学习。

1,对的,添加依赖

把下面mybatis和mysql依赖添加到pom.xml:

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.1.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <version>5.1.21</version>
  10. </dependency>

2,新建application配置文件

新建application.properties文件:

这里Tomcat默认使用8080端口,如有报错请注意
  1. spring.datasource.url=jdbc:mysql://localhost:3306/test
  2. spring.datasource.username=root
  3. spring.datasource.password=123456
  4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3,新建一个数据库表

  1. Create Table: CREATE TABLE `USER` (
  2. `id` int(20) NOT NULL AUTO_INCREMENT,
  3. `loginname` varchar(200) DEFAULT NULL,
  4. PRIMARY KEY (`id`)
  5. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

4,编写User实体类

新建一个entity包,在这个包下面新建User实体类:

  1. package com.dome.test.entity;
  2. public class User {
  3. private Integer id;
  4. private String loginname;
  5. public Integer getId() {
  6. return id;
  7. }
  8. public void setId(Integer id) {
  9. this.id = id;
  10. }
  11. public String getLoginname() {
  12. return loginname;
  13. }
  14. public void setLoginname(String loginname) {
  15. this.loginname = loginname;
  16. }
  17. @Override
  18. public String toString() {
  19. return "User [id=" + id + ", loginname=" + loginname + "]";
  20. }
  21. }

5,编写dao层代码(也叫mapper)

新建一个dao包,在下面新建一个UserDao类:


  1. package com.dome.test.dao;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. import com.dome.test.entity.User;
  6. @Mapper
  7. public interface UserDao {
  8.     @Select("SELECT * FROM USER")
  9.     List<User> getall();
  10. }

6,新建service层

新建一个service包,在下面新建UserService和UserServiceImpl两个类:

UserService:

  1. package com.dome.test.service;
  2. import java.util.List;
  3. import com.dome.test.entity.User;
  4. public interface UserService {
  5. List<User> getall();
  6. }

UserServiceImpl:
  1. package service;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import dao.UserDao;
  6. import entity.User;
  7. @Service
  8. public class UserServiceImpl implements UserService{
  9. @Autowired
  10. UserDao userdao;
  11. @Override
  12. public List<User> getall() {
  13. // TODO Auto-generated method stub
  14. return userdao.getall();
  15. }
  16. }

7,新建controller(控制器)

在Controller包下面新建一个Usercontroller类:


  1. package com.dome.test.Controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import com.dome.test.entity.User;
  8. import com.dome.test.service.UserService;
  9. @Controller
  10. @RequestMapping("test")
  11. public class Usercontroller {
  12. @Autowired
  13. UserService userService;
  14. @RequestMapping("/getall")
  15. @ResponseBody
  16. public List<User> getall(){
  17. return userService.getall();
  18. }
  19. }

8,重新编写SpringbootApplication

之前简单搭建springboot微服务框架的时候编写过,但是位置对于现在来说不对,这个application应用程序启动入口应该放在最外侧,既包含所有子包

在包最外侧编写SpringbootApplication:

上图为本次搭建的dome项目,搭建完成的项目结构
  1. package com.dome.test;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication(scanBasePackages="com.dome.test")//扫描该包目录下的文件
  6. @MapperScan("com.dome.test.dao")//扫描mapper文件
  7. public class SpringbootApplication {
  8. public static void main(String[] args) throws Exception {
  9. SpringApplication.run(SpringbootApplication.class, args);
  10. }
  11. }

9,本次搭建的springboot实战demo以完成,我们去测试一波

我们右击SpringbootApplication.class-->run as-->Java Application,运行后出现以下界面:


注意:再次提醒如果报Tomcat错误,请检查端口是否被占用,默认使用8080

好了,运行成功啦,你成功了吗?

然后去浏览器输入:http://localhost:8080/test/getall【本人端口8088】,出现以下页面那就恭喜你,本次Springboot框架实战dome搭建完成了



希望各位能学习到,本人也是一直在学习的路上,第一次编写原创博文,有错误或者说法不当,错误的地方希望能大家能指出让本人更正一下。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/57750
推荐阅读
相关标签
  

闽ICP备14008679号