如果您正在寻找轻量级 ORM,我会推荐 jORM。请注意,我的回答是有偏见的,因为我是该项目的贡献者之一。
我们决定编写轻量级替代方案的主要原因是(需要):
- Hazzlefree 配置
- 清除事务定义
- 内存管理和速度
- 简单的代码生成
- 手动调整的 SQL
简而言之;发展迅速。
示例配置
DataSource dataSource = new DataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/moria");
dataSource.setUsername("gandalf");
dataSource.setPassword("mellon");
Database.configure("moria", dataSource); // now there's a named database
示例记录
@Jorm(database="moria", table="goblins", id="id")
public class Goblin extends Record {
public Integer getId() {
return get("id", Integer.class);
}
public void setId(Integer id) {
set("id", id);
}
public Integer getTribeId() {
return get("tribe_id", Integer.class);
}
public void setTribeId(Integer id) {
set("tribe_id", id);
}
public Tribe getTribe() {
return get("tribe_id", Tribe.class);
}
public void setTribe(Tribe tribe) {
set("tribe_id", tribe);
}
public String getName() {
return get("name", String.class);
}
public void setName(String name) {
set("name", name);
}
}
映射查询示例
Goblin goblin = Record.findById(Goblin.class 42);
List<Goblin> goblins = Record.findAll(Goblin.class);Goblin bolg = Record.find(Goblin.class, new Column("name", "Bolg"));
自定义查询示例
Tribe tribe = new Tribe();
tribe.setId(1);
String name = "Azog";
Goblin azog = Record.select(
Goblin.class,
"SELECT * FROM goblins WHERE name = #1# AND tribe_id = #2:id#",
name, // #1#
tribe // #2#
);
Goblin bolg = Record.find(Goblin.class, "SELECT * FROM goblins WHERE name = #1#", "Bolg");