1. 程式人生 > >詳解intellij idea搭建SpringBoot

詳解intellij idea搭建SpringBoot

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. 生成模板

詳解intellij idea搭建Spring Boot

然後用idea匯入生成的模板,匯入有疑問的可以看我另外一篇文章

詳解intellij idea搭建Spring Boot

2. 建立Controller

詳解intellij idea搭建Spring Boot

3. 執行專案

添加註解 @ComponentScan(註解詳情點這裡) 然後執行

詳解intellij idea搭建Spring Boot

在看到"Compilation completed successfully in 3s 676ms"訊息之後,開啟任意瀏覽器,輸入 http://localhost:8080/index 即可檢視效果,如下圖

詳解intellij idea搭建Spring Boot

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,'[email protected]');
insert into users values(2,'',27,136,'[email protected]m');
insert into users values(3,'',31,159,'[email protected]');
insert into users values(4,'',35,130,'[email protected]'

分別建立三個包,分別是dao/pojo/service, 目錄如下

詳解intellij idea搭建Spring Boot

新增User:

詳解intellij idea搭建Spring Boot
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;
    }
}
View Code

新增UserMapper:

詳解intellij idea搭建Spring Boot
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);
}
View Code

新增UserService:

詳解intellij idea搭建Spring Boot
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);
}
View Code

新增UserServiceImpl

詳解intellij idea搭建Spring Boot
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);
    }
}
View Code

controller新增API方法

詳解intellij idea搭建Spring Boot
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;
    }
}
View Code

修改租車ZucheApplication

詳解intellij idea搭建Spring Boot
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);
    }
}
View Code

新增資料庫連線相關配置,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

按如下提示執行

詳解intellij idea搭建Spring Boot

瀏覽器輸入得到效果:

詳解intellij idea搭建Spring Boot

vgithub地址

v部落格總結

系統故障常常都是不可預測且難以避免的,因此作為系統設計師的我們,必須要提前預設各種措施,以應對隨時可能的系統風險。

作  者: 出  處:http://www.cnblogs.com/toutou/ 關於作者:專注於基礎平臺的專案開發。如有問題或建議,請多多賜教! 版權宣告:本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連結。 特此宣告:所有評論和私信都會在第一時間回覆。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信我 聲援博主:如果您覺得文章對您有幫助,可以點選文章右下角推薦一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!