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

SpringBoot学习笔记02

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

SpringBoot学习笔记02

SpringBoot学习笔记02

目录
  • SpringBoot学习笔记02
    • 配置数据源
      • 配置德鲁伊数据源
    • 整合mybatis

配置数据源

springboot默认的数据源是hikari

配置德鲁伊数据源
  1. 导入依赖


    com.alibaba
    druid
    1.2.8

在application.yaml中配置

spring:
  datasource:
    username: root
    password:  123456
    url: jdbc:mysql://localhost:3306/boot_mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false&autoReconnect=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    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.Properity
    #则导入log4j 依赖就行
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

下载log4j依赖


    log4j
    log4j
    1.2.12

springboot内置了Servlet容器,所以没有web.xml替代方法ServletRegistrationBean
DruidConfig

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    //后台监控==>web.xml
    //springboot内置了Servlet容器,所以没有web.xml替代方法ServletRegistrationBean
    @Bean
    public ServletRegistrationBean StatViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        //后台需要有人登录账号密码
        HashMap initParameters = new HashMap<>();
        //增加配置
        initParameters.put("loginUsername","admin");//固定的
        initParameters.put("loginPassword","123456");
        //允许谁访问
        initParameters.put("allow","");
        //禁止谁访问
        initParameters.put("zzz", "192.168.11.123");

        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }

    //filter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        HashMap initParameters = new HashMap<>();
        //不进行统计
        bean.setInitParameters(initParameters);
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        return bean;
    }
}

整合mybatis

导入依赖



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.2

application.properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/boot_mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false&autoReconnect=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.bo.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml


UserMapper

//表示了这是一个mybatis的mapper类
@Mapper
@Repository
public interface UserMapper {
    List queryList();

    User queryUserByID(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

UserMapper.xml




    
        select * from boot_mybatis.user where id = #{id};
    

    
        insert into boot_mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd});
    

    
        update boot_mybatis.user set name = #{name},pwd=#{pwd} where id = #{id};
    

    
        delete from boot_mybatis.user where id = #{id};
    

很关键,为了使用方便在peoperties中配置了别名
默认别名就为这个类的类名,首字母小写(写大写也可以运行
mapper

方法1:在UserMapper上注解@Mapper

方法2:在启动类上注解

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

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

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