springboot配置mysql连接

一:导入pmo.xm配置包

  1. mysql库连接、druid连接池、mybatis组件
	<!-- 使用MySQL数据库-->
 	<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!--druid连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!-- 使用mybatis-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
  1. 配置扫描文件
<build>
     <!--扫描xml文件-->
     <resources>
         <resource>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/ *.xml</include>
             </includes>
             <filtering>true</filtering>
         </resource>
         <resource>
             <directory>src/main/resources</directory>
             <includes>
                 <include>*</include>
             </includes>
             <filtering>true</filtering>
         </resource>
     </resources>
  </build> 

二:application.yml文件配置

#项目工程信息
spring:
#Mysql数据库信息
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://连接IP地址:端口/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: 用户名
    password: 密码
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #下面为连接池的补充设置,应用到上面所有数据源中
      initial-size: 5 #初始化大小,最小,最大
      min-idle: 5 #最小,最大
      max-active: 20  #最大
      max-wait: 60000 #配置获取连接等待超时的时间
      time-between-eviction-runs-millis: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      min-evictable-idle-time-millis: 300000  #配置一个连接在池中最小生存的时间,单位是毫秒
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true  #打开PSCache,并且指定每个连接上PSCache的大小
      max-pool-prepared-statement-per-connection-size: 20 #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall
      use-global-data-source-stat: true
      connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000  #通过connectProperties属性来打开mergeSql功能;慢SQL记录
      #配置监控服务器
      stat-view-servlet:
        login-username: admin
        login-password: 123456
        reset-enable: false
        url-pattern: /druid/*
      web-stat-filter:
        url-pattern: /* #添加过滤规则
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #忽略过滤格式
#mybatis集成
mybatis:
  mapper-locations: classpath:com/../../mapper/*.xml

三:编写dao层接口

使用注解:@Mapper

四:编写xml文件sql语句



<mapper namespace="dao层类路径">
	
	<select>select>
	
	<insert>insert>
	
	<update>update>
	
	<delete>delete>
	......
mapper>

你可能感兴趣的:(java,mysql)