今天测试了使用mariadb的使用,我使用的springboot  +  mariadb 来操作数据库,和以前的代码基本一样,就数据变成了mariadb ,驱动还是使用mysql的。

pom 文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.2.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.handan</groupId>
   <artifactId>deda</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>deda</name>
   <description>Demo project for Spring Boot</description>
   <properties>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>2.1.1</version>
      </dependency>
      <!-- 与数据库操作相关依赖 -->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>

      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.62</version>
      </dependency>
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
      </dependency>

      <!--<dependency>-->
         <!--<groupId>com.oracle.ojdbc</groupId>-->
         <!--<artifactId>ojdbc8</artifactId>-->
         <!--<scope>runtime</scope>-->
      <!--</dependency>-->

      <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis-typehandlers-jsr310</artifactId>
         <version>1.0.1</version>
      </dependency>


      <!--<dependency>-->
         <!--<groupId>io.springfox</groupId>-->
         <!--<artifactId>springfox-swagger2</artifactId>-->
         <!--<version>2.9.2</version>-->
      <!--</dependency>-->
      <!--<dependency>-->
         <!--<groupId>io.springfox</groupId>-->
         <!--<artifactId>springfox-swagger-ui</artifactId>-->
         <!--<version>2.9.2</version>-->
      <!--</dependency>-->

      <!--<dependency>-->
         <!--<groupId>io.swagger</groupId>-->
         <!--<artifactId>swagger-core</artifactId>-->
         <!--<version>1.5.22</version>-->
      <!--</dependency>-->

      <!--<dependency>-->
         <!--<groupId>io.swagger</groupId>-->
         <!--<artifactId>swagger-models</artifactId>-->
         <!--<version>1.5.22</version>-->
      <!--</dependency>-->

      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.25</version>
      </dependency>
      <dependency>
         <groupId>ch.qos.logback</groupId>
         <artifactId>logback-core</artifactId>
         <version>1.1.11</version>
      </dependency>
      <dependency>
         <groupId>ch.qos.logback</groupId>
         <artifactId>logback-classic</artifactId>
         <version>1.1.11</version>
      </dependency>
      <!--后台调用restful接口的jar包-->
      <dependency>
         <groupId>org.apache.httpcomponents</groupId>
         <artifactId>httpclient</artifactId>
         <version>4.5.6</version>
      </dependency>


      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
               <excludes>
                  <exclude>
                     <groupId>org.projectlombok</groupId>
                     <artifactId>lombok</artifactId>
                  </exclude>
               </excludes>
            </configuration>
         </plugin>
      </plugins>
   </build>

</project>

mapper 层的代码如下

@Repository
public interface  StuMapper {
    @Insert("insert into stu(sid,name,number,age) values(#{sid},#{name},#{number},#{age})")
    public void add(Stu stu);
}

配置文件如下

server:
  port: 8050
  tomcat:
    max-http-header-size: 8192
    uri-encoding: utf-8
#测试环境
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=CTT&useSSL=false&useUnicode=true&autoReconnect=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  type-aliases-package: com.handan.deda.entity
  mapper-locations:  classpath:mappers/*.xml
swagger:
   enable: true

最后是可以成功添加数据,估计是两个的驱动是可以互换的

开启远程连接代码入下:

 grant all privileges on *.* to 'root'@'%'identified by 'root';  

刷新配置: flush privileges;

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