springboot知识小总结2

本文总结了SpringBoot的自动配置特性,包括扫描`application.properties`,使用`@ConfigurationProperties`进行配置注入,以及在Controller层中`@Controller`与`@RestController`的区别,`@PathVariable`、`@RequestParam`、`@RequestBody`参数接收方式,和SpringBoot+Mybatis的多表查询基础操作。链接提供了更多详细信息。

1.springboot自动扫描application.properties,不需要配置,扫描自定义配置文件,只需要写一个类,加上@Configuration+@PropertySource("")就可以,里面不需要任何内容,需要获取时,用@Value("${key}")获取。当然配置文件要在classpath下,(https://blog.youkuaiyun.com/qq_31351071/article/details/81006760) 如:

@Configuration
@PropertySource("classpath:a.properties")
public class TestConfig1 {

}

@Value("${a}")
	private int a;

2.controller层中:
@Controller和@RestController的区别:@Controller标识:返回前端页面,要返回数据,在@RequestMapper下加上@ResponseBody;@RestController标识:返回数据。如:

@RequestMapping("/fa")
	@ResponseBody
	public List<User> findAll() {
		return user.findAll();
	}

3.@ConfigurationProperties(prefix = “test”):自动扫描application.yml(或者properties),将配置文件的属性值自动赋值到Bean中,可以操作。注意:一定要带上get/set方法。

//可以是@Configuration(其底部有@Component)
@Component
@ConfigurationProperties(prefix = "test")
public class TestBean1 {
	
	private String b;
	private String c;
	
	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

	public String getC() {
		return c;
	}

	public void setC(String c) {
		this.c = c;
	}
	
}
//也可以调用这个类
@EnableConfigurationProperties({TestBean1.class})
public class DruidConfig {
    @Autowired
    private TestBean1 properties;
    }

@ConfigurationProperties(prefix = “test”) 不仅可以自动装配application.properties,也可以装配自定义配置文件 ,只需要和
@PropertySource(“classpath:a.properties”)配合使用即可,如:

@Configuration
@PropertySource("classpath:a.properties")
@ConfigurationProperties(prefix = "test")
public class TestBean1 {
	
	private String b;
	
	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

	
}

4.关于Controller接收前端页面的参数:
1>.@PathVariable:即url/{param}这种形式
2>.@RequestParam:般我们使用该注解来获取多个参数,在()内写入需要获取参数的参数名即可,一般在PUT,POST中比较常用。
3>.@RequestBody:该注解和@RequestParam殊途同归,我们使用该注解将所有参数转换,在代码部分在一个个取出来,也是目前我使用到最多的注解来获取参数(因为前端不愿意一个一个接口的调试)例如下代码:

@PostMapping("/createUserByMap")
public void createUserByMap(@RequestBody Map<String,Object> reqMap){
    String tel = reqMap.get("tel").toString();
    String pwd = reqMap.get("pwd").toString();
    userService.createUser(tel,pwd);
}

4>.使用对象获取

/**
 * 添加用户2
 * @param userInfo
 */
@PostMapping("/createUser2")
public void createUser2(UserInfo userInfo){
    userService.createUser(userInfo.getTel(),userInfo.getPassWord());
}

5>.参数与传的参数一致即可,如:

http://localhost:987/fu1?ids=2
	@RequestMapping("/fu1")
	@ResponseBody
	public User findUserById1(String ids) {
		String id="2";
		return user.findById(ids);
	}

5.springboot+mybatis多表查询
1>注解:

//一对一查询
public interface UserRepository {
    @Select("SELECT * FROM `user` where id = #{id}")
    @Results({
            @Result(property = "address", column = "address_id",
                    one = @One(select = "com.kingboy.repository.address.AddressRepository.findAddressById"))
    })
    User findUserWithAddress(Long id);
}
//一对多查询
public interface UserRepository {
    /**
     * 查询带有车信息的用户===============演示一对多(关于多对多其实就是两个一对多组成)
     */
    @Select("SELECT * FROM `user` WHERE id = #{id}")
    @Results({
            @Result(property = "cars", column = "id",
                    many = @Many(select = "com.kingboy.repository.car.CarRepository.findCarByUserId"))
    })
    User getUserWithCar(Long id);
}


2>xml文件:

//1
<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>
//2
//select语句
<!-- Very Complex Statement -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
  select
       B.id as blog_id,
  from Blog B
       left outer join Author A on B.author_id = A.id
       left outer join Post P on B.id = P.blog_id
       left outer join Comment C on P.id = C.post_id
       left outer join Post_Tag PT on PT.post_id = P.id
       left outer join Tag T on PT.tag_id = T.id
  where B.id = #{id}
</select>
<!-- 超复杂的 Result Map -->
<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <!-- 一对一 -->
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
    <!-- 一对多 -->
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>
总结:注解的@Results==<requestMap></requestMap>,sql语句怎样写都行(连接查询,无论单表还是多表)
注解的@Requests和@Request就是提供一个容器(场所),存放数据,和sql语句的实现无关(就是说,sql语句无论怎样写,只看结果)
,如左连接查询,即使一条数据对应多条数据,就可以用注解many或者xml的collection。

springboot知识小总结1:https://blog.youkuaiyun.com/weixin_43075298/article/details/88662097
不定时更新!

学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring Boot与Web开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值