Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/create1st/spring-boot-starter-mybatis

Spring Boot MyBatis Starter
https://github.com/create1st/spring-boot-starter-mybatis

Last synced: about 7 hours ago
JSON representation

Spring Boot MyBatis Starter

Awesome Lists containing this project

README

        

# spring-boot-starter-mybatis

This is unofficial Spring Boot Starter for MyBatis

In opposite to official [MyBatis Spring Boot support](https://github.com/mybatis/mybatis-spring-boot) it is following
the convention of other Spring Boot data repositories and implements proper Spring Data interfaces.
Advantage of such approach is almost zero effort on switching the persistence layer.

## Issue Tracking

[GitHub Issues](https://github.com/create1st/spring-boot-starter-mybatis/issues)

## Maven dependency

```xml

com.create
spring-boot-starter-mybatis
1.3.0.RELEASE

```

## Default properties configuration

```properties
spring.mybatis.mapperLocations=classpath*:mapper/**/*.xml # mappers file
spring.mybatis.typeAliasesPackage= # domain object's package
spring.mybatis.typeHandlersPackage= # handler's package
spring.mybatis.executorType=simple # mode of execution. Default is SIMPLE
```

## Sample Spring Configuration

```java
@Configuration
@EnableMyBatisRepositories({
"com.create.mybatis.repository"
})
@EnableAutoConfiguration
public class MyBatisConfiguration {

}
```

## Entity definition

```java
public class Person {
@Id
private Long id;
private String firstName;
private String lastName;
// Getters and setters
// ...
}
```

## Sample repository

```java
public interface PersonRepository extends MyBatisRepository {

/**
* Example of usage XML based mappings.
*/
List findByFirstName(final String firstName);

/**
* Example of usage {@link Select} annotation for query construction.
*/
@Select("SELECT id, firstName, lastName FROM person WHERE lastName = #{lastName}")
List findByLastName(final String lastName);
}
```

## Mapper XML

In order to define queries in XML mapper file you have to follow the convention defined for query ID.
* All **find** methods should have statement definition id starting with **find**
* All **delete** methods should have statement definition id starting with **delete**
* All **save** methods should have statement definition id starting with **save** for persisting a new entity and with **update** for modification of already existing entity

```xml






INSERT INTO person(firstName, lastName)
VALUES(#{entity.firstName}, #{entity.lastName})

SELECT TOP 1 id as returnedId
FROM person
ORDER BY returnedId DESC


UPDATE person
SET
firstName = #{entity.firstName},
lastName = #{entity.lastName}
WHERE id = #{entity.id}


SELECT id, firstName, lastName
FROM person

WHERE id = #{id}


SELECT id, firstName, lastName
FROM person
WHERE firstName = #{firstName}


DELETE
FROM person

WHERE id = #{id}

```