zoukankan      html  css  js  c++  java
  • springboot的使用01

    yml配置对象属性

    @ConfigurationProperties作用

    将配置文件中配置的每一个属性的值,映射到这个组件中;

    告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定

    参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应

    只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能

    @ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
    只有当我们引入了相关的jar包,springboot才会将相应的对象加载到spring容器中去

    @EnableConfigurationProperties的作用
    让使用@ConfigurationProperties注解的类生效
    如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入


    @ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
    @ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
    @ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
    @ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
    @ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
    @ConditionalOnNotWebApplication(不是web应用)

    WebMvcConfigurer

    可以实现这个类,重写addViewController方法来定义静态资源的映射,因为springboot不会加载到templates文件夹下的资源,所以需要配置路径映射来适配.

    public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("login");
            registry.addViewController("/login.html").setViewName("login");
            registry.addViewController("/main.html").setViewName("dashboard");
        }


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

    跳转@{},参数后面加括号

    国际化的表达式  #{}

    springboot国际化

    1.在resource目录下新建一个i18n的文件,新建login.properties,login_en_US.properties,login_zh_CN.properties 三个文件.

    2.在配置文件中配置文件的名字 spring.message.basename = i18n/login

    <a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>

    <a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>

    public class MyLocaleResolver implements LocaleResolver{
    
        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String language = request.getParameter("l");
            System.out.println(language);
            Locale locale = Locale.getDefault();
            if(!StringUtils.isEmpty(language)){
                String[] split = language.split("_");
                locale = new Locale(split[0],split[1]);
            }
            return locale;
        }
    
        @Override
        public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    
        }
    }

    配置一个国际化解析器,然后注册到spring容器中就可以了.

    springboot拦截器的使用

    首先自定义一个拦截器,实现HandlerInterceptor接口,重写preHandle方法

    public class MyLoginInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
            Object obj = request.getSession().getAttribute("loginUser");
            if(obj == null){
                request.setAttribute("msg","你没有权限,请先登录");
                request.getRequestDispatcher("/login.html").forward(request,response);
                return false;
            }else{
                return true;
            }
    
        }
    }
     
       @Override
        public void addInterceptors(InterceptorRegistry interceptorRegistry) {
            interceptorRegistry.addInterceptor(new MyLoginInterceptor()).addPathPatterns("/**")
                    .excludePathPatterns("/","/user/login","/login.html","/asserts/css/*","/asserts/img/*","/asserts/js/*");
        }
    
    

    然后将拦截器注册到spring容器中,并过滤掉一些路径.

    Springboot JDBC的配置

    1.配置数据源

    spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=fuck
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    2.使用jdbcTemplate

      @Autowired
        JdbcTemplate jdbcTemplate;
    
        @ResponseBody
        @RequestMapping("/jdbc")
        public List<Map<String,Object>> jdbcTest() throws SQLException {
            String sql = "select * from person";
            return jdbcTemplate.queryForList(sql);
        }

     Druid数据源的监控页面的配置

    @Configuration
    public class DruidConfig {
    
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DataSource druidDataSource(){
            return new DruidDataSource();
        }
    
        //后台监控
        @Bean
        public ServletRegistrationBean statViewServlet(){
            ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
    
            //后台需要有人登陆,账号密码配置
            HashMap<String,String> initParameters = new HashMap<>();
    
            //增加配置
            initParameters.put("loginUsername","admin");
            initParameters.put("loginPassword","123456");
    
            //允许谁可以访问
            initParameters.put("allow","");
    
            bean.setInitParameters(initParameters); //设置初始化参数
            return bean;
        }
    }

    Springboot与mybatis的整合

     导入mybatis的依赖

    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
    @SpringBootApplication
    @MapperScan("com.lzh.springboot02data.mapper")
    public class Springboot02DataApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02DataApplication.class, args);
        }
    
    }

      在启动类上加上@MapperScan注解,该注解会在编译阶段生成代理实现类对象加入到容器中

  • 相关阅读:
    界面和效果
    第一篇博客
    Java作业(六)
    Java学习(五)
    Java学习(四)
    JAVA学习(三)
    Java学习(二)
    Java学习心得
    音乐播放器项目计划进度安排
    课程设计 高云鹏 郑帅康 张程 姬泽辉
  • 原文地址:https://www.cnblogs.com/lzh66/p/14077154.html
Copyright © 2011-2022 走看看