美文网首页
五.SpringBoot与数据访问

五.SpringBoot与数据访问

作者: 21号新秀_邓肯 | 来源:发表于2020-10-22 13:24 被阅读0次

    1.整合jdbc

    <dependency>
        <groupId>org.springframework.boot</groupId>            
            <artifactId>spring‐boot‐starter‐jdbc</artifactId>            
        </dependency>        
        <dependency>        
        <groupId>mysql</groupId>            
            <artifactId>mysql‐connector‐java</artifactId>            
        <scope>runtime</scope>            
    </dependency> 
    

    jdbc.yml

    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://192.168.15.22:3306/jdbc
        driver‐class‐name: com.mysql.jdbc.Driver
    

    2.整合Druid数据源

    导入jar包

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.21</version>
            </dependency>
    

    1 代码

    @Configuration
    public class DruidConfig {
    
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DataSource druid(){
           return  new DruidDataSource();
        }
    
        //配置Druid的监控
        //1、配置一个管理后台的Servlet
        @Bean
        public ServletRegistrationBean statViewServlet(){
            ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
            Map<String,String> initParams = new HashMap<>();
    
            initParams.put("loginUsername","admin");
            initParams.put("loginPassword","123456");
            initParams.put("allow","");//默认就是允许所有访问
            initParams.put("deny","192.168.15.21");
    
            bean.setInitParameters(initParams);
            return bean;
        }
    
    
        //2、配置一个web监控的filter
        @Bean
        public FilterRegistrationBean webStatFilter(){
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new WebStatFilter());
    
            Map<String,String> initParams = new HashMap<>();
            initParams.put("exclusions","*.js,*.css,/druid/*");
    
            bean.setInitParameters(initParams);
    
            bean.setUrlPatterns(Arrays.asList("/*"));
    
            return  bean;
        }
    }
    
    

    2. yml文件

    spring:
        #Spring Boot 默认是不注入这些属性值的,需要自己绑定
        #druid 数据源专有配置
        druid:
          initial-size: 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-return: false
          test-on-borrow: false
          test-while-idle: true
          pool-prepared-statements: true
     
          #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
          #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
          #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
          filters: stat,wall,log4j
          max-pool-prepared-statement-per-connection-size: 20
          use-global-data-source-stat: true
          connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
     
    

    3. 整合MyBatis

    3.1 导入jar包
    <dependency>        
        <groupId>org.mybatis.spring.boot</groupId>            
        <artifactId>mybatis‐spring‐boot‐starter</artifactId>            
        <version>1.3.1</version>            
    </dependency>
    
    3.2 配置规则

    1. 代码版

    @org.springframework.context.annotation.Configuration
    public class MyBatisConfig {
        @Bean
        public ConfigurationCustomizer configurationCustomizer(){
            return new ConfigurationCustomizer(){
                @Override
                public void customize(Configuration configuration) {
                    configuration.setMapUnderscoreToCamelCase(true);
                }
            };
        }
    }
    

    2. yml

    mybatis:
      config‐location: classpath:mybatis/mybatis‐config.xml 指定全局配置文件的位置
      mapper‐locations: classpath:mybatis/mapper/*.xml  指定sql映射文件的位置
    

    4. 使用MapperScan批量扫描所有的Mapper接口

    @MapperScan(value = "com.atguigu.springboot.mapper")
    @SpringBootApplication
    public class SpringBoot06DataMybatisApplication {
        public static void main(String[] args) {    
            SpringApplication.run(SpringBoot06DataMybatisApplication.class, args);        
        }    
    }
    
    

    相关文章

      网友评论

          本文标题:五.SpringBoot与数据访问

          本文链接:https://www.haomeiwen.com/subject/nhqykctx.html