springboot整合框架(一)

springboot整合web

在pom中添加web的整合jar依赖即可。

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

springboot整合FastJson

添加jar依赖

<!-- fastjson依赖 -->
    <dependency>
      		<groupId>com.alibaba</groupId>
      		<artifactId>fastjson</artifactId> 
      		<version>1.2.15</version>
    </dependency>  

配置FastJson

方式一:让启动类继承WebMvcConfigurerAdapter

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication(scanBasePackages="com.yunxiang")
public class SpringApplications extends WebMvcConfigurerAdapter{

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		//创建FastJson的信息转换器
		FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
		
		//创建FastJson的配置对象
		FastJsonConfig config = new FastJsonConfig();
		//将json数据进行格式化
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		//将配置添加到信息转换器中
		convert.setFastJsonConfig(config);
		//将FastJson的信息转换器添加到HttpMessageConverter中
		converters.add(convert);
		
	}
	//springboot的入口
	public static void main(String[] args) {
		/*
		 * 第一个参数,springboot入口所在类的字节码
		 * 第二个参数,就是字符串数组的参数名
		 */
		SpringApplication.run(SpringApplications.class, args);
	}
}

注:此过程会出现乱码,但是在springboot的全局配置文件中开启response即可。

spring.http.encoding.force=true

方式二:使用@Bean注入

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;


import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication(scanBasePackages="com.yunxiang")
public class SpringApplications {

	@Bean
	public HttpMessageConverters fastJsonMessageConverter(){
		
		//创建FastJson的信息转换器
		FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
		
		//创建FastJson的配置对象
		FastJsonConfig config = new FastJsonConfig();
		//将json数据进行格式化
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		
		convert.setFastJsonConfig(config);
		
		HttpMessageConverter<?> con = convert;
		
		return new HttpMessageConverters(con);
	}
	
	
	
	public static void main(String[] args) {
		/*
		 * 第一个参数,springboot入口所在类的字节码
		 * 第二个参数,就是字符串数组的参数名
		 */
		SpringApplication.run(SpringApplications.class, args);
	}
}

springboot整合jsp

<!-- springboot整合jsp,需要是war工程,另外需要依赖另外两个包 -->
      <dependency>
      	  <groupId>org.springframework.boot</groupId>
      	  <artifactId>spring-boot-starter-tomcat</artifactId>
      </dependency>
       <dependency>
      	  <groupId>org.apache.tomcat.embed</groupId>
      	  <artifactId>tomcat-embed-jasper</artifactId>
       </dependency>


<!-- maven项目为war类型时,要加这个插件后,可以不参加web.xml -->
  <build>
      <plugins>
          <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-war-plugin</artifactId>
             <version>2.3</version>
             <configuration>
                 <failOnMissingWebXml>false</failOnMissingWebXml>
             </configuration>
          </plugin>
      </plugins>
</build>

springboot整合Freemarker

springboot官方不建议使用jsp,建议使用模板引擎(themleaf,velocity,freemarker等)。在默认的情况下Freemarker的文件以".ftl"结尾,文件存放在"src/main/resources"的"classpath"路径下的"templates"目录下。

	<!-- Freemarker依赖 -->
	  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>

Freemarker的默认配置信息,如果需要修改,就在springboot的全文配置文件中修改

#springboot整合freemarker的默认配置信息
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.settings.classic_compatible=true

Freemarker的模板

<html>
	<head>
	</head>
	<body>
		${name}
	</body>
</html>

SpringBoot整合Thymeleaf

<!-- thymeleaf的依赖 -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <!-- 解决thymeleaf使用非严格的html出现的异常 -->
  <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
</dependency>

springboot整合thymeleaf的配置信息(必须写在springboot的核心配置文件中)

#springboot整合thymeleaf
#关闭thymeleaf缓存 开发时使用 否则没有实时画面
spring.thymeleaf.cache=false
## 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template-location=true
#Content-Type值
spring.thymeleaf.content-type=text/html
#启用MVC Thymeleaf视图分辨率
spring.thymeleaf.enabled=true
#应该从解决方案中排除的视图名称的逗号分隔列表
spring.thymeleaf.excluded-view-names=
#模板编码
spring.thymeleaf.mode=LEGACYHTML5
# 在构建URL时预先查看名称的前缀
spring.thymeleaf.prefix=classpath:/templates/
# 构建URL时附加查看名称的后缀.
spring.thymeleaf.suffix=.html
# 链中模板解析器的顺序
#spring.thymeleaf.template-resolver-order= o
# 可以解析的视图名称的逗号分隔列表
#spring.thymeleaf.view-names=
#thymeleaf end

thymeleaf模板

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1 th:text="${name}"></h1>
<a th:text="${name}"></a>
</body>
</html>

注:在thymeleaf模板中,使用"th:text=“ " " 来 接 收 数 据 , 但 是 " t h : t e x t = " {}" "来接收数据,但是"th:text=" """th:text="{}” "必须写在HTML的标签中,否则无法准确接收数据。

springboot整合QuartZ

springboot整合QuartZ时,需要在springboot的启动类上开启任务调度。
springboot的启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(scanBasePackages="com.yunxiang")
//开启任务调度
@EnableScheduling
public class SpringApplications {

	public static void main(String[] args) {
		
		SpringApplication.run(SpringApplications.class, args);
	}
}

MyJob

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//创建任务,并且将创建的任务存储在springboot中
@Component
public class MyJob {
	
	//任务执行的时间间隔
	//秒、分、时、日、月、年
	//@Scheduled(cron = "0 0 0 0/1 * ?")
	@Scheduled(fixedRate=1000)
	public void run(){
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}
}

springboot整合JdbcTemplate

<!-- springboot整合jdbctemplate -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-jdbc</artifactId>
       </dependency>
<!-- mysql的依赖 -->       
<dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>

数据库配置文件(配置在springboot的全局配置文件中)

#配置数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值