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;
...
}
原文地址:https://www.cnblogs.com/lyh233/p/12547846.html