前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SSM,SpringBoot相关知识盲区整理

SSM,SpringBoot相关知识盲区整理

作者头像
大忽悠爱学习
发布2021-11-15 15:29:06
4470
发布2021-11-15 15:29:06
举报
文章被收录于专栏:c++与qt学习c++与qt学习

SSM,SpringBoot相关知识盲区整理


@ResponseBody详解

@ResponseBody详解 @RequestBody的使用


ResponseEntity详解

ResponseEntity的基本简介


@ModelAttribute运用详解

@ModelAttribute运用详解


@RequestParam无法获取参数

application/x-www-form-urlencoded是以表格的形式请求,而application/json则将数据序列化后才进行传递,如果使用了@RequestParam会在Content里面查找对应的数据,结果因为传递的数据已经被序列化所以不能找到,所以当要使用@RequestParam注解时候应当使用application/x-www-form-urlencoded,而如果想要使用application/json则应当使用@RequestBody获取被序列化的参数


@RequestParam和@RequestBody的区别

@RequestParam

① 支持POST和GET请求。 ② 只支持Content-Type:为application/x-www-form-urlencoded编码的内容。Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

@RequestBody

① 不支持GET请求。 ② 必须要在请求头中申明content-Type(如application/json)springMvc通过HandlerAdapter配置的HttpMessageConverters解析httpEntity的数据,并绑定到相应的bean上。

@RequestParam和@RequestBody的区别


Spring常用注解(绝对经典)

笔记


Spring中的Environment

Spring中的Environment


@Param注解的使用和解析

