利用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工程,点击:
利用SpringBoot简单快速搭建WEB项目入门_第1张图片
第二步,填入项目名称与项目域名,就会生成一个maven项目:
利用SpringBoot简单快速搭建WEB项目入门_第2张图片
第三步,打开pom文件引入springboot坐标,引入springboot启动类,jdk版本信息,javaweb启动类


   		 4.0.0
		cn.xindazj
    xindazj-portal
    1.0-SNAPSHOT
    war
	war
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
    
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
        
        
            org.projectlombok
            lombok
        
        
        
            org.mybatis
            mybatis
            3.4.0
        
        
        
            com.github.pagehelper
            pagehelper
            4.1.3
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.0.2
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            redis.clients
            jedis
        
        
            org.springframework.data
            spring-data-redis
        
        
        
            com.alibaba
            fastjson
            1.2.28
        
    

第四步,对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);
    }
}

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

利用SpringBoot简单快速搭建WEB项目入门_第3张图片

总结

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

你可能感兴趣的:(技术博客)