博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
阿里P8大神教你十分钟构建好SpringBoot + SSM框架 成功晋升
阅读量:2145 次
发布时间:2019-04-30

本文共 5862 字,大约阅读时间需要 19 分钟。

目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐。而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了大众的青睐

而本文,我将教大家如何在 intellij idea 中快速构建好一个 Maven + Spring + SpringMVC + MyBatis + SpringBoot 的框架,做到了足够精简,让你可以立刻开始你的 web 项目

一. 创建项目

选择 Spring Initiallizr

image

添加最基本的几个依赖 Web,MySQL,MyBatis,其他需求可以后续再添加 ; 数据库选择了 MySQL

二. 配置数据源

数据源中存储了所有建立数据库连接的信息

1. 配置 IDEA 数据源

输入地址,端口,用户名,密码等等完成设置

2. 配置 spring 数据源

application.properties 文件添加:

spring.datasource.url = jdbc:mysql://xx.xx.xx.x:xxx/xxx?characterEncoding=utf8&allowMultiQueries=true&useSSL=falsespring.datasource.username = rootspring.datasource.password = 123456spring.datasource.driver-class-name = com.mysql.jdbc.Driver
  • url : 数据源 url ,格式为 jdbc:mysql://Host(主机名或 IP 地址):Post(端口)/Database(数据库名称),其中 allowMultiQueries = true : 允许多条 sql 同时执行(分号分隔);useSSL : 是否进行 SSL 连接,根据实际情况选择
  • username : 用户名
  • password : 密码
  • driver-class-name : 驱动名,不同的数据库有不同的 Drivername,如 oracle 数据库的 oracle.jdbc.driver.OracleDriver,MySQL 数据库为 com.mysql.jdbc.Driver

三. Spring 注解

  • 使用 @Controller / @RestController 注解标注一个控制器,表明这个类是作为控制器的角色而存在的
  • 使用 @Service 注解标注一个业务层类
  • 使用 @Repository 注解标注一个持久层 mapper 接口
  • 使用 @Component 注解标注其他组件
  • 使用 @Configuration 注解标注配置类

四. MyBatis

整个项目的构建最主要的部分就是 springboot 和 mybatis 的整合,而 springboot 也提供了十分方便的方式。

1. xml 文件

  • 声明为映射文件
  • namespace : 指该映射文件对应的映射接口 ; 一般来说,一个 XML 映射配置文件对应一个命名空间,而这个命名空间又对应一个接 口

2. application.properties

  • Mybatis 配置,指定了 mybatis 基础配置文件和实体类映射文件的地址
mybatis.mapperLocations = classpath:mapper/**/*.xmlmybatis.typeAliasesPackage = com.swit.model
  • 配置 typeAliasesPackage 可以使得 com.swit.model 包内的实体类可以在映射文件中使用别名,如:

如没有配置 typeAliasesPackage ,则需要 resultType="com.swit.model.User"

  • 如果要对 MyBatis 通过 xml 文件进行另外的配置,则添加文件路径:
mybatis.config-locations=classpath:mybatis/mybatis-config.xml

3. 添加对 mapper 类的扫描

以下两种方法二选其一

(1)可以选择在启动类添加 @MapperScan

value 为 mapper 类所在的包(注意这里是包的路径,而不是类的路径!)

@MapperScan(value = "com.swit.dao")

另外, @MapperScan 注解面向的是接口类,只要是加了注解的接口类都需要进行通过该注解来扫描

(2)可以在每个 mapper 类上添加 @mapper 注解

@Mapper@Repositorypublic interface MyMapper {}

到目前为止,你已经完成了你的项目的构建,下面我还会介绍些别的东西。

五. 其他要注意的点

1. @SpringBootApplication

  • 这个注解位于启动类
  • @SpringBootApplication 等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan, 所以启动类无需再添加这三个注解
  • @Configuration :标注一个类为配置类。
  • @EnableAutoConfiguration :开启自动配置。
  • @ComponentScan :自动收集所有的 Spring 组件

