SpringBoot(一)快速入门

经过一段时间的研究和使用,暂时并不考虑花太多时间去学这个东西,为了工作蛮弄吧……

常规的项目框架,使用SpringBoot可以轻松搭建起来,因为大部分的事情SpringBoot都帮我们做了;

但是想写点东西放到SpringBoot中,困难程度比Spring要高不少。

就像在Spring中使用Mybatis,Mybatis是一个独立的框架,脱离Spring也可以用,如果想集成到Spring中,需要添加Spring-Mybatis的jar包;

封装代码已经很麻烦了,如果想在SpringBoot中添加自己写的东西,还要再写一份适配代码,那是真的烦;

开发下来,确实能感觉到Xml配置在解耦方面要优于注解,“零配置”的想法很好,但是,这跟代码解耦,和降低开发难度相悖的。

(当然,你也可以在SpringBoot中启用Xml配置就是了……)

SpringBoot目录

main
--java:Java代码存放目录
--resources:文件资源存放目录
    |--static:静态资源存放目录(JS、CSS、Html,不做任何配置即可访问)
    |--templates:页面模版(动态页面,一般存放需要渲染的页面,Freemaker、Beetl、Thymeleaf、Velocity等)
    |--mapping:Mybatis关系映射配置(根据实际需求,可任意指定位置)
--webapp:

pom.xml:Maven配置

test:代码单元测试

SpringBoot程序入口

确实难以想象,一个Web项目可以用主函数启动,不过看了Tomcat的程序目录,大概也明白怎么回事:Tomcat很多代码都是由java开发,能用主函数启动也很正常。

此时往static目录下放置静态资源文件,启动项目就可以直接访问,不需要做任何配置。

SpringBootApplication:SpringBoot主函数
ComponentScan:扫描包配置
MapperScan:Mybatis扫描包配置(配置了MapperScan,ylm文件就可以不配置,@Mapper注解不使用也正常运行)

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 主函数
 *
 * Created by CSS on 2018/4/28.
 */
