Mybatis is is adopted in orm of many projects.If MyBatis can be integrated, it will quickly convert the application based on Spring Framework to the application based on Quarkus Framework.
Why not integrate Mybatis into the system of quarkus?
1k views Asked by 任钢的编程田地 At
2
There are 2 answers
0
On
Of course, You can integrate Mybatis into the system of quarkus.
quarkus-mybatis 0.0.1 first version released at Oct, 2020. Latest version of quarkus-mybatis 0.0.9 is released at Jun 2021.
Lot of companies use Quarkus in their tech stacks, including Backbase, Biting Bit, and JustChunk.
Demo:
Create new Quarkus project using https://code.quarkus.io/
Add quarkus-mybatis extension to your project. Here I am using Maven for dependency management.
Maven
<dependency>
<groupId>io.quarkiverse.mybatis</groupId>
<artifactId>quarkus-mybatis</artifactId>
<version>0.0.9</version>
</dependency>
Datasource configuration:
application.properties
quarkus.datasource.db-kind = h2
quarkus.datasource.username = sa
# quarkus.datasource.password =
quarkus.datasource.jdbc.url = jdbc:h2:mem:test
quarkus.mybatis.initial-sql=schema.sql
schema.sql:
create table users
(
id integer not null,
firstName varchar(255) not null,
lastName varchar(255) not null,
emailId varchar(255) not null,
primary key(id)
);
Create User Model:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class User {
private long id;
private String firstName;
private String lastName;
private String emailId;
}
Create User MyBatis Repository:
@Mapper
public interface UserRepository {
@Select("select * from users")
public List<User> findAll();
@Select("SELECT * FROM users WHERE id = #{id}")
public User findById(long id);
@Delete("DELETE FROM users WHERE id = #{id}")
public int deleteById(long id);
@Insert("INSERT INTO users(id, firstName, lastName,emailId) " +
" VALUES (#{id}, #{firstName}, #{lastName}, #{emailId})")
public int insert(User user);
@Update("Update users set firstName=#{firstName}, " +
" lastName=#{lastName}, emailId=#{emailId} where id=#{id}")
public int update(User user);
}
Endpoint:
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("id") Long id) {
return userResource.findById(id);
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void updateUser(User user) {
userResource.update(user);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void addUser(User user) {
userResource.insert(user);
}
@DELETE
@Path("/{id}")
public void deleteUser(@PathParam("id") Long id) {
userResource.deleteById(id);
}
}
Build application jar file: mvn clean package
Start application : java -jar quarkus-run.jar
Saturday, 26 March 2022 - Updated
Latest version quarkus-mybatis 1.0.2
<!-- https://mvnrepository.com/artifact/io.quarkiverse.mybatis/quarkus-mybatis -->
<dependency>
<groupId>io.quarkiverse.mybatis</groupId>
<artifactId>quarkus-mybatis</artifactId>
<version>1.0.2</version>
</dependency>
Reference, Build REST CRUD API with Quarkus and MyBatis
There is an extension proposal for it inside Quarkus issue board: https://github.com/quarkusio/quarkus/issues/1958
You can react with +1 on it to show your interest and follow it.
There is even a PR to provides support for it but it didn't seems to make any progress since a few months.