https://github.com/yananhub/flying-mybatis
Let Mybatis fly, provide some basic CRUD methods by simply inheriting AutoMapper, without losing any of the original Mybatis Spring functionally.
https://github.com/yananhub/flying-mybatis
database mybatis spring ssm
Last synced: 6 months ago
JSON representation
Let Mybatis fly, provide some basic CRUD methods by simply inheriting AutoMapper, without losing any of the original Mybatis Spring functionally.
- Host: GitHub
- URL: https://github.com/yananhub/flying-mybatis
- Owner: yananhub
- License: apache-2.0
- Created: 2021-04-05T07:59:35.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-16T06:36:56.000Z (almost 2 years ago)
- Last Synced: 2024-07-16T08:42:55.428Z (almost 2 years ago)
- Topics: database, mybatis, spring, ssm
- Language: Java
- Homepage:
- Size: 105 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**Read this in other languages: [English](README.md), [中文](README_zh.md).**
# Flying Mybatis
Let Mybatis fly, provide some basic CRUD methods by simply inheriting `AutoMapper`, without losing any of the original
Mybatis Spring functionally.
Use steps:
Add the jar package dependency, using gradle as an example:
```groovy
dependencies {
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:x.y.z'
implementation 'tech.yanand:flying-mybatis:x.y.z' // Flying Mybatis jar
}
```
Add configuration in `application.properties` to turn on the camel case conversion, which is required:
```properties
mybatis.configuration.map-underscore-to-camel-case=true
# XML mapper can be turned on if needed, but it is not necessary
mybatis.mapper-locations=classpath:mapper/*.xml
```
Add a Spring configuration class to your project:
```java
@Configuration
@Import(AutoMapperProcessor.class)
class AutoMapperConfig {
}
```
Create a new entity class that maps the tables and columns of the DB using `@Table` and `@Column` annotations.
Take the `Book` entity as an example:
```java
@Table
public class Book {
@Column
@PrimaryKey
private Long id;
@Column
private String name;
@Column("release_date")
private LocalDate publishDate;
}
```
Define the `BookMapper` interface, which extends from the `AutoMapper` interface.
It will derive basic methods of adding, deleting, modifying, selecting, and their functionality.
```java
@Mapper
public interface BookMapper extends AutoMapper {
// Custom methods that define SQL in XML Mapper.
Collection selectByName(@Param("name") String name);
}
```
Use the `BookMapper`:
```java
@Autowired
private BookMapper bookMapper;
void testBookMapper() {
int result = bookMapper.insertAll(List.of(book1, book2));
int result = bookMapper.insert(book3);
List bookList = bookMapper.selectAll();
Book book = bookMapper.selectById(1L);
List bookList = bookMapper.selectAllById(List.of(1L, 0L));
List bookList = bookMapper.selectAllByColumn("name", "test_book_2");
long count = bookMapper.countAll();
int result = bookMapper.updateAll(List.of(book3, book4));
int result = bookMapper.updateAllSelective(List.of(book3, book4));
int result = bookMapper.update(book3);
int result = bookMapper.updateSelective(book3);
int result = bookMapper.deleteById(1L);
int result = bookMapper.deleteAllById(List.of(0L, 1L));
bookMapper.deleteAll();
}
```