springboot的WEB开发笔记(整合数据库)

整合数据库

整合JDBC

如果时区报错,则添加serverTimezone=UTC

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

springboot 会已经帮我们封装好JdbcTemplate对象,我们可以直接注入属性进行使用。

整合Druid

spring.datasource.type 默认数据源是Hikari

@Import({Hikari.class, Tomcat.class, Dbcp2.class, Generic.class, DataSourceJmxConfiguration.class})

type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源为Druid

配置一下属性

 	#Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: 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
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

利用@Bean注册druid的web.xml内容

/**后台监控:web.xml,ServletRegistrationBean
 * 因为springboot 内置了servlet容器,所以没有web.xml
 * 替换方法:将ServletRegistrationBean注册进去
 */
@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");
    //允许谁能访问,""代表都可以,"localhost"代表本机
    initParameters.put("allow","");

    bean.setInitParameters(initParameters);
    return bean;
}

过滤器的配置

@Bean
public FilterRegistrationBean webStatFilter(){
    FilterRegistrationBean bean= new FilterRegistrationBean();
    bean.setFilter(new WebStatFilter());
    //过滤的请求
    Map<String, String> InitParameters = new HashMap<>();
    InitParameters.put("exclusions","*.js,*.css./druid/*");
    bean.setInitParameters(InitParameters);
    return bean;
}

整合mybatis

pom.xml

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

yaml配置

注意实体类的配置和mapper的配置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characherEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  type-aliases-package: com.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

Mapper接口有两种扫描方法

①@Mapper直接配置

//表示mybatis的mapper接口
@Mapper
@Repository
public interface UserMapper {
}

②用@MapperScan进行扫描

@SpringBootApplication
@MapperScan("com.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值