2. 部署服务器

如果你想把自己的 SpringBoot 项目部署到阿里云,腾讯云等服务器,那么你还需要加点东西。 1.如果需要通过打包的方式在web容器中进行部署,则需要继承 SpringBootServletInitializer 覆盖configure(SpringApplicationBuilder)方法

public class SpringbootApplication extends SpringBootServletInitializer {    public static void main(String[] args) {        SpringApplication.run(SpringbootApplication.class, args);    }    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {        // 注意这里要指向原先用main方法执行的Application启动类        return builder.sources(SpringbootApplication.class);    } }

2.pom 文件添加打包插件

projectName
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.8
1.8
true
  1. 你很有可能还需要做个跨域处理
@Componentpublic class CorsFilter implements Filter {    @Override    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {        HttpServletResponse response = (HttpServletResponse) res;        HttpServletRequest request = (HttpServletRequest) req;        response.setHeader("Access-Control-Allow-Origin", Origin);        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");        response.setHeader("Access-Control-Max-Age", "3600");        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, " + this.tokenHeader);        response.setHeader("Access-Control-Allow-Credentials", "true");        chain.doFilter(req, res);    }    @Override    public void init(FilterConfig filterConfig) {    }    @Override    public void destroy() {    }}

六. 整合其他组件

1. redis

redis 也是我们项目中经常用到的 NoSQL,经常用来做做缓存什么的。

依赖

org.springframework.boot
spring-boot-starter-data-redis

application.properties

# Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址spring.redis.host=127.0.0.1# Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空)spring.redis.password=123456# 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=15# 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1# 连接池中的最大空闲连接spring.redis.pool.max-idle=15# 连接池中的最小空闲连接spring.redis.pool.min-idle=0# 连接超时时间(毫秒)spring.redis.timeout=0

2. Druid 数据源

针对监控而生的 DB 连接池

依赖

com.alibaba
druid
1.0.20

application.properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.filters=statspring.datasource.maxActive=20spring.datasource.initialSize=5spring.datasource.maxWait=60000spring.datasource.minIdle=1spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=select 'x'spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxOpenPreparedStatements=20

你的关注是我写博客最大的动力~

最后:

我们身为技术人员,最怕的就是安于现状,一直在原地踏步,那么你可能在30岁就会迎来自己的职业危机,因为你工作这么久提升的只有自己的年龄,技术还是万年不变!

我知道,对于一些学历没有优势的人来说,外包是别无选择,但是未来的路究竟要怎么走,取决你的步子迈多开。每个人都有自己的选择,如果你喜欢稳定,那按部就班适合你,但你有想法,不甘平庸,那就别让外包埋没了你。

如果你想在未来能够自我突破,圆梦大厂,那或许以上这份学习资料,你需要阅读阅读,希望能够对你的职业发展有所帮助。

最后,希望未来的我发展顺利,早日拿下p7!同样,也祝愿你实现自己的人生理想,愿我们都越来越好,共勉!

获取方式: 只需你**点赞+关注**后,Java进阶交流群:714827309 哦-!

获取方式: 只需你点赞+关注后,Java进阶交流群:714827309 进群拿资料哦-!

转载地址:http://qyegf.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-40》64.最小路径和
查看>>
Leetcode C++《热题 Hot 100-41》75.颜色分类
查看>>
Leetcode C++《热题 Hot 100-42》78.子集
查看>>
Leetcode C++《热题 Hot 100-43》94.二叉树的中序遍历
查看>>
Leetcode C++ 《第175场周赛-1 》5332.检查整数及其两倍数是否存在
查看>>
Leetcode C++ 《第175场周赛-2 》5333.制造字母异位词的最小步骤数
查看>>
Leetcode C++ 《第175场周赛-3》1348. 推文计数
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-45》338.比特位计数
查看>>
读书摘要系列之《kubernetes权威指南·第四版》第一章:kubernetes入门
查看>>
Leetcode C++《热题 Hot 100-46》739.每日温度
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>