23、springboot——整合Mybatis之创建项目,搭建环境①

 


  1、创建工程需要的maven坐标

 这个mybatis的starter是mybatis官方出的适应springboot

通过图来了解这个依赖导入了哪些包

 

2)、数据连接池的使用

  引入Druid数据连接池

      <!--引入druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

3)、数据连接池的配置

  application.yml配置文件的设置:

  依然是Druid的配置

复制代码
spring:
  datasource:
    #   数据源基本配置
    username: root
    password: 123
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springbootmybatis
    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,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
复制代码

4、将数据源加入到容器,配置才起效果+配置数据源监控

复制代码
@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
       return new DruidDataSource();
    }

    //配置Durid数据源监控
    //1、配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,String> initParameters = new HashMap<>();

        initParameters.put("loginUsername","root");
        initParameters.put("loginPassword","123");
        //允许访问,默认所有都可访问
        initParameters.put("allow","");//默认就是允许所有访问
        //不让访问
        initParameters.put("deny","192.168.15.21");
        //设置初始化参数
        bean.setInitParameters(initParameters);
        return bean;
    }

    //2、配置一个监控的Filter
    @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);
        //拦截的请求
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
复制代码

5.配置完上面这些先运行应用访问druid的login.html页面看看有没有配置成功

 6、创建数据库表

  application.yml配置(红色部分为springboot2.x之后需要加上的

复制代码
spring:
  datasource:
    #   数据源基本配置
    username: root
    password: 123
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springbootmybatis?serverTimezone=UTC
    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,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    schema:
      - classpath:sql/department.sql
      - classpath:sql/employee.sql
    initialization-mode: always
复制代码

  将建表的sql文件放入指定的路径下

   此时启动项目之后数据库中表已经建好

表建好之后就把application.yml中的schema:配置注解掉;不然每次启动项目都会删掉原来的表然后新建表

7、创建对应的javabean类

public class Employee {
    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;
    private Integer dId;
.....
}
public class Department {
    private Integer id;
    private String departmentName;
...
}

 

posted @   Arbitrary233  阅读(234)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示