zoukankan      html  css  js  c++  java
  • Spring Boot入门

      最近在公司太清闲了,为了打发我过剩的精力,我逛起了微信上关注的几个关于java技术的公众号,有一篇文章上《Java工程师技能表》,上边的技能排名挺有意思的:

    其他技能即使在公司项目中没用过,但是咱最少自学过,有点了解。但是Spring Boot竟然排名这么高,那我今天就来入入门。

    什么是Spring Boot?特点是什么?

      Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

      特点:

        1. 创建独立的Spring应用程序
        2. 嵌入的Tomcat,无需部署WAR文件
        3. 简化Maven配置
        4. 自动配置Spring
        5. 提供生产就绪型功能,如指标,健康检查和外部配置
        6. 绝对没有代码生成和对XML没有要求配置
    -------------------------------------
      Spring Boot配置简单、功能齐全,如果我们只需要开发一个功能简单的程序,就可以使用Spring Boot,省去繁琐的框架整合时间。
     
     
    下面就开始进行一个Spring Boot demo的创建吧!
     
    第一步:下载压缩包,导入项目。
      
      下载地址: http://start.spring.io/
      下载获得: demo.zip  解压缩并导入到项目中。
     
     
    第二步:编写相关代码。
     
    1.pom.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>com.example</groupId>
     7     <artifactId>demo</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>demo</name>
    12     <description>Demo project for Spring Boot</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>1.5.2.RELEASE</version>
    18         <relativePath /> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25     </properties>
    26 
    27     <dependencies>
    28         <dependency>
    29             <groupId>org.springframework.boot</groupId>
    30             <artifactId>spring-boot-starter</artifactId>
    31         </dependency>
    32 
    33         <dependency>
    34             <groupId>org.springframework.boot</groupId>
    35             <artifactId>spring-boot-starter-test</artifactId>
    36             <scope>test</scope>
    37         </dependency>
    38 
    39 
    40         <dependency>
    41             <groupId>org.springframework.boot</groupId>
    42             <artifactId>spring-boot-starter-web</artifactId>
    43         </dependency>
    44 
    45         <!-- mybatis -->
    46         <dependency>
    47             <groupId>org.mybatis.spring.boot</groupId>
    48             <artifactId>mybatis-spring-boot-starter</artifactId>
    49             <version>1.1.1</version>
    50         </dependency>
    51         <dependency>
    52             <groupId>mysql</groupId>
    53             <artifactId>mysql-connector-java</artifactId>
    54         </dependency>
    55         <dependency>
    56             <groupId>org.springframework.boot</groupId>
    57             <artifactId>spring-boot-devtools</artifactId>
    58             <optional>true</optional>
    59         </dependency>
    60     </dependencies>
    61 
    62     <build>
    63         <plugins>
    64             <plugin>
    65                 <groupId>org.springframework.boot</groupId>
    66                 <artifactId>spring-boot-maven-plugin</artifactId>
    67             </plugin>
    68         </plugins>
    69     </build>
    70 
    71 
    72 </project>

    2.application.properties

    1 mybatis.type-aliases-package=com.example.myproject.entity
    2 
    3 spring.datasource.driverClassName = com.mysql.jdbc.Driver
    4 spring.datasource.url = jdbc:mysql://localhost:3306/tao?useUnicode=true&characterEncoding=utf-8
    5 spring.datasource.username = root
    6 spring.datasource.password = 123
    3.实体类
     1 package com.example.myproject.entity;
     2 
     3 import java.io.Serializable;
     4 
     5 public class User implements Serializable {
     6     private String username;
     7     private String password;
     8     private String name;
     9     private String email;
    10     public String getUsername() {
    11         return username;
    12     }
    13     public void setUsername(String username) {
    14         this.username = username;
    15     }
    16     public String getPassword() {
    17         return password;
    18     }
    19     public void setPassword(String password) {
    20         this.password = password;
    21     }
    22     public String getName() {
    23         return name;
    24     }
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28     public String getEmail() {
    29         return email;
    30     }
    31     public void setEmail(String email) {
    32         this.email = email;
    33     }
    34     
    35 }

    4.DAO层代码

     1 package com.example.myproject.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.ibatis.annotations.Delete;
     6 import org.apache.ibatis.annotations.Insert;
     7 import org.apache.ibatis.annotations.Select;
     8 import org.apache.ibatis.annotations.Update;
     9 
    10 import com.example.myproject.entity.User;
    11 
    12 public interface UserDao {
    13     @Select("SELECT * FROM user")
    14     List<User> getAll();
    15 
    16     @Select("SELECT * FROM user WHERE username = #{username}")
    17     User getOne(String username);
    18 
    19     @Insert("INSERT INTO user(username,password,name,email) VALUES(#{username}, #{password}, #{name}, #{email})")
    20     void insert(User user);
    21 
    22     @Update("UPDATE user SET name=#{name} WHERE username =#{username}")
    23     void updateNameByUsername(User user);
    24 
    25     @Delete("DELETE FROM user WHERE username =#{username}")
    26     void delete(String username);
    27 }

    5.service层代码

     1 package com.example.myproject.service;
     2 
     3 import java.util.List;
     4 
     5 import com.example.myproject.entity.User;
     6 
     7 public interface UserService {
     8     /**
     9      * 查询全部user
    10      * @return
    11      */
    12     List<User> getAll();
    13     /**
    14      * 根据username查询一条user
    15      * @param username
    16      * @return
    17      */
    18     User getOne(String username);
    19     /**
    20      * 插入一条user
    21      * @param user
    22      */
    23     void insert(User user);
    24     /**
    25      * 根据username查到user,修改name
    26      * @param user
    27      */
    28     void updateNameByUsername(User user);
    29     /**
    30      * 删除user
    31      * @param username
    32      */
    33     void delete(String username);
    34 }
     1 package com.example.myproject.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 
     8 import com.example.myproject.dao.UserDao;
     9 import com.example.myproject.entity.User;
    10 import com.example.myproject.service.UserService;
    11 
    12 @Service("userService")
    13 public class UserServiceImpl implements UserService {
    14     
    15     
    16     @Autowired
    17     private UserDao userDao;
    18 
    19     @Override
    20     public List<User> getAll() {
    21         return userDao.getAll();
    22     }
    23 
    24     @Override
    25     public User getOne(String username) {
    26         return userDao.getOne(username);
    27     }
    28 
    29     @Override
    30     public void insert(User user) {
    31         userDao.insert(user);
    32     }
    33 
    34     @Override
    35     public void updateNameByUsername(User user) {
    36         userDao.updateNameByUsername(user);
    37     }
    38 
    39     @Override
    40     public void delete(String username) {
    41         userDao.delete(username);
    42     }
    43 
    44 }

    6.controller代码:

     1 package com.example.myproject.action;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.ResponseBody;
     9 import org.springframework.web.bind.annotation.RestController;
    10 
    11 import com.example.myproject.entity.User;
    12 import com.example.myproject.service.UserService;
    13 
    14 @Controller
    15 public class HelloWorldController {
    16     
    17     
    18     @Autowired(required = false)
    19     private UserService userService;
    20     
    21     
    22     @RequestMapping("/getAll")
    23     public @ResponseBody List<User> getAll() {
    24         return userService.getAll();
    25     }
    26     
    27     @RequestMapping("/getOne")
    28     public @ResponseBody User getOne(String username) {
    29         return userService.getOne(username);
    30     }
    31     
    32     @RequestMapping("/insert")
    33     public @ResponseBody User insert() {
    34         User user = new User();
    35         user.setUsername("zs");
    36         user.setPassword("123456");
    37         user.setName("zhangsan");
    38         user.setEmail("123@qq.com");
    39         userService.insert(user);
    40         return userService.getOne("zs");
    41     }
    42     
    43     @RequestMapping("/updateNameByUsername")
    44     public @ResponseBody User updateNameByUsername() {
    45         User user = new User();
    46         user.setUsername("zs");
    47         user.setName("zs1111");
    48         userService.updateNameByUsername(user);
    49         return userService.getOne("zs");
    50     }
    51     
    52     @RequestMapping("/delete")
    53     public @ResponseBody List<User> delete() {
    54         userService.delete("zs");
    55         return userService.getAll();
    56     }
    57 }
    7.运行代码修改:
     1 package com.example;
     2 
     3 import org.mybatis.spring.annotation.MapperScan;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 @SpringBootApplication
     8 //地址到dao层接口处,否则报错
     9 @MapperScan("com.example.myproject.dao")
    10 public class DemoApplication {
    11 
    12     public static void main(String[] args) {
    13         SpringApplication.run(DemoApplication.class, args);
    14     }
    15 }

      至此,只要运行这个main方法,访问   localhost:8080/getAll     就可以运行了。

     
     
      简单的试用了一下Spring Boot,确实被它简单的配置给惊到了,看来以后自己学点学习的demo时,就用它吧!哈哈。。。

    感谢以下博文:

      http://www.cnblogs.com/ityouknow/p/6037431.html

      http://www.cnblogs.com/ityouknow/p/5662753.html

  • 相关阅读:
    DNS部署与安全
    DHCP部署与安全
    jenkins漏洞复现
    Apache Axis2 漏洞复现
    制作war包
    JBOOS 漏洞复现
    Tomcat漏洞复现
    编写登陆接口(2)
    学习使用新工具Pycharm
    while练习99乘法表
  • 原文地址:https://www.cnblogs.com/taozhiye/p/6599738.html
Copyright © 2011-2022 走看看