栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【SpringBoot】SpringBoot——整合持久层技术

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【SpringBoot】SpringBoot——整合持久层技术

文章目录

5. 整合持久层技术

5.1 整合JdbcTemplate5.2 整合MyBatis5.3 Spring Data JPA

5.3.1 JPA、Spring Data、Spring Data JPA的故事5.3.2 整合Spring Data JPA5.3.3 CORS跨域配置


5. 整合持久层技术

持久层的实现是和数据库紧密相连的,在JAva领域内,访问数据库的通常做法是使用JDBC,JDBC使用灵活而且访问速度较快,但JDBC不仅需要操作对象,还需要操作关系,并不是完全面向对象编程。目前主流的持久层框架包括:MyBatis、JPA等。这些框架都对JDBC进行了封装,使得业务逻辑开发人员不再面对关系型操作,简化了持久型开发。

5.1 整合JdbcTemplate

Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的。JDBC模板类是第一种工作模式。

JdbcTemplate类通过模板设计模式帮助我们消除了冗长的代码,只做需要做的事情(即可变部分),并且帮我们做了固定部分,如连接的创建及关闭等。

SpringBoot自动配置了JdbcTemplate数据模板。

    配置文件

    在application.yml中添加如下配置:

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
        username: root
        password: admin123
        driver-class-name: com.mysql.jdbc.Driver
    

    JDBC默认采用org.apache.tomcat.jdbc.pool.DataSource数据源,数据源的相关配置参考都封装在org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration类中。默认使用Tomcat连接池,可以使用spring.datasource.type指定自定义数据源类型。

    SpringBoot默认支持的数据源:

    org.apache.tomcat.jdbc.pool.DataSourceHikariDataSourceBasicDataSource

    整合Druid数据源

    导入Druid依赖:

    
                com.alibaba
                druid
                1.2.3
            
    

    修改application.yml配置文件:

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
        username: root
        password: admin123
        driver-class-name: com.mysql.jdbc.Driver
    
    
        type: com.alibaba.druid.pool.DruidDataSource
        tomcat:
          initial-size: 5
          min-idle: 5
          max-active: 20
          max-wait: 60000
          time-between-eviction-runs-millis: 60000
          min-evictable-idle-time-millis: 300000
          validation-query: SELECt 1 FROM DUAL
          test-on-borrow: false
          test-while-idle: true
          test-on-return: false
        dbcp2:
          pool-prepared-statements: true
        filter:
          stat:
            enabled:true
            log-show-sql:true
          wall:
            enabled:true
    

    配置type:com.alibaba.druid.pool.DruidDataSource来切换数据源,但是文件中数据源其他配置信息还不会起任何作用。

    因为Druid是第三方数据源,在SpringBoot中并没有自动配置类,需要自己配置数据。所以我们需要自定义一个Druid数据源进行绑定:

    package com.shenziyi.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class Config {
        
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DruidDataSource druidDataSource(){
            return new DruidDataSource();
        }
    }
    

    配置Druid的监控和过滤器:

    package com.shenziyi.config;
    
    
    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    
    @Configuration
    public class DruidConfig {
        
        @Bean
        public ServletRegistrationBean statViewServlet(){
            ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid
        User findByUsernameAndPassword(String username, String password);
    

    重启项目,在Postman中进行测试,如下:

    我们也可以在日志中看到Hibernate输出的SQL日志:

    Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.password as password3_0_, user0_.username as username4_0_ from t_user user0_ where user0_.username=? and user0_.password=?
    

    这是因为在Spring Data JPA中,只要自定义的方法符合既定规范,Spring Data就会分析开发者的意图,从而根据方法名生成相应的SQL查询语句。

    JPA在这里遵循Convention over configuration(约定大约配置)的原则,遵循Spring及JPQL定义的方法命名。Spring提供了一套可以通过命名规则进行查询构建的机制。这套机制首先通过方法名过滤一些关键字,例如find…By、read…By、query…By、count…By和get…By。系统会根据关键字将命名解析成两个子语句,第一个By是区分这两个子语句的关键词。这个By之前的子语句是查询子语句(指明返回要查询的对象),后面部分是条件子语句。如果不区分就是find…By返回的就是定义Respository时指定的领域对象集合,同时JPQL中也定义了丰富的关键字:And、Or、Between等,Spring Data JPA支持的方法命名规则:

    spring data JPA命名规范
    关键字方法命名sql where字句
    AndfindByNameAndPwdwhere name= ? and pwd =?
    OrfindByNameOrSexwhere name= ? or sex=?
    Is,EqualsfindById,findByIdEqualswhere id= ?
    BetweenfindByIdBetweenwhere id between ? and ?
    LessThanfindByIdLessThanwhere id < ?
    LessThanEqualsfindByIdLessThanEqualswhere id <= ?
    GreaterThanfindByIdGreaterThanwhere id > ?
    GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
    AfterfindByIdAfterwhere id > ?
    BeforefindByIdBeforewhere id < ?
    IsNullfindByNameIsNullwhere name is null
    isNotNull,NotNullfindByNameNotNullwhere name is not null
    LikefindByNameLikewhere name like ?
    NotLikefindByNameNotLikewhere name not like ?
    StartingWithfindByNameStartingWithwhere name like '?%'
    EndingWithfindByNameEndingWithwhere name like '%?'
    ContainingfindByNameContainingwhere name like '%?%'
    OrderByfindByIdOrderByXDescwhere id=? order by x desc
    NotfindByNameNotwhere name <> ?
    InfindByIdIn(Collection c)where id in (?)
    NotInfindByIdNotIn(Collection c)where id not in (?)
    TRUEfindByAaaTuewhere aaa = true
    FALSEfindByAaaFalsewhere aaa = false
    IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)

    自定义查询Using @Query

    既定的方法命名规则不一定满足所有的开发需求,因此Spring Data JPA也支持自定义JPQL或者原生SQL。只需在声明的方法上标注@Query注解,同时提供一个JPQL查询语句即可。如需对username进行模糊查询,则需在UserRepository中添加如下代码:

    @Query("select u from User u where u.username like %?1%")
        List getUserByUsername(@Param("username") String username);
    

    接着在UserController中暴露接口:

    @GetMapping("/ops")
        public List getOPs( String username){
            return  userRepository.getUserByUsername(username);
        }
    
5.3.3 CORS跨域配置

在实际开发中我们经常会遇到跨域问题,跨域其实是浏览器对于Ajax请求的一种安全限制。目前比较常用的跨域解决方案有三种:

Jsonp:最早的解决方案,利用script标签可以跨域的原理实现。nginx反向代理:利用nginx反向代理把跨域转为不跨域,支持各种请求方式。CORS:规范化的跨域请求解决方案,安全可靠。

这里采用CORS跨域方案。

CORS全称"跨域资源共享"(Cross-Origin Resource Sharing)。CORS需要浏览器和服务器同时支持,才可以实现跨域请求,目前绝大多数浏览器支持CORS,IE版本则不能低于IE10.CORS的整个过程都由浏览器自动完成,前端无需做任何设置,跟平时发送Ajax请求并无差异。实现CORS的关键在于服务器(后台),只要服务器实现CORS接口,就可以实现跨域通信。

Spring MVC已经帮我们写好了CORS的跨域过滤器:CorsFilter,内部已经实现了判定逻辑,我们直接使用即可。

    配置CORS

    在之前的基础上进行前后端分离开发,在该项目下新建GlobalCorsConfig类用于跨域的全局配置:

    @Configuration
    public class GlobalCorsConfig  {
        @Bean
        public CorsFilter corsFilter() {
            //1.添加CORS配置信息
            CorsConfiguration config = new CorsConfiguration();
            //1) 允许的域,不要写*,否则cookie就无法使用了
            config.addAllowedOrigin("http://localhost:8081");
            //2) 是否发送cookie信息
            config.setAllowCredentials(true);
            //3) 允许的请求方式
            config.addAllowedMethod("OPTIONS");
            config.addAllowedMethod("HEAD");
            config.addAllowedMethod("GET");
            config.addAllowedMethod("PUT");
            config.addAllowedMethod("POST");
            config.addAllowedMethod("DELETE");
            config.addAllowedMethod("PATCH");
            // 4)允许的头信息
            config.addAllowedHeader("*");
    
            //2.添加映射路径,我们拦截一切请求
            UrlbasedCorsConfigurationSource configSource = new UrlbasedCorsConfigurationSource();
            configSource.registerCorsConfiguration("
                    url:'http://localhost:8080/users',
                    data:JSON.stringify({
                        "username":"admin",
                        "password":"123456",
                        "email":"635498720"
                    }),
                    dataType: "json",
                    contentType:"application/json;charset=UTF-8",
                    success:function (msg) {
                    }
                })
            }
            function deleteUser() {
                $.ajax({
                    type:'delete',
                    
                    url:'http://localhost:8080/users/5',
                    success:function (msg) {
                    }
                })
            }
        
    
    
    

    在该文件中两个Ajax分别发送了跨域请求,请求5.3.2中添加和删除接口,然后在该项目中把端口设置为8081:

    server.port=8081
    

    测试

    启动项目,在浏览器输入http://localhost:8081/index.html,查看页面如下:

    单击"添加数据"按钮,查看数据库变化:

    单击"删除数据"按钮:(报错,因为数据库没有id为5的元组)

    修改一下代码,删除id为2的元组,结果如下:

    总的来说,CORS的配置完全在后端设置,配置起来比较容易,目前对于大部分浏览器的兼容性也比较好。CORS的优势也比较明显,可以实现任何类型的请求,相较于Jsonp跨域只能使用GET请求来说,更加便于使用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/719502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号