项目作者: 11039850

项目描述 :
Very Simple ORM
高级语言: Java
项目地址: git://github.com/11039850/monalisa-orm.git
创建时间: 2015-06-14T07:51:35Z
项目社区:https://github.com/11039850/monalisa-orm

开源协议:

下载


Features

Join the chat at https://gitter.im/monalisa-orm/community

  • Using the database takes only 1 line of code
  • Generic ORM functions(CRUD)
  • Auto-Generate DTOs
  • Object fields
  • Reload SQL dynamically
  • Sharding support
  • Write multi-line strings easily

5 minutes video: Youtube / YouKu

Example Project | Eclipse Plugin

Usage

Using Database with plugin

image

Using Database without plugin

  1. @DB(url="jdbc:mysql://127.0.0.1:3306/test" ,username="root", password="root")
  2. public interface TestDB{
  3. public static DBConfig DB=DBConfig.fromClass(TestDB.class);
  4. public static class NewsModelGenerator{
  5. public static void main(String[] args) {
  6. DBModelGenerateMain.generateModelClass(TestDB.class);
  7. }
  8. }
  9. }
  1. new User().setName("zzg.zhou").setStatus(1).save();

Auto-Generate DTOs (need eclipse-plugin)

image

  1. public class UserBlogDao {
  2. @Select(name="test.result.UserBlogs") // <--- Auto create/update: test.result.UserBlogs
  3. public List selectUserBlogs(int user_id){ // <--- Auto replace List to List<UserBlogs>
  4. Query q=TestDB.DB.createQuery();
  5. q.add(""/**~{
  6. SELECT a.id, a.name, b.title, b.content, b.create_time
  7. FROM user a, blog b
  8. WHERE a.id=b.user_id AND a.id=?
  9. }*/, user_id);
  10. return q.getList(); // <--- Auto replace getList() to getList<UserBlogs>
  11. }
  12. }

Database Service

Direct Database Access by HTTP, see: monalisa-service

  1. curl http://localhost:8080/your_web_app/dbs/testdb/your_table_name

Query Example

Insert

  1. //insert
  2. new User().setName("zzg.zhou").setStatus(1).save();
  3. //parse data from type: Map, json/xml string, JsonObject(Gson), HttpServletRequest, JavaBean
  4. new User().parse("{'name':'oschina','status':0}").save();
  5. new User().parse("<data> <name>china01</name><status>1</status> </data>").save();
  6. new User().parse(request).save();
  7. //Object field
  8. Address address=new Address("guangdong","shenzhen");
  9. user.setAddress(address).save();
  10. //File field
  11. String detail_save_path="path/001.txt";
  12. String content="This is a big text.";
  13. user.setDetail(path,content.getBytes()).save();
  14. user.getDetailAsString();

Delete

  1. //delete user by primary key or unique key
  2. user.delete();
  3. //SQL: DELETE FROM `user` WHERE `name`='china01'
  4. User.WHERE().name.eq("china01").delete();
  5. User.DELETE().deleteAll();
  6. User.DELETE().truncate();

Update

  1. //update by primary key
  2. User user=User.SELECT().selectOne("name=?", "zzg.zhou");
  3. user.setStatus(3).update();
  4. //SQL: UPDATE user SET name='tsc9526' WHERE name like 'zzg%'
  5. User updateTo=new User().setName("tsc9526");
  6. User.WHERE().name.like("zzg%").update(updateTo);

Select

  1. //select by primary key
  2. User.SELECT().selectByPrimaryKey(1);
  3. //SQL: SELECT * FROM `user` WHERE `name` = 'zzg.zhou'
  4. User.SELECT().selectOne("name=?", "zzg.zhou");
  5. //SQL: SELECT `name`, `status` FROM `user`
  6. User.SELECT().include("name","status").select();
  7. //SQL: SELECT * FROM `user` WHERE (`name` like 'zzg%' AND `status` >= 0)
  8. // OR (`name` = 'zzg' AND `status` > 1) ORDER BY `status` ASC
  9. for(User x:User.WHERE()
  10. .name.like("zzg%").status.ge(0)
  11. .OR()
  12. .name.eq("zzg").status.gt(1)
  13. .status.asc()
  14. .SELECT().select()){ //SELECT / delete / update
  15. System.out.println(x);
  16. }
  17. //Page
  18. Page<User> page=User.WHERE()
  19. .name.like("zzg%")
  20. .status.in(1,2,3)
  21. .SELECT().selectPage(10,0);

