Giter VIP home page Giter VIP logo

spring-data-mybatis's Introduction

Spring Data MyBatis

Build Status Gitter chat Maven Central License: Apache 2.0

Simplified Chinese

Documentation

Example Project

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use data access technologies. This module deals with enhanced support for MyBatis based data access layers.

Features

  • Implementation of CRUD methods for normal Entities
  • Dynamic query generation from query method names
  • Implementation domain base classes providing basic properties
  • Support for transparent auditing (created, last changed)
  • Possibility to integrate custom repository code
  • Easy Spring integration with custom namespace
  • Support MySQL, Oracle, Sql Server, H2, PostgreSQL, etc.
  • Support SpringBoot 2.x

Getting Help

If you have any question, please record a issue to me.

Quick Start

Download the jar through Maven:

<dependency>
  <groupId>io.easybest</groupId>
  <artifactId>spring-data-mybatis</artifactId>
  <version>2.0.1.RELEASE</version>
</dependency>

The simple Spring Data Mybatis configuration with Java-Config looks like this:

@Configuration
@EnableMybatisRepositories(
        value = "org.springframework.data.mybatis.repository.sample",
        mapperLocations = "classpath*:/org/springframework/data/mybatis/repository/sample/mappers/*Mapper.xml"
)
public class TestConfig {

    @Bean
    public DataSource dataSource() throws SQLException {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:/test-init.sql").build();
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        return factoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

Create an entity:

@Entity
@Table(name = "user")
public class User extends LongId {

  
  @Condition
  private String firstname;
  @Condition(type=Condition.Type.CONTAINING)  private String lastname;
  private String fullName;
  @Conditions({@Condition, @Condition(type=Condition.Type.CONTAINING,properties = "fuzzyName")})
  private String fullName;
  @Transient
  private String fuzzyName;
  @Column(name = "usertype")
  private String status;
  // Getters and setters
  // (Firstname, Lastname)-constructor and noargs-constructor
  // equals / hashcode
}

When using findAll method of MybatisRepository the @Condition or @Conditions annotations will work

Create a repository interface in com.example.repositories:

public interface UserRepository extends MybatisRepository<User, Long> {
  List<User> findByLastname(String lastname);  
  
  @Query("select firstname from user")
  List<String> findUsersFirstName();
  
}

Write a test client:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class UserRepositoryIntegrationTest {
     
  @Autowired UserRepository repository;
     
  @Test
  public void sampleTestCase() {
    User dave = new User("Dave", "Matthews");
    dave = repository.save(dave);
         
    User carter = new User("Carter", "Beauford");
    carter = repository.save(carter);
         
    List<User> result = repository.findByLastname("Matthews");
    assertThat(result.size(), is(1));
    assertThat(result, hasItem(dave));
  }
}

Use Spring Boot

add the jar through Maven:

<dependency>
    <groupId>io.easybest</groupId>
    <artifactId>spring-data-mybatis-boot-starter</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

If you need custom Mapper, you should add property in your application.yml like this:

mybatis:
  mapper-locations: "classpath*:/mapper/**/**Mapper.xml"

And you need not to define SqlSessionFactory manually.

The full test code like this:

@SpringBootApplication
public class SpringDataMybatisSamplesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataMybatisSamplesApplication.class, args);
    }

    @Bean
    public CommandLineRunner dummyCLR(ReservationRepository reservationRepository) {
        return args -> {
            Stream.of("Tom", "Jack", "Apple")
                    .forEach(name -> reservationRepository.save(new Reservation(name)));
        };
    }

}

@RepositoryRestResource // here we use RepositoryRestResource
interface ReservationRepository extends MybatisRepository<Reservation, Long> {
}

@Entity
@Table(name = "user")
class Reservation extends LongId {

    private String reservationName;

    public Reservation() {
    }

    public Reservation(String reservationName) {
        this.reservationName = reservationName;
    }

    public String getReservationName() {
        return reservationName;
    }

    @Override
    public String toString() {
        return "Reservation{" +
                "reservationName='" + reservationName + '\'' +
                '}';
    }
}

The full example you can find in https://github.com/easybest/spring-data-mybatis-samples

Contributing to Spring Data MyBatis

Here are some ways for you to get involved in the community:

  • Github is for social coding: if you want to write code, we encourage contributions through pull requests from forks of this repository.

Help me better - Donation

paypal

spring-data-mybatis's People

Contributors

easybest avatar overcat 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.