zoukankan      html  css  js  c++  java
  • 使用springBoot搭建REATFul风格的web demo

    1 Spring boot 核心特性

    • 自动配置;针对常见 spring 应用程序的常见应用功能,Spring boot 自动提供相应配置
    • 起步依赖;告诉springboot 需要什么功能,他就会自动引入所需的库
    • Actuator;深入运行中的Spring boot 应用程序,一探究竟

    开发Spring boot 项目,两种方案

    1)新建一个maven项目,然后自己配置依赖,配置目录结构。

    2)使用Spring initializer ,指定所需功能即可生成一个工程目录结构。

    主要使用的springboot 配件

    web + mysql + connector + mybatis

    自己使用了之前的方法生成一个maven项目,然后配置项目。

    2 pom文件配置

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.huitong</groupId>
      <artifactId>demo1</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>demo1 Maven Webapp</name>
      <url>http://maven.apache.org</url>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
      </parent>
    
      <properties>
        <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>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.3.1</version>
        </dependency>
    
    
        <!--&lt;!&ndash; 添加缓存支持 &ndash;&gt;-->
        <!--<dependency>-->
          <!--<groupId>org.springframework.boot</groupId>-->
          <!--<artifactId>spring-boot-starter-cache</artifactId>-->
        <!--</dependency>-->
    
        <!-- 添加 redis 缓存支持 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    
        <!-- 添加邮件支持 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    
        <dependency>
          <groupId>com.jayway.jsonpath</groupId>
          <artifactId>json-path</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
        </dependency>
    
    
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
        </dependency>
    
        <!-- Spring boot 热启动依赖 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <optional>true</optional>
        </dependency>
    
    
      </dependencies>
    
    
      <build>
        <finalName>demo1</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <includes>
              <include>**/*.xml</include>
              <include>**/*.properties</include>
            </includes>
          </resource>
    
          <resource>
            <directory>src/main/java</directory>
            <includes>
              <include>**/*.xml</include>
              <include>**/*.properties</include>
            </includes>
          </resource>
        </resources>
    
        <!--spring boot maven的构造插件-->
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
              <fork>true</fork>
            </configuration>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>${java.version}</source>
              <target>${java.version}</target>
              <testSource>${java.version}</testSource>
              <testTarget>${java.version}</testTarget>
            </configuration>
          </plugin>
    
    
    
        </plugins>
      </build>
    
    </project>
    View Code

    使用Spring boot 构建插件。

    构建插件的主要功能是把项目打包成一个可执行的超级JAR(uber-JAR),包括把应用程序的所有依赖打入JAR文件内,并为JAR添加一个描述文件,其中的内容能让你用 java -jar 来运行应用程序。

    POM中 spring-boot-starter-parent 具有依赖管理功能,继承很多常用库的依赖版本,在你声明依赖时就不用再去指定版本号了。

    项目目录如下

    3 响应对象Greeting.java

    package com.huitong.demo.entity;
    
    import lombok.Data;
    
    
    @Data
    public class Greeting {
    
        private long id;
        private String content;
    
        public Greeting(long id, String content) {
            this.id = id;
            this.content = content;
        }
    
    
    }
    View Code

    4 请求controller, GreetingController.java

    package com.huitong.demo.controller;
    
    import com.huitong.demo.entity.Greeting;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.concurrent.atomic.AtomicLong;
    
    
    @RestController
    public class GreetingController {
    
        private final AtomicLong counter = new AtomicLong();
    
        @RequestMapping(value = "/demo1/greeting", method = RequestMethod.POST)
        public Greeting greeting(@RequestParam String name) {
            String s = "hello %s!";
    
            return new Greeting(
                    counter.incrementAndGet(),
                    String.format(s, name));
        }
    
    
    }
    View Code

    5 生成可执行程序,Demo1Application.java

    package com.huitong;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    public class Demo1Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Demo1Application.class, args);
    
        }
    }
    View Code

    @SpringBootApplication 开启了Spring的组件扫描和Spring Boot的自动配置功能。 @SpringBootApplication 将三个有用的注解组合在了一起。

    • Spring的 @Configuration :标明该类使用Spring基于Java的配置。虽然本书不会写太多配置,但我们会更倾向于使用基于Java而不是XML的配置。

    • Spring的 @ComponentScan :启用组件扫描,这样你写的Web控制器类和其他组件才能被自动发现并注册为Spring应用程序上下文里的Bean。

    • Spring Boot 的 @EnableAutoConfiguration : 这 个 不 起 眼 的 小 注 解 也 可 以 称 为@Abracadabra ,就是这一行配置开启了Spring Boot自动配置的魔力,让你不用再写成篇的配置了。

    5.2 如果应养程序需要使用除了Spring boot 自动配置以外的配置,需要使用将配置写到一个单独的@Configuration 标注的类里。(组件扫描会发现并使用这些类的。)

    至此就创建完成了,只需启动Demo1Application 即可。可以看到如下

    进而可以进行测试验证是否成功。

    6 整合mybatis 数据持久化

    6.1 需要配置两个地方

    1)在spplication.xml文件中,配置mapper-location

    # mybatis 配置
    mybatis.mapper-locations=classpath*:/com/huitong/**/mybatis/*.xml

    2)在启动java类中配置mapperScan

    @MapperScan(basePackages = {"com.huitong.*.mybatis"})

    6.2 实际操作接口编写

    1)mapper接口文件

    package com.huitong.persistence.adapter.mybatis;
    
    import com.huitong.entity.Student;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    @Mapper
    public interface StudentMapper {
    
        Student getStudentById(@Param("id") Integer id);
    
    
    }

    2)对应的xml文件sql语句,在同一目录下的情况

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.huitong.persistence.adapter.mybatis.StudentMapper">
    
        <select id="getStudentById" resultType="com.huitong.entity.Student">
            SELECT * FROM student WHERE id = #{id}
        </select>
    
    
    </mapper>

    需要注意:

    使用maven默认构建方法是不会将java源码包下的配置文件(xml、properties)文件编译到target文件夹的classes文件夹下,需要在pom文件中进行编译配置

    详细信息可以参看之前博客springboot 多模块 maven 项目构建jar 文件配置

    http://www.cnblogs.com/zhaopengcheng/p/8616762.html

  • 相关阅读:
    cmd 新建安卓工程
    新创建的android工程里面没有activity问题解决
    背景色横向渐变css5
    Serialable与Parcelable
    Observer观察者模式
    Linux基础(1)
    android 出现Make sure the Cursor is initialized correctly before accessing data from it
    android项目中导入actionbarsherlock 需要注意的地方
    android BadgeView的使用(图片上的文字提醒)
    android仿系统Launcher界面,实现分屏,左右滑动效果(ViewSwitcher)
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/8135526.html
Copyright © 2011-2022 走看看