https://github.com/ifyun/simpleorm
A super simple ORM.
https://github.com/ifyun/simpleorm
Last synced: 3 months ago
JSON representation
A super simple ORM.
- Host: GitHub
- URL: https://github.com/ifyun/simpleorm
- Owner: ifyun
- License: mit
- Created: 2020-03-21T11:37:21.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-29T00:54:07.000Z (almost 3 years ago)
- Last Synced: 2025-01-25T10:28:13.047Z (4 months ago)
- Language: Java
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Simple ORM
This is a super simple ORM using Java Reflect and Dynamic Proxy.
### Use
#### Crete Factory
Set a `DataSource` for `DaoFactory` .
```java
// Create a DataSource (DBCP, HikariCP and so on).
DataSource dataSource = ...;
DaoFactory factory = new DaoFactory(dataSource);
```#### Create Dao Interface
Use `@Select` , `@Insert` ,`@Update` and `@Delete` to write DML statements.
```java
public interface ItemDao {@Select(sql = "select * from item")
List> getAll();@Select(sql = "select * from item where id = ?")
HashMap getById(long id);@Insert(sql = "insert into item(name) values(?)", useGeneratedKey = true)
long add(String name);@Delete(sql = "delete from item where id = ?")
long removeById(long id);
}
```> The `@Select` annotation only support `HashMap` as return type currently.
> set `useGeneratedKey` to `true` could return auto-generated primary key.#### Create Proxy Object
Use proxy object to manipulate data.
```java
ItemDao itemDao = (ItemDao) factory.create(ItemDao.class);
// Select all
List> items = itemDao.getAll();
// Select by id
HashMap item = itemDao.getById(id);
// Insert
long id = itemDao.add("NewItem");
```