Skip to content

How to use Spring CRUD Generator with MariaDB

Purpose

This post shows how to set up Spring CRUD Generator with MariaDB to auto-generate Spring Boot CRUD code instead of writing boilerplate manually.

Environment

  • Spring Boot 3.2.0
  • Java 17
  • Maven 3.9
  • MariaDB 10.11
  • Spring CRUD Generator 1.3.0

The Problem

When I create Spring Boot applications with CRUD operations, I write the same code repeatedly. For each entity, I need to create:

  1. Entity class with JPA annotations
  2. DTO classes (request and response)
  3. Mapper to convert between entity and DTO
  4. Service interface and implementation
  5. Repository interface
  6. REST controller with endpoints

I create a simple User entity with fields like id, username, email, and createdAt. Then I write all the CRUD code manually every time.

I tried to find a way to generate this code automatically. I found Spring CRUD Generator, but I wasn’t sure how to set it up with MariaDB since the documentation focused on MySQL and PostgreSQL.

What is Spring CRUD Generator?

Spring CRUD Generator is a Maven plugin that generates Spring Boot CRUD code from a YAML or JSON configuration file. It can generate:

  • Entity classes with JPA annotations
  • DTO classes for API requests and responses
  • Mappers for entity-DTO conversion
  • Service interfaces and implementations
  • REST controllers with CRUD endpoints
  • Optional: OpenAPI documentation, Flyway migrations, and Docker resources

Version 1.3.0 added MariaDB support alongside MySQL, PostgreSQL, and MSSQL.

How to Set Up with MariaDB

Step 1: Add MariaDB Dependency

First, I added the MariaDB connector to my pom.xml:

pom.xml
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.0</version>
</dependency>

I used version 3.0.0 because it’s compatible with Spring Boot 3.x and MariaDB 10.x.

Step 2: Configure MariaDB Connection

Then I set up the database connection in application.properties:

src/main/resources/application.properties
# Database connection
spring.datasource.url=jdbc:mariadb://localhost:3306/myapp
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# JPA configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

I made sure to use jdbc:mariadb:// in the URL, not jdbc:mysql://. This is a common mistake because MariaDB is a fork of MySQL, but it requires its own JDBC URL format.

Step 3: Add Spring CRUD Generator Plugin

Next, I added the Maven plugin to the build section:

pom.xml
<build>
<plugins>
<plugin>
<groupId>com.github.mzivkovicdev</groupId>
<artifactId>spring-crud-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<configFile>src/main/resources/crud-config.yaml</configFile>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<databaseType>MARIADB</databaseType>
</configuration>
<executions>
<execution>
<id>generate-crud</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

The key setting here is <databaseType>MARIADB</databaseType>. This tells the plugin to generate code compatible with MariaDB.

Step 4: Create Configuration File

I created crud-config.yaml in src/main/resources/ and defined my entities:

src/main/resources/crud-config.yaml
project:
basePackage: com.example.myapp
groupId: com.example
artifactId: myapp
entities:
- name: User
table: users
fields:
- name: id
type: Long
primaryKey: true
generated: true
- name: username
type: String
nullable: false
unique: true
- name: email
type: String
nullable: false
- name: createdAt
type: LocalDateTime
nullable: false

This YAML defines a User entity with a primary key id, a unique username, an email field, and a timestamp for when the record was created.

Step 5: Run the Generator

I ran the Maven plugin to generate the code:

Terminal window
mvn spring-crud-generator:generate

This command reads the configuration file and generates all the CRUD code in the target/generated-sources directory.

Step 6: Use the Generated Code

The plugin created this structure:

target/generated-sources/
└── com/
└── example/
└── myapp/
├── entity/
│ └── User.java
├── dto/
│ ├── UserDTO.java
│ └── UserResponseDTO.java
├── mapper/
│ └── UserMapper.java
├── service/
│ ├── UserService.java
│ └── impl/
│ └── UserServiceImpl.java
└── controller/
└── UserController.java

Now I can use the generated code in my application. The UserController has all CRUD endpoints:

  • GET /api/users - Get all users
  • GET /api/users/{id} - Get user by ID
  • POST /api/users - Create new user
  • PUT /api/users/{id} - Update user
  • DELETE /api/users/{id} - Delete user

Common Mistakes

Wrong JDBC URL

I tried using jdbc:mysql:// in the datasource URL because MariaDB is compatible with MySQL. This caused a connection error:

java.sql.SQLException: Could not create connection to database server

I fixed it by changing the URL to jdbc:mariadb://:

spring.datasource.url=jdbc:mariadb://localhost:3306/myapp

Wrong Database Type in Plugin

At first, I set <databaseType>MYSQL</databaseType> in the plugin configuration. The plugin generated code, but some MariaDB-specific features weren’t available.

I changed it to <databaseType>MARIADB</databaseType> and the generated code worked better with MariaDB.

IDE Not Seeing Generated Sources

After running the generator, my IDE didn’t recognize the generated classes. I had to add the target/generated-sources directory as a source root in my IDE.

In IntelliJ IDEA, I right-clicked the directory and selected “Mark Directory as” → “Generated Sources Root”.

Why This Works

Spring CRUD Generator uses templates to generate code based on the configuration. When you specify MARIADB as the database type, the plugin:

  1. Uses MariaDB-specific SQL dialect in generated code
  2. Configures the right JDBC driver (org.mariadb.jdbc.Driver)
  3. Generates MariaDB-compatible Flyway migrations
  4. Sets up proper database connection properties

The plugin follows Spring Boot conventions, so the generated code works seamlessly with Spring Data JPA and Spring Web.

Summary

In this post, I showed how to set up Spring CRUD Generator with MariaDB to auto-generate CRUD code. The key points are:

  1. Add MariaDB JDBC driver dependency
  2. Configure jdbc:mariadb:// URL in application properties
  3. Set <databaseType>MARIADB</databaseType> in the plugin
  4. Define entities in YAML configuration
  5. Run mvn spring-crud-generator:generate

This approach saves time and ensures consistent code structure across your Spring Boot projects.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments