springboot学习笔记

常用注解:

控制器
@Controller
控制器,处理http请求

请求参数
@RequestMaping()
将http请求响应到控制器,required不允许值为空
@GetMapping,@PostMapping
获取http的get,post请求
@PathVariable
获取url中的数据
@RequestParam(value="",required=“false/true”)
获取请求参数的值
@CookieValue
获取Cookie值

注入Bean
@Service
作用在类上,被@Service标记的类会被Spring扫描并注入为Bean
@Bean
表面生产一个bean方法,并交给spring容器管理
@Autowired
Bean的自动注入

数据库实体类
@Entity
实体类注解
@Table(name="")
注释在实体类上,对应数据库中相应的表
@Id
注释在实体类参数标注为数据库表Id
@Column
注释在实体类参数标注为数据库表参数
@GeneratedValue(strategy=GenerationType.IDENTITY)
注释在实体类参数标注为数据库为自增
@JsonIgnoreProperties({“hibernateLazyInitializer”})
注释在数据库实体类中防止数据类型出错

导入配置文件
@component
把普通的pojo实例化到spring容器中,相当于配置文件
@PropertySource
引入properties文件
@Import
导入额外的配置信息

异常处理
@ControllerAdvice
同一处理异常
@ExceptionHandler
注解异常处理方法

AOP

public class 名称 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //实现拦截器
        Object user=request.getSession().getAttribute("session");
        if (user!=null){
            return true;
        }
        request.setAttribute("键","值");
        request.getRequestDispatcher("跳转页面").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
//cookie和session
    public String login(HttpSession httpSession,
                        HttpServletResponse response
                        ){
 httpSession.setAttribute(,); //创建session
 
 //创建cookie
Cookie cookie= new Cookie(,);
response.addCookie(cookie);
}
@Configuration
public class MyMvcconfig implements WebMvcConfigurer {

    @Override//重定向
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("").setViewName("");
    }

    @Override//aop的实现拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new 拦截器类).addPathPatterns("拦截页面")
                .excludePathPatterns(“放过页面”);
   
    }


}

一些有用的yml配置

spring:
  #数据库配置
  datasource:
    url: jdbc:mysql://地址/数据名
    username: 数据库用户名
    password: 数据库密码
    driver-class-name: com.mysql.jdbc.Driver 数据库驱动(新版本有个时区设置这里使用旧版本)

	  #jpa配置
  jpa:
    hibernate:
      #更新或创建数据表
      ddl-auto: update
      #控制台显示sql
    show-sql: true
 
   #thymleaf 禁用缓存
  thymeleaf:
      cache: false

#手动配置springboot put请求
  mvc:
    hiddenmethod:
      filter:
        enabled: true

一些常用的资源包xml配置

<!--引入thymleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!--引入bootstrap 具体前端框架搜索webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.4.1-1</version>
         </dependency>
         <!-- jpa-->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
         <!-- 使用旧版mysql驱动-->
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
            <scope>runtime</scope>
        </dependency>

jpa

//继承JpaRepository来完成对数据库的操作
public interface 接口名称 extends JpaRepository<实体类名称,主数据类型>, JpaSpecificationExecutor<实体类名称> {

}

jpa操作文档

thymeleaf

th:if="${}"判读语句
th:text="${}"显示参数语句,可以使用三元运算
th:herf="@{}"链接
th:src="@{}"链接
th:fargment="名称"重用的基础页面
th:replace="{地址::名称}"替换页面

更加完善的笔记

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值