自我救赎 → 利用 IDEA 和 Spring Boot 搭建 SSM

前言

  开心一刻html

    儿子读高中放学回来了,一贯无论他学习的我忽然来了兴趣,想看看他的学习他的情况,抄起他的数学习题看了起来,当看到 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 = 10! 我当时火冒三丈,一巴掌就呼过去了,怒吼道:你这是怎么读的,1乘以2乘以3乘以...10怎么等于10! 旁边的媳妇一平底锅甩我脸上:不等于10的阶乘,那等于多少?我:这不是感叹号吗?java

写做背景

  关于 ssm 的基础篇,我一共写了三篇博客:mysql

  利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)git

    搭建 ssm 比较简单,eclipse 配置好 maven,工程中配置好相关配置文件便可;可是此种方式已过期,包括eclipse、spring4.0.二、ssm的搭建方式(pom中配置的依赖太多),再也不推荐此种方式。github

  关于利用maven搭建ssm的博客,咱们一块儿来探讨下问的最多的问题web

    根据你们搭建 ssm 过程当中遇到的问题,总结出了此篇,主要是针对数据绑定的问题进行了详细的说明;内容不过时,推荐仔细看看。spring

  Spring MVC的工做原理,咱们来看看其源码实现sql

    由问题:Spring mvc 是什么时候、何地、如何将Model中的属性绑定到哪一个做用域 引起的 Spring MVC 的工做原理的探索,随着 Spring 版本的变迁,Spring MVC 或许会有少量的改动,但单核心不会变,推荐仔细看看。apache

  近来,仍是陆陆续续收到一些刚入门的小伙伴的求助,利用maven/eclipse搭建ssm仍是会出现各类各样的问题,关键是我在帮忙解决的过程当中居然发现我不太会用 eclipse 了, 并且 spring 的版本仍是 4,我就问他们了:大家怎么还在用 eclipse,spring的版本为何那么低? 结果我获得一个统一的回答:我是参考的你的博客搭建的。安全

  此刻我意识到了这是个人问题:随着时间的流逝,我没有及时的更新个人博客 → 利用maven/eclipse搭建ssm(spring+spring mvc+mybatis),致使不少刚入门的小伙伴看到的是过期的内容,误人子弟实乃大罪过!因此我赶忙写了此篇,指正当前正确的方向。一段时间后,此篇博文涉将的内容也会过期,因此你们最好能本身抓住当下主流的内容。

  目前公司用的是 idea + spring boot 实现的工程搭建,也是当下最流行的方式,下面我也就演示下如何利用 idea 和 spring boot 搭建 ssm工程。

IDEA

  IDEA 全称 IntelliJ IDEA,具体它是什么,有什么用,怎么用 等等关于 IDEA 的内容,你们自行去查阅,我就不作啰嗦了。可参考:IDEA 使用教程(持续更新,19年6月14号更新)

  推荐你们用最新的 IDEA 版本,有能力的请支持正版

  只强调一点:maven 设置

    maven的更多内容你们自行去查阅资料,maven 的安装可参考利用maven/eclipse搭建ssm(spring+spring mvc+mybatis),推荐用最新的;安装好maven以后,推荐配置上阿里的镜像,在 maven 的配置文件 settings.xml 中的 mirrors 节点下新增以下 mirror 节点

<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf> 
</mirror>

    maven安装好后,须要在 idea 中配置好 maven,配置过程很简单,以下图所示

    这样 idea 集成 maven 就成功了

Spring Boot

  关于 Spring Boot 的详细信息,你们能够去它的官方文档看,也能够看园子内大佬的博客,推荐:Spring boot 入门篇 

  Spring Boot 是由 Pivotal 团队提供的全新框架,但不是 Spring 的替代品,而是 Spring 的衍生品,目的是用来简化新 Spring 应用的初始搭建以及开发过程;它采用了约定大于配置的作法,默认配置了不少框架的使用方式,就像 Maven 整合了全部的 Jar 包,Spring Boot 整合了全部的框架,同时 Spring Boot 采用 Starters 的方式简化了工程的 maven 依赖配置。

  目前 Spring Boot 的最新的发布版本是 2.1.6 ,咱们不采用它,咱们用 2.1.0 这个发布了有一段时间的版原本演示

SSM 搭建

  咱们能够直接下载官网的样例,而后导入到 idea,这是一种方式,但我仍是想给你们演示一个完整的从无到有的过程

  工程搭建

    方式不少种,我独爱白的像一张纸同样的初始配置

  配置 maven 依赖

    pom.xml

<?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.lee</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <pagehelper.version>1.2.5</pagehelper.version>
        <druid.version>1.1.10</druid.version>
    </properties>

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

    <dependencies>

        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- mybatis相关 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
</project>
View Code

  工程配置

    application.yml

server:
  port: 8888
  servlet:
    context-path: /ssm
spring:
  #链接池配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
      username: root
      password: 123456
      initial-size: 1                     #链接池初始大小
      max-active: 20                      #链接池中最大的活跃链接数
      min-idle: 1                         #链接池中最小的活跃链接数
      max-wait: 60000                     #配置获取链接等待超时的时间
      pool-prepared-statements: true    #打开PSCache,而且指定每一个链接上PSCache的大小
      max-pool-prepared-statement-per-connection-size: 20
      validation-query: SELECT 1 FROM DUAL
      validation-query-timeout: 30000
      test-on-borrow: false             #是否在得到链接后检测其可用性
      test-on-return: false             #是否在链接放回链接池后检测其可用性
      test-while-idle: true             #是否在链接空闲一段时间后检测其可用性
#mybatis配置
mybatis:
  type-aliases-package: com.entity
  #config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mapper/*.xml
# pagehelper配置
pagehelper:
  helperDialect: mysql
  #分页合理化,pageNum<=0则查询第一页的记录;pageNum大于总页数,则查询最后一页的记录
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
View Code

  完整工程

    java文件夹下的内容,包括 conroller、service、mapper、dao 以及 SsmApplication就不逐一展现了,更多详细代码请看:ssm

   实现效果

总结

  不是说过期的东西不能用,而是不推荐用,效率是一方面,安全也是一方面,保不许过期的内容会在后续哪一个版本就直接剔除了,那就是真的不能用了;当下 java web 的热门开发工具就是 IDEA(不只仅只是java哦),热门框架就是 Spring Boot,当下流行不保证一直流行,咱们须要保持一颗学习的心,多关注时下的热门技术。

参考

  SpringBoot入门教程(一)详解intellij idea搭建SpringBoot

  Spring boot 入门篇

相关文章
相关标签/搜索