javascript
教你十分钟构建好 SpringBoot + SSM 框架
來(lái)源:Howie_Y
https://juejin.im/post/5b53f677f265da0f8f203914
目前最主流的 java web 框架應(yīng)該是 SSM,而 SSM 框架由于更輕便與靈活目前受到了許多人的青睞。而 SpringBoot 的輕量化,簡(jiǎn)化項(xiàng)目配置, 沒(méi)有 XML 配置要求等優(yōu)點(diǎn)現(xiàn)在也得到了大眾的青睞。
而本文,我將教大家如何在?intellij idea?中快速構(gòu)建好一個(gè)?Maven + Spring + SpringMVC + MyBatis + SpringBoot?的框架,做到了足夠精簡(jiǎn),讓你可以立刻開始你的 web 項(xiàng)目。
附上這個(gè)簡(jiǎn)單的框架構(gòu)建的 github 地址 SSM-SpringBoot:
https://github.com/HowieYuan/SSM-SpringBoot
選擇 Spring Initiallizr
添加最基本的幾個(gè)依賴 Web,MySQL,MyBatis,其他需求可以后續(xù)再添加 ; 數(shù)據(jù)庫(kù)選擇了 MySQL
數(shù)據(jù)源中存儲(chǔ)了所有建立數(shù)據(jù)庫(kù)連接的信息
1. 配置 IDEA 數(shù)據(jù)源
輸入地址,端口,用戶名,密碼等等完成設(shè)置
2. 配置 spring 數(shù)據(jù)源
application.properties 文件添加:
spring.datasource.url = jdbc:mysql://xx.xx.xx.x:xxx/xxx?characterEncoding=utf8&allowMultiQueries=true&useSSL=false
spring.datasource.username = root
spring.datasource.password =?123456
spring.datasource.driver-class-name?= com.mysql.jdbc.Driver
url ?: 數(shù)據(jù)源 url ,格式為?jdbc:mysql://Host(主機(jī)名或 IP 地址):Post(端口)/Database(數(shù)據(jù)庫(kù)名稱),其中 allowMultiQueries = true : 允許多條 sql 同時(shí)執(zhí)行(分號(hào)分隔);useSSL : 是否進(jìn)行 SSL 連接,根據(jù)實(shí)際情況選擇
username : 用戶名
password : 密碼
driver-class-name : 驅(qū)動(dòng)名,不同的數(shù)據(jù)庫(kù)有不同的 Drivername,如 oracle 數(shù)據(jù)庫(kù)的?oracle.jdbc.driver.OracleDriver,MySQL 數(shù)據(jù)庫(kù)為?com.mysql.jdbc.Driver
使用 @Controller / @RestController 注解標(biāo)注一個(gè)控制器,表明這個(gè)類是作為控制器的角色而存在的
使用 @Service 注解標(biāo)注一個(gè)業(yè)務(wù)層類
使用 @Repository 注解標(biāo)注一個(gè)持久層 mapper 接口
使用 @Component 注解標(biāo)注其他組件
使用 @Configuration 注解標(biāo)注配置類
整個(gè)項(xiàng)目的構(gòu)建最主要的部分就是 springboot 和 mybatis 的整合,而springboot 也提供了十分方便的方式。
1. xml 文件
聲明為映射文件
namespace : 指該映射文件對(duì)應(yīng)的映射接口 ; 一般來(lái)說(shuō),一個(gè) XML 映射配置文件對(duì)應(yīng)一個(gè)命名空間,而這個(gè)命名空間又對(duì)應(yīng)一個(gè)接口
xml version="1.0"?encoding="UTF-8"?
<mapper?namespace="com.swit.dao.MyMapper">
</mapper>
2. application.properties
Mybatis 配置,指定了 mybatis 基礎(chǔ)配置文件和實(shí)體類映射文件的地址
mybatis.mapperLocations = classpath:mapper/**/*.xml
mybatis.typeAliasesPackage =?com.swit.model
配置 typeAliasesPackage 可以使得 com.swit.model 包內(nèi)的實(shí)體類可以在映射文件中使用別名,如:
<select?id="getUser"?parameterType="int"?resultType="User">
</select>
如沒(méi)有配置 typeAliasesPackage ,則需要?resultType="com.swit.model.User"
如果要對(duì) MyBatis 通過(guò) xml 文件進(jìn)行另外的配置,則添加文件路徑:
3. 添加對(duì) mapper 類的掃描
以下兩種方法二選其一
(1)可以選擇在啟動(dòng)類添加 @MapperScan
value 為 mapper 類所在的包(注意這里是包的路徑,而不是類的路徑!)
另外, @MapperScan 注解面向的是接口類,只要是加了注解的接口類都需要進(jìn)行通過(guò)該注解來(lái)掃描
(2)可以在每個(gè) mapper 類上添加 @mapper 注解
@Mapper
@Repository
public interface MyMapper {
}
到目前為止,你已經(jīng)完成了你的項(xiàng)目的構(gòu)建,下面我還會(huì)介紹些別的東西。
1. @SpringBootApplication
這個(gè)注解位于啟動(dòng)類
@SpringBootApplication 等價(jià)于以默認(rèn)屬性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan, 所以啟動(dòng)類無(wú)需再添加這三個(gè)注解
@Configuration :標(biāo)注一個(gè)類為配置類。
@EnableAutoConfiguration :開啟自動(dòng)配置。
@ComponentScan :自動(dòng)收集所有的 Spring 組件
2. 部署服務(wù)器
如果你想把自己的 SpringBoot 項(xiàng)目部署到阿里云,騰訊云等服務(wù)器,那么你還需要加點(diǎn)東西。?
1. 如果需要通過(guò)打包的方式在web容器中進(jìn)行部署,則需要繼承 SpringBootServletInitializer 覆蓋configure(SpringApplicationBuilder)方法
public?class?SpringbootApplication?extends?SpringBootServletInitializer?{
? ?public?static?void?main(String[] args)?{
? ? ? ?SpringApplication.run(SpringbootApplication.class, args);
? ?}
? ?
? ?protected?SpringApplicationBuilder?configure(SpringApplicationBuilder builder)?{
? ? ? ?// 注意這里要指向原先用main方法執(zhí)行的Application啟動(dòng)類
? ? ? ?return?builder.sources(SpringbootApplication.class);
? ?}?
}
2.pom 文件添加打包插件
<build>
? ? ? ?<!--打包后的項(xiàng)目名,url 前綴-->
? ?<finalName>projectName</finalName>
? ?<plugins>
? ? ?<plugin>
? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ?<artifactId>spring-boot-maven-plugin</artifactId>
? ? ?</plugin>
? ? ?<plugin>
? ? ? ?<groupId>org.apache.maven.plugins</groupId>
? ? ? ?<artifactId>maven-compiler-plugin</artifactId>
? ? ? ?<version>3.1</version>
? ? ? ?<configuration>
? ? ? ? ?<!--設(shè)置編譯時(shí)使用的 JDK 版本-->
? ? ? ? ?<source>1.8</source>
? ? ? ? ?<!--設(shè)置運(yùn)行時(shí)使用的 JDK 版本-->
? ? ? ? ?<target>1.8</target>
? ? ? ? ?<!--設(shè)置為 true 則跳過(guò)測(cè)試-->
? ? ? ? ?<skip>true</skip>
? ? ? ?</configuration>
? ? ?</plugin>
? ?</plugins>
?</build>
3. 你很有可能還需要做個(gè)跨域處理
public?class?CorsFilter?implements?Filter?{
? ?/**
? ? * json web token 在請(qǐng)求頭的名字
? ? */
? ?private?String tokenHeader =?"X_Auth_Token";
? ?
? ?public?void?doFilter(ServletRequest req, ServletResponse res, FilterChain chain)?throws?IOException, ServletException?{
? ? ? ?HttpServletResponse response = (HttpServletResponse) res;
? ? ? ?HttpServletRequest request = (HttpServletRequest) req;
? ? ? ?String token = request.getHeader("X_Auth_Token");
? ? ? ?System.out.println(token +?"token");
? ? ? ?String Origin = request.getHeader("Origin");
? ? ? ?System.out.println("Origin:"?+ Origin);
? ? ? ?System.out.println("tokenHeader:"?+?this.tokenHeader);
? ? ? ?Logger logger = Logger.getLogger(this.getClass());
? ? ? ?logger.info("Origin: ?"?+ Origin);
? ? ? ?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);
? ?}
? ?
? ?public?void?init(FilterConfig filterConfig)?{
? ?}
? ?
? ?public?void?destroy()?{
? ?}
}
1. redis
redis 也是我們項(xiàng)目中經(jīng)常用到的 NoSQL,經(jīng)常用來(lái)做做緩存什么的。
依賴
<dependency>
? ?<groupId>org.springframework.boot</groupId>
? ?<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties
?Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0)
spring.redis.database=0
?Redis服務(wù)器地址
spring.redis.host=127.0.0.1
?Redis服務(wù)器連接端口
spring.redis.port=6379
?Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password=123456
?連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
spring.redis.pool.max-active=15
?連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
spring.redis.pool.max-wait=-1
?連接池中的最大空閑連接
spring.redis.pool.max-idle=15
?連接池中的最小空閑連接
spring.redis.pool.min-idle=0
?連接超時(shí)時(shí)間(毫秒)
spring.redis.timeout=0
2. Druid 數(shù)據(jù)源
針對(duì)監(jiān)控而生的 DB 連接池
依賴
<dependency>
? ? ??<groupId>com.alibaba</groupId>
? ? ??<artifactId>druid</artifactId>
? ? ??<version>1.0.20</version>
</dependency>
application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=20
spring.datasource.initialSize=5
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select?'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
總結(jié)
以上是生活随笔為你收集整理的教你十分钟构建好 SpringBoot + SSM 框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 记腾讯互娱网站布局(3)
- 下一篇: JS 获取图片的原始尺寸