1、通过maven创建springboot项目
mvn archetype:generate -DinteractiveMode=false -DgroupId=com.xcy -DartifactId=springboot -Dversion=1.0.0-SNAPSHOT
2、创建完成后,用eclipse导入选maven导入创建的springboot项目
idea修改maven的依赖jar包路径:在setting里面 找到maven的配置进行修改
idea-springboot
注:springboot中多用到组合注解,如@Controller与@ResponseBody组合成RestController 、@RequestMapping与method=Get 组合成 GetRequestMapping
1、配置文件(.properties/.yml/.yaml)
1、通常用.properties文件
多配置文件时,需要在properties.properties主配置文件里面添加 spring.profiles.active=xxx 激活语句,值设置为新增配置文件的 properties-xxx.properties 取properties-后面的名称
如果在主配置文件与激活配置文件里面有一样的配置,以激活配置文件里面的配置为主
如激活文件中没配置的文件,则以主配置文件为主
2、自定义配置文件
在配置文件中添加自定义的键值,如:jing.niki=niki
取值时有两种取值方式
a.通过给静态变量赋值
@Value("${jing.niki}")
private String niki;
b 通过成员变量赋值,用get、set将定义的值取出来
新增一个成员变量类,定义需要取出的变量 给类名加上
2、使用jsp
1、需要在核心配置文件POX.xml中添加引用
<!-- 引入spring boot 内嵌的Tomcat对JSP的解析包 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- servlet依赖的jar包start -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- jsp依赖jar包start -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
2、将配置文件、xml、jsp或者其他文件放在class目录下(将指定目录文件下的文件放入class目录下)
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
3、需要在application.properties文件中添加前缀后缀
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
3、集成myBatis
1、添加mybatis和jdbc的依赖
<!-- 加载mybatis整合springboot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<!-- 父级没有指定时需要给定版本号 -->
<version>1.3.1</version
</dependency>
<!-- mysql的jdbc驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- oracle的jdbc驱动包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
2指定mybatis的配置文件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 配置文件的位置 -->
<configurationFile>xxxx.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
3在配置mapper.xml文件时,指定的sql语句中,采用parameterType="hashmap" resultType="java.util.HashMap" 可将查询指定的表而不用特定在文件中写表相关的配置
4、springboot的事物管理
1、在main方法类中添加注解@EnableTransactionManagement (表示开启事物支持)
2、是用事物时,在访问数据的service上添加注解@Transactional即可
5、restfull的使用
1、在RequestMapping的value访问地址后面添加一个'/{id}'
2、在访问方法里面 用注解@PathVariable('id') 去取数据
6、热部署插件的应用
1、引用依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2、如果改动代码直接点击build 可直接运行代码
3、热部署有可能不是每次改动代码都会重启,build后执行结果没有改变时,需要手动重启
7、集成redis
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、添加配置
spring.redis.host=10.221.1.89 //ip
spring.redis.port=6380 //端口
spring.redis.password= //密码(一般没有)
3、在service中添加redis的注入及引用
@Autowried
private RedisTemplate<Object,Object> redisTemplate;
RedisTemplate里面包含多个方法opsForValue().get(key) 可以获取redis里面对应key的value,而set(key,value)进行数据的存放
4、添加字符串的序列化器
RedisSerializer redisSerializer = new StringRedisSerializer();
redisTemplate.setStringSerializer(redisSerializer);来实现
5、缓存击穿问题:
1、同一时间内对同一个redis数据进行读取操作时,一部分数据会直接跳过redis直接通过DB获取获取,增加了DB的压力
2、解决方案:增加同步锁,用synchronized 解决缓存击穿问题
6、哨兵模式
改properties里的redis配置项:
#redis集群中的哨兵模式
spring.redis.password=xxxx
#哨兵配置文件
spring.redis.sentinel.master=mymaster
#哨兵的节点,连接的多个redis的ip+port用','相隔
spring.redis.sentinel.nodes=10.221.1.89:6379,10.221.1.90:6379
8、集成dubbo
可参考:https://github.com/alizbaba 上搜索boot 可找到dubbo-spring-boot-starter
集成dubbo主要写三个项目
1、接口项目(maven项目)
2、服务提供项目(springboot项目)
3、服务消费项目(springboot项目)
1、接口项目
1、编写方法接口
2、编成jar包已供余下两个项目使用
2、服务提供项目(springboot项目)
1.添加dubbo依赖
<!--dubbo的起步依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<!-zookeeper的客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-连接数据时,需要添加数据的相关依赖-->
2、添加properties配置文件(得先将zookeeper服务启动)
server.port=8080
spring.dubbo.appname=springboot-dubbo-provider
spring.dubbo.registry=zookeeper://10.221.1.89:2181
3、编写实现类
3、服务消费项目(springboot项目)
1、添加依赖(与项目二类似)
2、添加配置
server.port=9090
#dubbo相关配置
spring.dubbo.appname=springboot-dubbo-comsumer
spring.dubbo.registry=zookeeper://10.221.1.89:2181
3、编写controller
1、添加接口项目的接口,用注解@Reference (注:如果在接口的实现类里面duibbo的service注解配置了版本号,此处的注解也一定要配置版本号)
2、编写访问方法
9、springboot使用springmvc的拦截器
1、新建一个拦截器实现HandlerIntercept ,在方法前的拦截里面 打印一个句话
2、新建一个配置类
1、继承WebMvcConfigurerAdapter 重写父类的方法addInterceptors(InterceptorRegistry registry)
2、在类上添加注解@Configuration使其类变成一个配置类
3、在方法addInterceptors中 调用registry.addInterceptor(“入:参即为新建的拦截器”)后面可加拦截路径方法.addPathPatterns(“入参:拦截路径数组”)不拦截路径方法excludePathPatterns(“入参:不拦截路径数组”)
10、springboot使用servlet
在main方法上添加servlet注解扫描 @ServletComponentScan(basePackages = "com.niki.pang.servlet")指定扫描路径
1、方式一
1、新建一个servlet==》MyServlet 添加注解 @WebServlet(urlPatterns = "/servlet/myServlet")
2、继承HttpServlet 重写doget和dopost方法 把方法序列化
2、方式二
1、新建一个servlet ==》Heservlet
2、继承HttpServlet 重写doget和dopost方法
3、新建一个Servlet配置类,添加注解@Configuration
4、新建方法并添加@Bean注解
@Bean
public ServletRegistrationBean heServletRegistrationBean(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HeServlet(),"/servlet/heServlet");
return registrationBean;
}
11、springboot使用filter
与使用servlet大致相同
1、在main方法中添加扫描路径使用@ServletComponentScan(basePackages = "com.niki.pang.filter")
2.方式一:
1、新建一个filter 添加注解@WebFilter(urlPatterns="/*")
2、实现Filter 重写实现类方法
3、在doFilter方法中添加过滤内容,并调用doFilter(request,response)
3、方式二
1、新建一个filter,实现Filter
2、新建一个filter配置类,添加方法FilterRegistrationBean,并添加@Bean注解
@Bean
public FilterRegistrationBean heFilterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new HeFilter());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}