SpringBoot+Mybatis+Mysq的基础环境搭建

SpringBoot+Mybatis+Mysql框架的搭建


SpringBoot基础框架的搭建

这是我之前写的一遍博客,https://blog.csdn.net/qq_15006743/article/details/88174258

SpringBoot+Mybatis+Mysql的开发

  • pom中添加依赖
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <!--要加版本号 因为版本管理里面没有这个-->
      <version>2.0.0</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.42</version>
      <scope>runtime</scope>
    </dependency>
    <!--事务管理-->
    <!--
    在Spring Boot中,当我们使用了spring-boot-starter-jdbc或spring-boot-starter-data-jpa依赖的时候,
    框 架会自动默认分别注入DataSourceTransactionManager或JpaTransactionManager。这样就可以直接通过注解进行控制事务了
    -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
      <version>2.1.3.RELEASE</version>
    </dependency>

我自己搭建的时候,发现druid这个alibaba提供的数据库管理工具不错,用的是druid这个进行监控的

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.3</version>
    </dependency>
  • application.yml的配置
    数据库相关信息的配置:
  • spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/cy?useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: root
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        filters: stat,wall,log4j
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
        connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
    

    mybatis 配置:

    mybatis:
      #xml的路径
      mapper-locations: classpath*:com/kido/mapper/*.xml
      type-aliases-package: com.kido
      configuration:
        #表示当查出来是null的时候,也会返给map中,不会不显示该值
        call-setters-on-nulls: true
        #mybatis默认是属性名和数据库字段名一一对应的,即数据库表列:user_name 实体类属性:user_name
        #但是java中一般使用驼峰命名 数据库表列:user_name 实体类属性:userName
        #在Springboot中,可以通过设置map-underscore-to-camel-case属性为true来开启驼峰功能。
        map-underscore-to-camel-case: true
    
  • 创建相应实体
          
  • 启动事务,以及自动扫描mapper类
  • 发生的错误情况
  • https://blog.csdn.net/qq_15006743/article/details/88179023
    发布了45 篇原创文章 · 获赞 28 · 访问量 4万+

    猜你喜欢

    转载自blog.csdn.net/qq_15006743/article/details/88179741