SpringBoot学习笔记02

SpringBoot学习笔记02

配置数据源

springboot默认的数据源是hikari

配置德鲁伊数据源

  1. 导入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.8</version>
</dependency>

在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依赖

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
</dependency>

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

/**
 * @author bo
 */
@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<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        //后台需要有人登录账号密码
        HashMap<String, String> 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<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        HashMap<String, String> initParameters = new HashMap<>();
        //不进行统计
        bean.setInitParameters(initParameters);
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        return bean;
    }
}

在这里插入图片描述

整合mybatis

导入依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

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<User> queryList();

    User queryUserByID(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bo.mapper.UserMapper">
    <select id="queryList" resultType="user">
        select * from boot_mybatis.user;
    </select>

    <select id="queryUserByID" resultType="user">
        select * from boot_mybatis.user where id = #{id};
    </select>

    <insert id="addUser" parameterType="user">
        insert into boot_mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd});
    </insert>

    <update id="updateUser" parameterType="user">
        update boot_mybatis.user set name = #{name},pwd=#{pwd} where id = #{id};
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from boot_mybatis.user where id = #{id};
    </delete>
</mapper>

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

方法1:在UserMapper上注解@Mapper
在这里插入图片描述
方法2:在启动类上注解
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值