spring boot入门笔记

一、spring boot简介:

Spring Boot是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。同时它集成了大量常用的第三方库配置,大部分的 Spring Boot 应用都只需要非常少量的配置代码(基于 Java 的配置)
spring boot的好处:
	配置、编码、部署都非常的简单,化繁为简,微服务的入门框架

二、spring boot容器

	spring容器提供了bean的创建和管理,全程使用注解 操作方式简化。
	创建一个普通maven项目
	pom文件内容添加:
		   <parent> 
		        <groupId>org.springframework.boot</groupId> 
		        <artifactId>spring-boot-starter-parent</artifactId> 
		        <version>2.0.6.RELEASE</version> 
		    </parent>
	添加spring mvn依赖	
		  <dependency> 
		        <groupId>org.springframework.boot</groupId> 
		        <artifactId>spring-boot-starter-web</artifactId> 
		  </dependency>
	下面会用到的注解:
		@SpringBootApplication他包括三个注解:
			1、@Configuration:表示将该类作用springboot配置文件类
			2、@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置
			3、@ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类
						
	创建一个cn.ps包 创建一个Boot类
package cn.ps;
		import java.util.HashMap;
		
		import org.springframework.beans.factory.annotation.Autowired;
		import org.springframework.boot.SpringApplication;
		import org.springframework.boot.autoconfigure.SpringBootApplication;
		import org.springframework.web.bind.annotation.GetMapping;
		import org.springframework.web.bind.annotation.RequestMapping;
		import org.springframework.web.bind.annotation.RestController;
		
		import cn.ps.entity.User;
		
		@RestController
		@SpringBootApplication
		public class Boot {
			
			@Autowired
			private User user;
			
		    @GetMapping("/")
		    String home() {
		        return "Hello World!";
		    }
		    @RequestMapping("/qwe")
		    HashMap home1() {
		    	HashMap map=new HashMap();
		    	map.put("id", 1);
		        return map;
		    }
		
		    public static void main(String[] args) throws Exception {
		        SpringApplication.run(Boot.class, args);
		    }
		
		}
	在主类Boot中运行main方法启动默认窗口8080,运行后可以在网页上直接检验,不需要上下文路径
	
	添加springmvc的控制类 cn.ps.controller 创建java类 默认springboot只会扫描启动类相同包或者子包中带有注解的bean
		
	springboot集成redis
	首先引入pom.xml
		<dependency> 
		        <groupId>org.springframework.boot</groupId> 
		        <artifactId>spring-boot-starter-data-redis</artifactId> 
		</dependency>

@Value直接获取所有导入的资源文件 默认资源文件 application.properties
@PropertySource 指定加载的properties文件 这些加载的文件和 application.properties的键值对
都可以使用 @Value获取 已经加载的后面的不能覆盖
@ConfigurationProperties 指定就某些前缀的资源 自动装配到属性中 不需要使用 @Value注解

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=ps123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

添加控制类redisController

package cn.ps.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisControlller {
	
	@Autowired
	StringRedisTemplate str;
	
	@GetMapping("/redis")
	public String redis() {
		str.boundValueOps("sex").set("N");
		return str.boundValueOps("sex").get();
	}
}

引入mysql依赖

	<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		 
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		</dependency>
创建jdbc控制类jdbcController
package cn.ps.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.pool.DruidDataSource;
@RestController
public class JdbcControlle {
	
	@Autowired
	JdbcTemplate jdbc;
	
	@Autowired
	DruidDataSource dru;
	
	@GetMapping("/jdbc")
	public List<Map<String, Object>> redis() {
		List<Map<String, Object>> query = jdbc.query("select * from emp", new ColumnMapRowMapper());
		return query;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值