杂七杂八整理(随笔2)

时间:2019-02-18
本文章向大家介绍杂七杂八整理(随笔2),主要包括杂七杂八整理(随笔2)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

thymeleaf:

1.thymeleaf有什么好处
  因为thymeleaf是html格式的,可以本地打开。美工和开发人员都可以访问,美工打开的是静态页面,开发人员打开的是动态页面。
  
2.如何使用thymeleaf
  pom.xml 添加spring-boot-starter-thymeleaf
  thymeleaf和jsp功能一样,不能同时存在。
  thymeleaf文件默认放在resources/templates目录下
  修改thymeleaf的目录:
  springboot默认配置文件中spring.thymeleaf.prefix
  新建html文档,默认是html5格式的
  如何声明th前缀:
  在html标签声明th前缀 xmlns:th="http://www.thymeleaf.org"
3.th:text作用
  <p th:text="${text}">123<p>
  用变量表达式中text的值替换<p>标签的内容
  th:object 作用
  <div th:object="${user}"></div>
  声明了一个本地的对象,在<div>标签中可以用选择表达式*{}使用该对象
  
4.变量表达式${}
  从request,session,application中获取变量值
  request中有key-->zhangsan    ${key}
  session中有name-->myUserName    ${session.name}
  servletContext中school-->buba   ${application.school}
  request中有user-->{userName:'',password:''}
     ${user.userName}   ${user.password}
5.选择表达式*{} 从指定的对象中获取变量(属性) 
  request中有user-->{userName:'',password:''} 
  <div th:object="${user}">
     *{userName}   *{password}
  </div>
  
6.消息表达式 #{}
   一般用来做国际化i18n   xxx_en_US    xxx_zh_CN
   读取resources目录下的.properties文件
   
   #resources目录下消息文件(.properties),不加语言修饰的基本的名称
   spring.messages.basename=messages
   
   定义一个区域解析器,区域解析器设置的是什么国家,就会读取哪个国家的消息文件

7.链接表达式  @{}用在所有的路径上
  可以自动添加上下文路径
  如何带参数  th:href="@{/user(id=1)}"


springboot:

1.springboot优点

1)自动化配置

2)依赖starter模块,简化配置

3)内嵌tomcat

   java -jar xx.jar  

4) 完全兼容spring框架

 

2.创建一个springboot工程

1)pom文件继承父工程   spring-boot-starter-parent

2) 启动类

3)默认的配置文件

 

3.启动类

 1)注解@SpringbootApplication

 2) main方法   SpringApplication.run(当前类的class对象);

 

4. @SpringbootApplication功能

1) @SpringbootConfiguration  

当前类是配置类,

和@Configuration功能一致,

@SpringbootConfiguration继承于@Configuration

2) @EnableAutoConfiguration

  自动化配置

3) @ComponentScan

  扫描当前包及其子包下所有的组件

 

5.默认配置文件

  application.properties   application.yml

  在resources

  server.port=8088

  server:       yml格式:冒号 换行 缩进

    port: 8088           参数值之前一个空格

 

6.springboot中如何使用单元测试

1)pom.xlm中引入spring-boot-starter-test模块

2)类上的注解有@SpringbootTest  @RunWith(SpringRunner.class)

 

7.读取默认配置文件中定义的参数

1)映射为简单参数

   属性上使用@Value(“${参数名的全称}”)

2)映射为一个pojo

   类上的注解为@ConfigurationProperties(prefix=””)   前缀

   类的属性名和配置文件参数前缀后的内容匹配,可以自动映射。

 

8.如何读取自定义配置文件中的内容

1)类上注解@PropertySource(value={“classpath:xx.properties”,“classpath:”})

2) 类属性上用@Value映射

 

9.springboot的静态资源

 resources  static  public  META-INF/resources

 

10.springboot中能使用spring的配置文件吗?

  能

1)创建一个配置类,  类上用@SpringbootConfiguration

2) 类上用@ImportResource(locations={”classpath:spring配置文件的路径”})

 

11.springMVC controller中有几个组合注解

1)@RestController==@Controller+@ResponseBody

2) @GetMapping==@RequestMapping(method=RequestMapping.GET)

3) @PostMapping

4) @PutMapping

5) @DeleteMapping

 

12.什么是restful

是一个设计思想,是一个约定,把网络上的资源和url一一对应,对资源的增删改查是通过四个请求方式区分的。get查询 put修改 post添加 delete删除

http://localhost:8080/user  get  查询所有用户http://localhost:8080/user/5

http://localhost:8080/user  post  添加用户

http://localhost:8080/user  put   修改用户

http://localhost:8080/user  delete 删除所有用户  http://localhost:8080/user/1

   如何获取url路径上的参数

   在controller方法中   findUserById(@PathVariable()){}

 

13.spring装配bean的方式,IOC的方式

1)注解  @Controller @Service  @Repository  @Component

2) spring配置文件的方式  <bean>

3)java配置类   类上@Configuration  方法上@Bean

 

14.属性注入的方式,DI的方式

1)spring配置文件的配置,构造器  set方法   自动注入

2)注解方式 @Resource  @Autowired

 

15.springboot如何使用jsp

1)pom文件中添加jasper依赖

2)springboot默认配置文件中配置

  spring.mvc.view.prefix|suffix   SpringMVC的视图解析器

3)jsp文件放在webapp目录中

 

16.springboot整合redis

1)pom文件中添加jedis依赖

2)自定义配置文件配置redis服务器的ip和端口

3)创建一个配置类

   读取自定义配置文件中的参数  类上@PropertySource  属性上@Value

   把JedisPool装配到IOC容器中  类上@SpringBootConfiguration 方法上@Bean

4)redis集群把JedisCluster装配到IOC容器中

 

17.springboot整合mybatis

1)pom文件添加mysql驱动, jdbc, mybatis

2)springboot的默认配置文件

  dataSource  数据源

  mybatis的相关配置  mapping映射文件

                      驼峰映射

                      起别名

3)在启动类上添加@MapperScan

4) 在mapper的接口上添加@Repository

 

18.mybatis打印sql语句

  logging.level.mapper接口的报名=debug

 

19.springboot如何使用事务

1)pom文件添加jdbc模块

2)@Transactional  可以用在方法上,也可以用在类上,用在类上表明该类下所有的方法都受事务控制

 

20.统一异常处理

1)类上@ControllerAdvice捕获所有的异常

2)方法@ExceptionHandler(value=某个异常类的calass对象) 处理指定的异常

 

21.springboot如何使用servlet

1)先创建一个类,继承HttpServlet,重写doGet()|doPost()

2)类上用@WebServlet

3)Springboot的启动类上@ServletComponentScan

 

22.springboot如何用Filter

1)创建一个类,实现Filter接口,  实现doFilter(),  init()  destroy()

2)类上用@WebFilter

3) Springboot的启动类上@ServletComponentScan

 

23.springboot如何使用拦截器interceptor

1)创建类,实现HandlerInterceptor接口或者是继承HandlerInterceptorAdapter类

2)重写三个方法

   preHandl()  目标方法执行前

   postHandl()  目标方法后,视图解析器前

   afterCompletion()   目标方法后,视图解析器后

3)创建的该类上用@Component注解交给IOC容器管理

4)创建一个配置类,类上用@SpringbootConfiguration, 

继承WebMvcConfigurerAdapter

5) 重写AddInterceptor方法