springboot微服務架構——搭建入門(轉)

java j2ee

springboot簡介 

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。SpringBoot是伴隨著Spring4.0誕生的;從字面理解,Boot是引導的意思,因此SpringBoot幫助開發(fā)者快速搭建Spring框架;SpringBoot繼承了原有Spring框架的優(yōu)秀基因;SpringBoot簡化了使用Spring的過程。 springboot的優(yōu)點 

改變了以前三層架構開發(fā)的配置繁瑣,簡化了配置,而且有springsource tool suite插件可以快速的搭建開發(fā)環(huán)境,注解功能十分強大。使部署更簡單,只需一個jar包 springboot的應用場景 

大數(shù)據(jù)、高并發(fā)的電商項目 springboot搭建 

新建maven project,導入所需要的jar包 

pom.xml文件內(nèi)容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.itcast</groupId>

  <artifactId>springBootDemo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.4.0.RELEASE</version>

  </parent>

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <java.version>1.8</java.version>

  </properties>

  <dependencies>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-test</artifactId>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-devtools</artifactId>

    </dependency>

  </dependencies>

</project>

2dc0bf38-ddeb-4df4-a242-b96f9a71b935.jpg

結構(controller等要放在MySpringBootDemo同級的包或下一級包下面,啟動主程序才能有效) 

springboot微服務架構搭建入門

主程序入口,通過主程序來啟動項目

package cn.itcast;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication//必要的注解

public class MySpringBootDemo {

    //項目入口

    public static void main(String[] args) {

        SpringApplication.run(MySpringBootDemo.class, args);

    }

}

測試

import org.springframework.web.bind.annotation.RestController;

import cn.itcast.domain.User;

import cn.itcast.service.UserService;

@RestController//這個注解包含了以前的@Controller,@ResponseBody

public class UserController {

    @Autowired

    private UserService userService;

    /**

     * @return 基本數(shù)據(jù)類型json格式

     */

    @RequestMapping("hello")

    public String showHello(){

        return "hello world!";

    }

    /**

     * @return POJO

     */

    @RequestMapping("pojo")

    public User showUser(){

        User u = new User();

        u.setId(01);

        u.setUsername("張飛");

        u.setSex("男");

        u.setAddress("燕");

        return u;

    }

    /**

     * @return 集合對象map

     */

    @RequestMapping("maps")

    public Map<String, Object> showMaps(){

        Map<String, Object> maps = new HashMap<String, Object>();

        maps.put("username", "岳飛");

        maps.put("age", "21");

        return maps;

    }

    /**

     * @return 集合對象list

     */

    @RequestMapping("list")

    public List<User> showList(){

        //創(chuàng)建list集合對象

        List<User> list = new ArrayList<User>();

        User u1 = new User();

        u1.setId(01);

        u1.setUsername("張飛");

        u1.setSex("男");

        u1.setAddress("燕");

        User u2 = new User();

        u2.setId(02);

        u2.setUsername("岳飛");

        u2.setSex("男");

        u2.setAddress("南宋");

        list.add(u1);

        list.add(u2);

        return list;

    }

    }

結果圖

cb4b4d03-5342-479f-8e50-52fc57fc19f8.jpg
e8ce59fc-921c-480d-9d0e-91521483e3fb.jpg

 

springboot微服務架構搭建入門springboot微服務架構搭建入門springboot微服務架構搭建入門

5. springboot整合mybatis 

總體結構 

8effb982-776f-4668-baf5-90ab215a314d.jpg

springboot微服務架構搭建入門

整合mybatis在application.properties文件

#springboot整合Mybatis框架

#加載Mybatis配置文件

mybatis.mapper-locations = classpath:mapper/*Mapper.xml

mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml

#定義別名

mybatis.type-aliases-package = cn.itcast.domain

#數(shù)據(jù)源

spring.datasource.url = jdbc:mysql://localhost:3306/normal

spring.datasource.driver-class-name = com.mysql.jdbc.Driver

spring.datasource.username = root

spring.datasource.password = root

在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.6</version>

    </dependency>

整合完了,似不似很簡單。 

測試就跟以前的三層架構一樣,這里就不列出來啦

以上是云棲社區(qū)小編為您精心準備的的內(nèi)容,在云棲社區(qū)的博客、問答、公眾號、人物、課程等欄目也有 的相關內(nèi)容,歡迎繼續(xù)使用右上角搜索按鈕進行搜索框架 , spring , 架構 , springboot , 入門 搭建 ,以便于您獲取更多的相關知識。

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

1