Storm Object Relational Mapping
| src | 4 years ago | ||
| .gitignore | 6 years ago | ||
| README.md | 4 years ago | ||
| lombok.config | 6 years ago | ||
| pom.xml | 5 years ago | ||
Context
[
{
"name" : "H2",
"driver" : "org.h2.Driver",
"url" : "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
"username" : "****",
"password" : "****"
},
{
"name" : "ORACLE",
"driver" : "oracle.jdbc.driver.OracleDriver",
"url" : "jdbc:oracle:thin:@server:port:SID",
"username" : "****",
"password" : "****"
}
]
Interface
@Database("H2")
private interface UserRepository {
@Select(sql = "select * from users")
public List<User> fetchAll();
@Delete(sql = "delete from users where id = #{id}")
public int delete(int id);
@Insert(sql = "insert into users (firstname, lastname, employee_id) values (#{firstname}, #{lastname}, #{employeeID})")
public int insert(@Param("firstname") String first, @Param("lastname") String last, @Param("employeeID") int id);
}
Use
UserRepository repository = StormRepository.newInstance(UserRepository.class); List<User> users = repository.fetchAll();
Entity Mapping
@Table("users")
public class User {
@Id(generated = Generated.AUTO)
private int id;
private String firstName;
private String lastName;
@ColumnProperty(name = "employee_id")
private int employeeID;
@Transient
private boolean hourly;
}
Select
@Select(sql = "select * from users") List<User> fetchAll();
Delete
@Delete
public int delete(User user);
@Delete
public int deleteAll(User... users);
@Delete(sql = "delete from users where id = #{id}")
public int deleteById(int id);
Update
@Update
public int update(User user);
@Update
public int updateAll(User... users);
@Update(sql = "update users set firstname = #{firstname} where id = #{id}")
public int updateFirstName(@Param("firstname") String firstname, @Param("id") int id);
Insert
@Insert
public int insert(User user);
@Insert
public int insertAll(User... users);
@Insert(sql = "insert into users (firstname, lastname, employee_id) values (#{firstname}, #{lastname}, #{employeeID})")
public int insert(@Param("firstname") String first, @Param("lastname") String last, @Param("employeeID") int id);
Insert Batch
@InsertBatch
public int insert(User... users);
@InsertBatch
public int insert(List<User> users);
Peek at SQL statement
@Database("H2")
private interface UserRepository extends Peek<UserRepository> {
...
}
Consumer<String> print = e -> System.out.println(e);
UserRepository repository = StormRepository.newInstance(UserRepository.class).peek(print);
repository.count();
Output: select count(*) from users
Add Paging to @Select
new Paging(page, rows);
@Database("H2")
private interface UserRepository {
@Select(sql = "select * from users order by id")
public User[] fetchAllOrdered(Paging paging);
}
Paging paging = new Paging(2, 2);
User[] users = repository.fetchAllOrdered(paging);
Note: Paging can be passed in at any point in the argument list
@Select(sql = "select * from users where lastname = #{lastname}")
public findByLastName(Paging paging, @Param("lastname") String lastname);
// or
public findByLastName(@Param("lastname") String lastname, Paging paging);