springboot学习-(三)

一、thymeleaf:

这个是模板引擎,里面配有页面的前缀和后缀

结论:如果需要使用thymeleaf,只需要导入对应的依赖就行,我们将html页面放在项目的templates目录下即可。

在页面使用thymeleaf取数据

所有的html元素都可以被thymeleaf替换接管: th:元素名

thymeleaf语法
thymeLeaf语法 说明
th:utext="${ }" 不转义特殊字符
th:text="${ }" 转义特殊字符
<h3 th:each=“user: u s e r s ” t h : t e x t = " {users}” th:text=" usersth:text="{user}"> 遍历集合元素

[[${user}]]

@{/} 这个代表项目资源的根目录,静态static目录不需要写
导入图片,传参用()
<form … th:action="@{ }" >
<p … th : if = “${not #strings.isEmpty(msg)}”> thymeleaf是个超级模板,可以和很多java语句融合,判断从controller传过来的数据是否为空
<nav … th: fragment=“xxx” …> 抽取这一部分,将其变成一个可使用的组件
将抽取的插件插入进去
将抽取的插件插入进去
<a th: class="${key==‘value’}" ? 选择一 : 选择二 > 判断key是否等于值value
遍历后台数据
格式化时间



配置在application.properties中, 清除thymeleaf模板引擎的缓存 : spring.thymeleaf.cache = false

二、首页配置
自定义解析器
//全面接管springmvc,扩展mvc配置,添加新配置
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
   

    //将创建一个一个组件,交给springboot,springboot会自动转配
    @Bean
    public ViewResolver myViewResolver(){
   
        return new MyViewResolver();
    }

    public static class MyViewResolver implements ViewResolver{
   

        //自定义一个视图解析器
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
   
            return null;
        }
    }
}
自定义一个自己的starter
  • 写一个后缀是Configuration类,再写一个后缀是Properties类,把两个类打成jar包的形式,然后添加到项目当中,可以用maven的package打包。
  • Configuration类中一定有带有ConditionalOnClassl注解来判断是否导入了公司编写的类,没有,该类就不能生效,就不能使用该类里面的功能。

总结,在springboot中,有非常多的XXXConfiguration配置类,用来扩展

@Repository       //把该类交给spring进行管理,放在类上
@AllArgsConstructor     //创建该类的所有构造参数,放在类上
@NoArgsConstructor      //创建该类的无参构造,放在类上
@Autowired      //在该类里可以使用类类型字段的引用,放在类类型的字段上,也就是从spring容器中拿到实例对象
@RequestMapper()  //访问接口名字,放在方法上
@Controller   //该类是个Controller容器,放在类上

//扩展spring
    
@Configuration    //配置访问接口,放在类上,实现WebMvc
@Bean    //将自己写的组件配置(类)到spring容器里
@ResponseBody   
@RequestParam()  
Model
new Model().addAttribute(key,value);
return  "redirect:/";
HttpSession
new HttpSession().setAttribute();
request.getSession().getAttribute(key);
request.getRequestDispatcher("页面地址").forward(request,response);   //转发
@PathVariable()    //路径变量
配置类步骤:可以不用controller层
  • 添加thymeleaf依赖
  • 将以Config为后缀创建配置类,统一放在项目config包下管理
//扩展spring,添加配置类
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
   

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
   
        registry.addViewController("luona").setViewName("login");
    }
}
idea快捷键

Shift + Alt+ Ctrl + 鼠标左键 :选中多行内容区域

Shift + Alt + 鼠表左键 :选中多行编辑多行

Shift + Alt +Ctrl + N 查找当前项目中的类

首页配置步骤总结:
  • 注意点,所有页面的静态资源都需要使用thymeleaf来接管
  • url: @{ }
三、页面国际化:

[]: https://www.bilibili.com/video/BV1PE411i7CV?p=22

  • Bundle:可视化配置视图