作用:用注解来简化xml配置的时候(比如Mybatis的Mapper.xml中的sql参数引入),@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中(一般通过#{}的方式,${}会有sql注入的问题)。

当你使用了使用@Param注解来声明参数时,如果使用 #{} 或 {} 的方式都可以,当你不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 {} 的方式,会报错。

当参数传递map时,也可以直接使用${}取值

不使用@Param注解时,最好传递 Javabean。在SQL语句里就可以直接引用JavaBean的属性,而且只能引用JavaBean存在的属性。

Mapper接口方法:

代码语言:javascript
复制
 public int getUsersDetail(User user);

对应Sql Mapper.xml文件:

代码语言:javascript
复制
 <!--这里直接引用对象属性即可,不需要对象.属性的方式--> 
<select id="getUserDetail" statementType="CALLABLE" resultMap="baseMap">
          Exec WebApi_Get_CustomerList #{userid}
</select>

当参数传递的是javabean时,又存在@param注解时,必须使用对象.属性名的方式:

代码语言:javascript
复制
 public int getUsersDetail(@param("u")User user);

对应Sql Mapper.xml文件:

代码语言:javascript
复制
 <!--这里直接引用对象属性即可,不需要对象.属性的方式--> 
<select id="getUserDetail" statementType="CALLABLE" resultMap="baseMap">
          Exec WebApi_Get_CustomerList #{u.userid}
</select>

JdbcType–指定当前javaBean属性对应数据库中的数据类型

JdbcType


parameterType 用法

parameterType 用法


@TableField注解可以起别名吗?

别名效果 value属性

  • 使用MP自动生成的方法时,可以用@TableField起别名
  • 在xml写sql时,需要用as或者resultMap来转化

防止字段持久化 exist属性

使用exist属性可以控制类中属性在表中是都一定对应,如果有不存在于数据表的字段,建议都设成fasle

总结:

手写的sql都不能自动生成别名

Mybatis Plus自动生成的sql可以起别名

原文


MybatisPlus中@TableField注解的使用

MybatisPlus中@TableField注解的使用


SpringBoot默认集成的Jackson框架,处理xml的还有xstream

Jackson快速入门

Java-Jackson使用详解


controller用pojo对象封装前端的请求参数—表单形式的请求

代码语言:javascript
复制
    //新增课程
    @PostMapping("/addCourse")
    public String addCourse(Course course)
    {
        System.out.println(course);
        return "error";
    }
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
@Data
@EqualsAndHashCode(callSuper = false)
public class Course extends Model<Course> {

    private static final long serialVersionUID = 1L;

    @TableField("courseName")
    private String coursename;

    @TableField("courseCoverPath")
    private String coursecoverpath;

    @TableField("courseVideoFilePath")
    private String coursevideofilepath;

      @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("courseVideotype")
    private Integer coursevideotype;

    @TableField("courseVideoTime")
    private Integer coursevideotime;

    @TableField("type")
    private  String type;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}
在这里插入图片描述
在这里插入图片描述

如果请求参数名,在pojo对象中找到了相同的属性名,则进行赋值,否则为null


@Value和@Bean注解的执行顺序问题

@Value和@Bean注解的执行顺序问题

@Value和@Bean注解不在同一文件下时,@Value先执行

在同一文件下时,@Bean先执行


@PropertySource和@Value一起使用,注入properties文件

  • 文件路径
在这里插入图片描述
在这里插入图片描述
  • 文件内容
在这里插入图片描述
在这里插入图片描述
  • 新建config文件
在这里插入图片描述
在这里插入图片描述

高级用法

使用@Value和@PropertySource注入外部资源

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
@Configuration
@ComponentScan("com.example.value.service") //扫包
@PropertySource("classpath:test.properties") //注意文件格式的指定
public class ElConfig {
    @Value("I Love You!") //1 注入普通字符串
    private String normal;

    @Value("#{systemProperties['os.name']}") //2 注入操作系统属性
    private String osName;

    @Value("#{ T(java.lang.Math).random() * 100.0 }") //3 注入表达式结果
    private double randomNumber;

    @Value("#{demoService.another}") //4  注入其他Bean的属性
    private String fromAnother;

    @Value("classpath:test.txt") //5  注入了文件资源
    private Resource testFile;

    @Value("http://www.baidu.com") //6   注入网页资源
    private Resource testUrl;

    @Value("${book.name}") //7   注入classpath:test.properties中资源项,注意美元符号$
    private String bookName;

    @Autowired
    private Environment environment; //7 属性也可以从environment中获取。

    @Bean //7
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }



    public void outputResource() {
        try {
            System.out.println(normal);
            System.out.println(osName);
            System.out.println(randomNumber);
            System.out.println(fromAnother);

            System.out.println(IOUtils.toString(testFile.getInputStream()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));
            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));
            System.out.println(environment.getProperty("book.school"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

注意:注入配置配件使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcePlaceholderConfigure的Bean


@ImportResource()注解的使用

@ImportResource注解用于导入Spring的配置文件,让配置文件里面的内容生效;(就是以前写的springmvc.xml、applicationContext.xml)

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上。

注意!这个注解是放在主入口函数的类上,而不是测试类上

该注解标注在主配置类上,用于加载我们自己手写的spring相关的配置文件

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dog1" class="com.yangzhenxu.firstspringboot.bean.Dog">
        <property name="name" value="zhangxue"/>
        <property name="age" value="27"/>
    </bean>

</beans>

必须是spring格式的配置文件,否则也会报错

@ImportResource()注解的使用


加载.properties配置文件的三种方式

加载.properties配置文件的三种方式

Properties类,配置文件,文件路径


Mybatis-Plus事务管理

Mybatis-Plus事务管理

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-09-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SSM,SpringBoot相关知识盲区整理
  • @ResponseBody详解
  • ResponseEntity详解
  • @ModelAttribute运用详解
  • @RequestParam无法获取参数
  • @RequestParam和@RequestBody的区别
  • Spring常用注解(绝对经典)
  • Spring中的Environment
  • @Param注解的使用和解析
  • JdbcType–指定当前javaBean属性对应数据库中的数据类型
  • parameterType 用法
  • @TableField注解可以起别名吗?
  • MybatisPlus中@TableField注解的使用
  • SpringBoot默认集成的Jackson框架,处理xml的还有xstream
  • controller用pojo对象封装前端的请求参数—表单形式的请求
  • @Value和@Bean注解的执行顺序问题
  • @PropertySource和@Value一起使用,注入properties文件
    • 高级用法
    • @ImportResource()注解的使用
    • 加载.properties配置文件的三种方式
    • Mybatis-Plus事务管理
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档