Query

  1. TestDB.DB.select("SELECT * FROM user WHERE name like ?","zzg%");
  2. TestDB.DB.createQuery()
  3. .add("SELECT * FROM user WHERE name like ?","zzg%")
  4. .getList(User.class);

DataTable

  1. Query q=new Query(TestDB.DB);
  2. DataTable<DataMap> rs=q.add("SELECT * FROM user WHERE name like ?","zzg%")
  3. .add(" AND status ").in(1,2,3)
  4. .getList();
  5. //Query inside DataTable
  6. //SQL: SELECT name, count(*) as cnt FROM _THIS_TABLE WHERE status>=0 GROUP BY name ORDER BY name ASC
  7. DataTable<DataMap> newTable=rs.select("name, count(*) as cnt","status>=0","name ASC","GROUP BY name");

Transaction

  1. //transaction
  2. Tx.execute(new Tx.Atom() {
  3. public int execute() {
  4. new User().setName("name001").setStatus(1).save();
  5. new User().setName("name002").setStatus(2).save();
  6. //... other database operation
  7. return 0;
  8. }
  9. });

Record

  1. //Dynamic model: Record
  2. Record r=new Record("user").use(TestDB.DB);
  3. r.set("name", "jjyy").set("status",1)
  4. .save();
  5. //SQL: SELECT * FROM `user` WHERE (`name` like 'jjyy%' AND `status` >= 0)
  6. // OR (`name` = 'zzg' AND `status` > 1) ORDER BY `status` ASC
  7. for(Record x:r.WHERE()
  8. .field("name").like("jjyy%").field("status").ge(0)
  9. .OR()
  10. .field("name").eq("zzg").field("status").gt(1)
  11. .field("status").asc()
  12. .SELECT().select()){
  13. System.out.println(x);
  14. }
  15. //SQL: DELETE FROM `user` WHERE `name` like 'jjyy%' AND `status` >= 0
  16. r.WHERE()
  17. .field("name").like("jjyy%").field("status").ge(0)
  18. .delete();

Sharding

  1. public class ShardingUser extends User{
  2. //Override
  3. public Table table(){
  4. String tableName= "user_"+( getId()%10 );
  5. return ModelMeta.createTable(tableName);
  6. }
  7. //Override
  8. public DBConfig db(){
  9. return getId()<10 ? TestDB.DB1 : TestDB.DB2;
  10. }
  11. }
  12. ShardingUser user1=new ShardingUser(1);
  13. user1.save(); //Will be saved to table: user_1, database: TestDB.DB1
  14. ShardingUser user2=new ShardingUser(15);
  15. user2.save(); //Will be saved to table: user_5, database: TestDB.DB2

Multi-line strings

see Multiple-line-syntax

  1. public static void main(String[] args) {
  2. String name="zzg";
  3. String lines = ""/**~!{
  4. SELECT *
  5. FROM user
  6. WHERE name="$name"
  7. }*/;
  8. System.out.println(lines);
  9. }

Output will be:

  1. SELECT *
  2. FROM user
  3. WHERE name="zzg"

Details

Maven:

  1. <dependency>
  2. <groupId>com.tsc9526</groupId>
  3. <artifactId>monalisa-orm</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>

Change Log

  • 2.1.0 Add oracle dialect
  • 2.0.0 Only Mysql

TODO list

  • Other database’s dialect
  • Automatic refresh the query cache in the background

If you have any ideas or you want to help with the development just write me a message.

zzg zhou, 11039850@qq.com