记录springboot的学习内容

SpringBoot的学习

有错误的地方欢迎指出(之后会补充springMVC 的内容)

基本了解

  • 之前spring, springmvc和mybatis是对于业务和controller和dao的封装,减少了代码量但同时也引出了大量的配置文件。所以springboot就是解决大量配置文件出现的问题,对配置文件做了一个封装。

基本使用方法

  1. 在springboot 官网 配置工程名和一些信息(需要的jar包)
    列如:
    官网生成springboot工程的操作

  2. 下载并解压到工作空间,并导入到eclipse中
    记住选择第二个导入工程

  3. 不需要大量的配置文件,只需在application.properties配置文件中配置所需的东西即可

配置文件application.properties

--数据库的配置
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:PZB
spring.datasource.username=JF190805
spring.datasource.password=JF190805
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver


--freemarker视图的配置
spring.freemarker.enabled=true
spring.freemarker.template-loader-path=classpath:/static/
spring.freemarker.prefix=
spring.freemarker.suffix=.html
spring.freemarker.content-type=text/html
spring.freemarker.charset=UTF-8
spring.freemarker.settings.tag_syntax=auto_detect

--配置端口号
server.port = 8005

  1. 配置文件完成后将JSP更改为HTML,并摒弃之前JSP留下的内容(例如EL表达式)
  2. 在提供的main方法里run即可,无需再打开server启动tomcat
  3. 多个工程时可以在配置文件中修改端口来避兔冲突 server.port = 8005

springBoot的拦截器和导包

拦截器

  1. 与之前的springMVC一样实现HandlerInterceptor接口,重写方法:
package org.peng.intercepter;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.peng.bean.ResultType;
import org.springframework.web.servlet.HandlerInterceptor;

import com.alibaba.fastjson.JSONObject;

public class LoginIntercepter implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		HttpSession session = request.getSession();
		Object data = session.getAttribute("user");
		
		if(data != null) {
			session.setAttribute("user", data);
			return true;
		}
		if(isAjaxRequest(request)) {
			
			JSONObject jsonObject = new JSONObject(); 
			jsonObject.put("resultType",2);
			jsonObject.put("msg","您还没有登录,请先登录!");
			
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(jsonObject);
			response.getWriter().close();
	
		}else {
			
			response.sendRedirect("index.jsp");
			
		}
		return false;
	}
	
	
	//判断请求是否为ajax(可以封装为一个utils类)
	public boolean isAjaxRequest(HttpServletRequest request){  
	    String header = request.getHeader("X-Requested-With");  
	    boolean isAjax = "XMLHttpRequest".equals(header) ? true:false;  
	    return isAjax;  
	}  
	
}
  1. 写一个配置类实现WebMvcConfigurer接口,重写addInterceptors方法
    注意:在new出对象拦截器时重写一个方法来new拦截器,用@Bean注解
    JAVA代码如下:
package org.peng.config;

import org.peng.intercepter.LoginIntercepter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer{

	@Bean
	public LoginIntercepter getLoginIntercepter() {
		return new LoginIntercepter();
	}
	
    //与springMVC里的配置一样(new拦截器,拦截对象和开放对象)
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(getLoginIntercepter()).addPathPatterns("/*.ajax").excludePathPatterns("/bgUserLogin.ajax");
	}
	
}

导包

  1. 先在pom.xml里配置build内容:
<resources>
    <!-- 创建工程时引入Mapper的xml文件 -->
    <resource>
        <targetPath>${project.build.directory}/classes</targetPath>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*Mapper.xml</include>
        </includes>
    </resource>	
        
    <!-- 引入resources里的配置文件 -->
    <resource>
        <targetPath>${project.build.directory}/classes</targetPath>
        <directory>src/main/resources</directory>
        <includes>
            <include>**</include>
        </includes>
    </resource>	

</resources>
  1. 选中pom.xml右键选择run as 的maven builde,在构建时输入clean package来构建工程
  2. 构建完成后会看到 build success 的标志
  3. 刷新target的目录会看到一个构建出来的jar包
  4. 打开cmd用jar来运行刚才的jar包,cmd运行结果
Microsoft Windows [版本 10.0.18362.657]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\hp>java -jar project.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

可以在cmd里看到开放的测试端口:Tomcat started on port(s): 8080 (http) with context path ''

  1. 打开浏览器输入测试地址加端口号即可127.0.0.1:8080/index.html
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值