利用SpringBoot简单快速搭建WEB项目入门

我们在WEB应用开发过程中,我们常常用到的语言是JAVA语言。作为WEB应用中的霸主级别中
的存在,却常常被嘲讽。“人生苦短,我用python”是python程序员们引以为傲的格言。人们经常
将JAVA与时代新宠Python作比较,至今网上还流传着python代码都在review了而JAVA还在搭框架解决jar包冲突。今天为了给我们JAVA程序员扳回一局。今天,我要向大家介绍的如何快速SpringBoot快速搭建一个SSM项目。

介绍SpringBoot

首先,我们访问Spring Boot官方网站https://spring.io/projects/spring-boot 可以看到官网对他的介绍:

		1.创建独立的Spring应用程序

		2.直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)

		3.提供自以为是的“入门”依赖项以简化构建配置

		4.尽可能自动配置Spring和第三方库

		5.提供生产就绪功能,例如指标,运行状况检查和外部化配置

		6.绝对没有代码生成,也不需要XML配置

快速搭建SSM框架

工具:IntellJ IDEA MAVEN
首先,我们创建一个普通的maven工程,点击:
在这里插入图片描述
第二步,填入项目名称与项目域名,就会生成一个maven项目:
在这里插入图片描述
第三步,打开pom文件引入springboot坐标,引入springboot启动类,jdk版本信息,javaweb启动类

<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.xindazj</groupId>
    <artifactId>xindazj-portal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
	<packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!--分页插件pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.3</version>
        </dependency>
        <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- 缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
        <!--json解析工具-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
    </dependencies>
</project>

第四步,对ssm,连接数据库进行相关配置,这里我将只介绍一种yml方式配置(启动类上@Bean注入,相关配置类重写 properties都可以进行配置)。在resources目录下新建一个application.yml:

server:
  port: 80
#jdbc
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xindadb?serverTimezone=GMT%2B8
    username: root
    password: root
#mybatis
mybatis:
  type-aliases-package: cn.xindazj.pojo
  				

第五步,进行启动类的编写:


import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.github.pagehelper.PageHelper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import tk.mybatis.spring.annotation.MapperScan;

import java.util.Properties;


@SpringBootApplication
@MapperScan("cn.xindazj.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    //配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","false");
        properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

    @Bean//使用@Bean注入fastJsonHttpMessageConvert
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        //1.需要定义一个Convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
        //2.添加fastjson的配置信息,比如是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig=new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3.在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter=fastConverter;
        return new HttpMessageConverters(converter);
    }
}

最后,对新搭建的项目进行访问测试:

在这里插入图片描述

总结

spring boot是自动配置的管理,将极大的减少我们因为jar包冲突,简化框架搭建,摆脱繁重的配置文件xml等,是JAVA划时代的变化。是不是很激动,让我们赶快试一试吧。这是我第一次发博客,由于自身能力尚浅,可能会存在很多错误之处,还希望csdn的大神们多指导,多批评。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不安分学徒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值