Skip to content

Get started

olestxcode edited this page Nov 6, 2020 · 1 revision

Welcome to the litebase wiki!

A litebase project has some basic interfaces and classes:

  • Column - this interface is a Java presentation of relational table's column.
  • ColumnBuilder - a builder class for Column creating.
  • DataContainer<T, ID> - this interface is a Java presentation of relational tables.
  • DataContainerBuilder<T, ID> - a builder class for DataContainer<T, ID> creating.
  • Data - this interface provides an access to a query result.
  • HashMapData - a simple implementation of Data class based on HashMap.

How to create a new table using litebase?

Let's create a Person class:

import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class Person {

    private final long id;
    private String name, surname;  
}

Let's create a DataContainer for Person class:

DataContainer persons = new MySqlDataContainerBuilder()  
        .withName("person_data")  
        .withDataSource(myDataSource)  
        .withColumn(new ColumnBuilder(Long.class)  
                .setName("id")  
                        .setPrimary(true)  
                        .build())  
                .withColumn(new ColumnBuilder(String.class)  
                        .setName("name")  
                        .build())  
                .withColumn(new ColumnBuilder(String.class)  
                        .setName("surname")  
                        .build())  
        .withDataMapper(object -> {  
                Data data = new HashMapData();  
                data.writeLongValue("id", object.getId());  
                data.writeStringValue("name", object.getName());  
                data.writeStringValue("surname", object.getSurname());  
                return data;  
            })  
        .withObjectMapper(data -> {  
            return new Person(data.getLongValue("id"), data.getStringValue("name"), data.getStringValue("surname"));  
        })  
        .build();

Now we can use persons!

We can:

  • create(); - create a table
  • delete() - delete a table
  • delete(Person) - delete a specified person from table
  • findById(Long) - find Person by id
  • and some other methods you can use.

Clone this wiki locally