注意国际化的数据要使用thymeleaf的 #{ }来获取
页面国际化:
-   在resources下建包i18n,然后将创建中英的properties,统一放在idea自己生成的Bundle下,最后是在把统一配置卸载项目的application.properties配置中。
  • 编写自己的国际化解析器,实现LocaleResolver接口

  • 将自己写的组件配置到spring容器里,在配置类中重写localeResolver方法,返回自己编写的国际化解析器类的对象,方法名上使用@Bean注解,配置给spring

  • 页面使用 #{ }来获取

注意:设置了首页配置和页面国际化后,页面的所有地址都是用 @{ }来表示
頁國際化文本显示用 #{ } ,数据获取用${ }
四、登录拦截器

[]: https://www.bilibili.com/video/BV1PE411i7CV?p=24

  • 创建拦截组件(类),实现 HandlerInterceptor 接口
  • 重写 preHandle 方法
  • 将拦截器组件配置到Spring 容器中
  • 在配置类(配置类实现了)中重写addInterceptors方法,并在方法上添加@Bean

vue是纯前端的模板引擎,thymeleaf是对后台比较友好的模板引擎

五、使用Thymeleaf语法遍历
1.提取公共页面
th:frament="xxx";
th: replace="~{页面名::xxx}"
<!--用()传参,直接接受即可-->
2.遍历后台数据
<tr th:each="emp:${emps}">
     <td  th:text="${emp.getId()}"></td>
     ......
</tr>
六.整合JDBC
#关闭默认图标
spring.mvc.favicon.enabled=false
#关闭模板引擎的缓存
spring.thymeleaf.cache=false
server.servlet.context-path=/luona
#我们的配置文件的真实位置
spring.messages.basename=i18n.login
#时间日期格式化
spring.mvc.date-format=yyyy-MM-dd
七、整合JDBC

问题:springboot项目的jdbc配置中时区报错,解决方法:在url 的值中 "?"后面添加serverTimezone=UTC "&"连接后面的字符编码格式

八、DRUID

是数据库连接池实现,结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控。

HikariDataSource:当前Java WEB数度最快的数据源 ,相比于传统的C3P0、DBCP、Tomcat、jdbc等连接池更加优秀。

一、将自定义的datasource配置信息绑定到配置类上步骤:
  • 1、使用yml编写druid配置信息。
  • 2、使用@ConfigurationProperties(prefix=“xxx”)注解找到符合前缀是"xxx"的配置信息.yml的文件。
  • 3、使用@Bean注解将适配到的了放到spring中进行管理
@Bean   将类放到sprin中进行管理
@ConfigurationProperties(prefix="xxx")

xxxConfig 配置类:

@Configuration
public class DruidConfig {
   

    @ConfigurationProperties(prefix="spring.datasource")
    @Bean
    public DataSource druidDataSource(){
   
        return new DruidDataSource();
    }
}

application.yml 配置类的配置信息

spring:
  datasource:
    password: root
    username: root
    url: jdbc:mysql://localhost:3306/cvs_db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource



    #初始化时池中建立的物理连接个数。
    initialSize: 5

    #最大的可活跃的连接池数量
    maxActive:
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,对于使用Druid连接池的Spring Boot 2.5.5应用程序,您需要在启动类中进行以下配置: 1.引入Druid依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2.配置数据源 在application.properties或application.yml中添加以下配置: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3.配置Druid连接池 在启动类中添加以下代码: ```java @Configuration public class DruidConfig { @Bean public ServletRegistrationBean<StatViewServlet> druidServlet() { ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); registrationBean.addInitParameter("allow", "127.0.0.1"); registrationBean.addInitParameter("deny", "192.168.1.100"); registrationBean.addInitParameter("loginUsername", "admin"); registrationBean.addInitParameter("loginPassword", "123456"); return registrationBean; } @Bean public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() { FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>(new WebStatFilter()); registrationBean.addUrlPatterns("/*"); registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return registrationBean; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { return new DruidDataSource(); } } ``` 以上代码中,我们配置了Druid的Servlet和Filter,并且创建了一个Druid连接池的DataSource。 注意:以上代码中的url、username和password需要根据您的实际情况进行修改。 希望这能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UPYOLo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值