SpringBoot相关

前奏(SpringMVC代码配置):
Spring Boot 的前端文件确定是放在 src/main/resources 下而非MAVEN标准的 src/main/webapp/WEB-INF下???
1、实现 org.springframework.web.WebApplicationInitializer 接口实现等效 web.xml 配置
2、继承 org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter并重写对应方法实现SpringMVC定制配置;注解@EnableWebMvc启用
3、继承 org.springframework.web.servlet.handler.HandlerInterceptorAdapter 或实现 org.springframework.web.servlet.HandlerInterceptor实现自定义拦截器配置
4、@ControllerAdvice注解定义对@RequestMapping全局生效的配置
5、路径参数匹配:若路径参数带<em>.</em>的话,其后参数均被忽略
6、继承 org.springframework.http.converter.AbstractHttpMessageConverter 实现自定义消息转换器

Boot相关:

1、Specification  使用

    // 通过 Specification 实现字符串值模糊匹配其余类型精确匹配
    public static <T> Specification byAuto(EntityManager entityManager, T t) {
        // 当前实体类对象类的类型
        Class<T> tClass = (Class<T>) t.getClass();
        return (Specification) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            EntityType<T> entity = entityManager.getMetamodel().entity(tClass);
            // 实体类所有属性遍历
            for (Attribute<T, ?> attr : entity.getDeclaredAttributes()) {
                // 属性对应的值
                Object attrValue = getValue(t, attr);
                if (null != attrValue) {
                    if (String.class == attr.getJavaType()) {
                        if (!StringUtils.isEmpty(attrValue)) {
                            predicates.add(cb.like(root.get(attribute(entity, attr.getName(), String.class)), pattern((String) attrValue)));
                        }
                    } else {
                        predicates.add(cb.equal(root.get(attribute(entity, attr.getName(), attrValue.getClass())), attrValue));
                    }
                }
            }
            // 条件列表转换 Iterables 为 com.google.guava 包工具类
            return predicates.isEmpty() ? cb.conjunction() : cb.and(Iterables.toArray(predicates, Predicate.class));
        };
    }


    private static <T, E> SingularAttribute<T, E> attribute(EntityType<T> entity, String fieldName, Class<E> fieldClass) {
        return entity.getDeclaredSingularAttribute(fieldName, fieldClass);
    }

    private static <T> Object getValue(T t, Attribute<T, ?> attr) {
        return ReflectionUtils.getField((Field) attr.getJavaMember(), t);
    }

    private static String pattern(String str) {
        return "%" + str + "%";
    }

2、扩展 Repository

    2.1、继承自 JpaResitory 的标准 Repository 继承自定义的 Repository(DAO)

    2.2、新建类似 JpaRepository 的自定义 Repository 替换原生 Repository  --  方法实践待定

3、一些注解说明

    核心启动类 :

        1、@SpringBootApplication 核心注解;开启自动配置(exclude:关闭特定的自动配置)

        2、@ImportResource 导入自定义 xml 配置

        3、注解 @ConfigurationProperties @PropertySource @EnableConfigurationProperties(开启属性注入) 可实现 properties 与 JavaBean 对应

        4、使用 @PropertySource 映射位于非默认 application.properties 中的属性时,获取其他默认 application.properties 中的自定义属性必须显示注明配置文件

        5、注解 @EnableJpaRepositories 启用 Spring Data JPA 支持(repositoryBaseClass 启用自定义 Repository 配置)

   Respository相关:

        1、JpaSpecificationExecutor 基于准则查询的查询方式

        2、通过继承自定义 Repository 实现 Spring Data JPA 和自定义 DAO 整合

        3、添加 spring-boot-starter-data-rest 即可启用 REST 访问(不能存在同名方法;通过实体名称+s访问)

        4、使用 @RestResource 暴露自定义REST路径(根路径/REST根路径/实体名称+s/search/暴露路径访问)

        5、使用 @RepositoryRestResource 配置REST节点路径及是否暴露

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值