Coder Social home page Coder Social logo

micronaut-mybatis's Introduction

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());
  }
}

micronaut-mybatis's People

Contributors

dant3 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.