SpringBoot学习笔记02

SpringBoot学习笔记02,第1张

SpringBoot学习笔记02

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

配置数据源

springboot默认的数据源是hikari

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

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.2.8version>
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>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.12version>
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

导入依赖


<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>2.2.2version>
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


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>

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

方法1:在UserMapper上注解@Mapper

方法2:在启动类上注解

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/876448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存