dant3 / micronaut-mybatis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MyBatis Micronaut Adapter

License

mybatis-spring

Micronaut-MyBatis adapter is an easy-to-use Micronaut bridge for MyBatis sql mapping framework.

Disclaimer

This is a very basic version, which has a lot to cover. Right now only Java api is supported. See tests for examples.

Essentials

Declare your regular mybatis mapper, like this:

@Mapper
public interface UserMapper {
  @Select("select * from users order by id")
  List<User> getUsers();

  @Select("select * from users where id=#{value}")
  User getUser(String userId);

  @Insert("insert into users VALUES (#{id},  #{name})")
  void insert(User user);
}

Notice the @Mapper annotation - it declares your mapper as a @Singleton so that you able to inject this later on in your service:

@Singleton
public class FooService {
  private final UserMapper userMapper;

  public FooService(UserMapper userMapper) {
    this.userMapper = userMapper;
  }

  public User doSomeBusinessStuff(String userId) {
    return this.userMapper.getUser(userId);
  }
}

Thats it! Micronaut-mybatis will take care of instantiating SqlSessionFactory, creating a new session and using it to call your mapper.

Every mapper call is made in a single transaction, so, if you want to make multiple calls withing transaction, just declare a default method in your mapper:

@Mapper
public interface UserMapper {
  @Select("select * from users order by id")
  List<User> getUsers();

  @Select("select * from users where id=#{value}")
  User getUser(String userId);

  @Insert("insert into users VALUES (#{id},  #{name})")
  void insert(User user);
  
  default User insertAndSelect(User user) {
    insert(user);
    return getUser(user.getId());
  }
}

About

License:Apache License 2.0


Languages

Language:Java 100.0%