@SpringBootApplication
@MapperScan("com.sea.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

模版引擎配置

templates目录主要用于存放动态页面,SpringBoot推荐使用thymeleaf,不过我用的性能更好的是FreeMarker,添加maven依赖、配置yml文件即可(代码在后面给出)

持久层配置

使用的是MyBatis,参数是Sql查询语句,因为是测试,怎么简单怎么来吧。

import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

@Mapper
public interface Debug {
    public List<Map<String, Object>> select(String sql) throws Exception;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sea.dao.Debug">
    <select id="select" parameterType="String" resultType="java.util.HashMap">
        ${value}
    </select>
</mapper>

Controller层

控制层可以使用SpringMVC原先的所有注解,且功能一样;

不要使用RequestMapping("/**")控制所有的动态页面,会跟静态资源文件冲突,使用(/**.ftl)即可。

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.dao.Debug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * Created by CSS on 2018/4/28.
 */
@Controller
public class Core {
    @RequestMapping("/page/**")
    public void core() {
    }

    @Autowired
    Debug mapper;

    @ResponseBody
    @RequestMapping("/data")
    public PageInfo<Map<String, Object>> data() {
        try {
            PageHelper.startPage(1, 2);
            List<Map<String, Object>> list = mapper.select("SELECT * FROM `sys_user`");
            return new PageInfo<Map<String, Object>>(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

单元测试

import com.sea.Application;
import com.sea.dao.Debug;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class PageTest {
    @Autowired
    Debug mapper;

    @Test
    public void testHome() throws Exception {
        System.out.println(mapper.select("SELECT * FROM `sys_user`"));
    }
}

Maven配置

比较特别的就PageHelper(Mybatis分页插件)和阿里的Druid(连接池)了,如果不用删除即可。

模版引擎看个人喜好了,JSP我已经一年不用了,没特别情况,今后不会再用了吧。

<?xml version="1.0" encoding="UTF-8"?>
<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>com.sea</groupId>
  <artifactId>SeaBoot</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SeaBoot Maven Webapp</name>
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/>
  </parent>

  <dependencies>
    <!--单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!--数据库连接-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.35</version>
    </dependency>

    <!--Web-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--模版引擎-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <!--单元测试-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!--数据库-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.1.2</version>
    </dependency>
  </dependencies>

  <!--build是IDEA自动生成的-->
  <build>
    <finalName>SeaBoot</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Application.yml配置

server:
  port: 8080

spring:
    datasource:
        name: test
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/med?useUnicode=true&amp;characterEncoding=utf8&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;failOverReadOnly=false
        username: root
        password: root
        type: com.alibaba.druid.pool.DruidDataSource

        initialSize: 5
        minIdle: 5
        maxActive: 10
        maxWait: 60000
        filters: stat
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select ‘x‘
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20

    #模版引擎
    freemarker:
        template-loader-path: classpath:/templates
        suffix: .ftl
        expose-request-attributes: true
        request-context-attribute: request

#持久层框架
mybatis:
    mapper-locations: classpath:mapping/*.xml

#分页工具
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

测试页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <title>Title</title>
</head>
<body>
<button id="btn">click</button>
Test Static Html
<script type="text/javascript">
    $(document).ready(function () {
        $("#btn").click(function () {
            alert("-----------------")
        });
    });
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/chenss15060100790/p/9094977.html

时间: 2024-08-02 02:42:00

SpringBoot(一)快速入门的相关文章

SpringBoot快速入门

最近学习了一下SpringBoot,其实也不是什么新功能,只是可以快速启动一下一个Spring应用,就像Maven集成了所有jar包一样,Springboot集成了大部门的框架,需要使用的时候,只要在pom.xml文件中引入即可. 前面我们使用SpringMvc+myBtais+Spring搭建一个web应用,需要很多配置文件,等项目开发完后,测试的时候需要发布到Tomcat或者其他容器才能运行起来. 总的来说,SpringBoot有以下几个优点:(1)提供各种默认配置来简化项目配置  (2)内

SpringData 基于SpringBoot快速入门

SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战中学习两个框架的知识,又可以为单点登录系统打下基础.通过本章你将掌握 SpringBoot项目的搭建,Starter pom的使用,配置全局文件,核心注解SpringBootApplication 介绍以及单元测试 SpringBootTest注解的使用.SpringData 的入门使用,Repos

springboot(一):快速入门

么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. 使用spring boot有什么好处 在没有springboot之前,使用ssh或者ssm时候都会做相应的xml的配置后,如下 1)配置web.xml,加载spring和spring mvc 2)配置数据库连接.配置spring事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件等 现在有使用spingboot我们不在去写配置文件,就能简

SpringBoot集成beetl模板快速入门

SpringBoot集成beetl模板快速入门 首次探索 beetl官方网址:http://ibeetl.com/ 创建SpringBoot工程(idea) 新建工程 选择创建Spring工程 书写包名和项目名称等 选择集成web依赖 确认项目保存路径信息 修改maven本地仓库位置 maven本地仓库位置,默认在C盘的: "C:\Users\用户名 ?.m2\repository" 可以复制maven工程下的setting.xml修改其文件,详情见该博客 http://blog.cs

SpringBoot 快速入门案例

SpringBoot是一个配置很少就能轻松搭建Web应用框架,相信学过SSH或者SSM框架的开发者都知道在该框架环境下需要配置一堆XML配置文件才能实现搭建Web应用,学习完SpringBoot后,搭建Web应用会让你有丝滑般的畅快. SpringBoot2.2.2版本快速入门环境要求 目前Spring官网官网正式发行的版本是2.2.2版本,在其官方文档列出以下环境要求,本文也是基于2.2.2版本快速搭建入门的案例,所谓工欲善其事必先利其器,生产环境得搞起来. 工具 版本 Maven 3.3+

Springboot之YAML快速入门教学

Java作为高级编译程序的元老,以一处编译到处运行的特点,广受开发者喜爱 我们学习Java,都是先介绍properties文件,使用properties文件配合Properties对象能够很方便的适用于应用配置上.然后在引入XML的时候,我们介绍properties格式在表现层级关系和结构关系的时候,十分欠缺,而XML在数据格式描述和较复杂数据内容展示方面,更加优秀.到后面介绍JSON格式的时候,我们发现JSON格式比较XML格式,更加方便(除去数据格式限制之外),所以现在很多配置文件(比如Ng

Spring Boot 快速入门

什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适). 使用spring boot有什么好处 其实就是简单.快速.方便!平时如果我

Spring MVC 快速入门

Spring MVC 快速入门 环境准备一个称手的文本编辑器(例如Vim.Emacs.Sublime Text)或者IDE(Eclipse.Idea Intellij) Java环境(JDK 1.7或以上版本) Maven 3.0+(Eclipse和Idea IntelliJ内置,如果使用IDE并且不使用命令行工具可以不安装) 一个最简单的Web应用 使用Spring Boot框架可以大大加速Web应用的开发过程,首先在Maven项目依赖中引入spring-boot-starter-web:po

Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门

文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Maven 依赖 4.2. 开启缓存支持 4.3. 服务层 4.4. 控制层 4.5. 运行 4.6. 课后作业 6. 源代码 为了提高性能,减少数据库的压力,使用缓存是非常好的手段之一.本文,讲解 Spring Boot 如何集成缓存管理. 声明式缓存 Spring 定义 CacheManager 和

Springboot(一):入门篇

什么是spring boot spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适). 使用spring boot有什么好处 其实就是简单.快速.方便!平时如果我