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:
- Entity class with JPA annotations
- DTO classes (request and response)
- Mapper to convert between entity and DTO
- Service interface and implementation
- Repository interface
- 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:
<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:
# Database connectionspring.datasource.url=jdbc:mariadb://localhost:3306/myappspring.datasource.username=your_usernamespring.datasource.password=your_passwordspring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# JPA configurationspring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=trueI 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:
<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:
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: falseThis 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:
mvn spring-crud-generator:generateThis 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.javaNow I can use the generated code in my application. The UserController has all CRUD endpoints:
GET /api/users- Get all usersGET /api/users/{id}- Get user by IDPOST /api/users- Create new userPUT /api/users/{id}- Update userDELETE /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 serverI fixed it by changing the URL to jdbc:mariadb://:
spring.datasource.url=jdbc:mariadb://localhost:3306/myappWrong 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:
- Uses MariaDB-specific SQL dialect in generated code
- Configures the right JDBC driver (
org.mariadb.jdbc.Driver) - Generates MariaDB-compatible Flyway migrations
- 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:
- Add MariaDB JDBC driver dependency
- Configure
jdbc:mariadb://URL in application properties - Set
<databaseType>MARIADB</databaseType>in the plugin - Define entities in YAML configuration
- 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:
- 👨💻 Spring CRUD Generator GitHub Repository
- 👨💻 MariaDB Connector Java Documentation
- 👨💻 Spring Boot Data JPA Documentation
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments