monalisa-ormORM软件
monalisa-orm 的主要特点:
- 一行代码使用数据库
- SQL代码自动生成DTOs
- 支持书写多行语法
- 支持对象存储
- 支持分表分库
- 支持HTTP访问数据库
- 动态加载SQL代码
更多用法参考:https://github.com/11039850/monalisa-orm/wiki
一行代码引入数据库:
@DB(url="jdbc:mysql://127.0.0.1:3306/test" ,username="root", password="root")
SQL代码自动生成DTOs:
/** * 数据访问类 */ public class UserBlogDao { final static long $VERSION$= 18L; //!!! 版本号, 每次保存为自动 +1 //@Select 注解指示该方法需自动生成结果类 //默认类名: Result + 方法名, 默认包名:数据访问类的包名+"."+数据访问类的名称(小写) //可选参数:name 指定生成结果类的名称 @Select(name="test.result.UserBlogs") //!!! 保存后会自动修改该函数的返回值为: List -> List //第一次编写时,由于结果类还不存在, 为了保证能够编译正常, //函数的返回值 和 查询结果要用 泛值 替代, 保存后,插件会自动修改. //函数的返回值 和 查询结果 泛值的对应关系分三类如下: //1. List查询 //public DataTable method_name(...){... return Query.getList(); } 或 //public List method_name(...){... return Query.getList(); } // //2. Page查询 //public Page method_name(...){... return Query.Page(); } // //3. 单条记录 //public Object method_name(...){... return Query.getResult(); } // public List selectUserBlogs(int user_id){ Query q=TestDB.DB.createQuery(); q.add(""/**~{ SELECT a.id,a.name,b.title, b.content,b.create_time FROM user a, blog b WHERE a.id=b.user_id AND a.id=? }*/, user_id); //!!! 保存后会自动修改 查询结果为: getList() -> getList return q.getList(); } @Select //自动产生结果类: test.dao.userblogdao.ResultSelectUserBlogsOne public Object selectUserBlogsOne(int user_id){ Query q=TestDB.DB.createQuery(); q.add(""/**~{ SELECT a.id,a.name,b.title, b.content,b.create_time FROM user a, blog b WHERE a.id=b.user_id AND a.id=? }*/, user_id); return q.getResult(); } }
一些基本的数据库操作:
//insert new User().setName("zzg.zhou").setStatus(1).save(); //parse data from type: Map, json/xml string, JsonObject(Gson) //, HttpServletRequest, JavaBean new User().parse("{'name':'oschina','status':0}").save(); new User().parse(" china011 ").save(); //select User.SELECT().selectByPrimaryKey(1); //SQL: SELECT * FROM `user` WHERE `name` = 'zzg.zhou' User.SELECT().selectOne("name=?", "zzg.zhou"); //SQL: SELECT `name`, `status` FROM `user` User.SELECT().include("name","status").select(); Page page=User.WHERE().name.like("zzg%").status.in(1,2,3) .SELECT().selectPage(10,0); System.out.println(page.getTotalRow()); //SQL: SELECT * FROM `user` WHERE `name` like 'zzg%' AND `status` IN(0, 1) for(User x:User.WHERE().name.like("zzg%").status.in(0, 1).SELECT().select()){ System.out.println(x); } //SQL: SELECT * FROM `user` WHERE (`name` like 'zzg%' AND `status` >= 0) // OR (`name` = 'zzg' AND `status` > 1) // ORDER BY `status` ASC for(User x:User.WHERE() .name.like("zzg%").status.ge(0) .OR() .name.eq("zzg").status.gt(1) .status.asc() .SELECT().select()){ //SELECT / delete / update System.out.println(x); } //general query TestDB.DB.select("SELECT * FROM user WHERE name like ?","zzg%"); TestDB.DB.createQuery().add("SELECT * FROM user WHERE name like ?","zzg%") .getList(User.class); Query q=new Query(TestDB.DB); DataTable rs=q.add("SELECT * FROM user WHERE name like ?","zzg%") .add(" AND status ").in(1,2,3) .getList(); for(User x:rs.as(User.class)){ System.out.println(x); } //DataTable query //SQL: SELECT name, count(*) as cnt FROM _THIS_TABLE // WHERE status>=0 GROUP BY name ORDER BY name ASC DataTable newTable= rs.select("name, count(*) as cnt","status>=0","name ASC","GROUP BY name"); //update User user=User.SELECT().selectOne("name=?", "zzg.zhou"); user.setStatus(3).update(); User updateTo=new User().setName("tsc9526"); User.WHERE().name.like("zzg%").update(updateTo); //transaction Tx.execute(new Tx.Atom() { public int execute() { new User().setName("name001").setStatus(1).save(); new User().setName("name002").setStatus(2).save(); //... other database operation return 0; } }); //Dynamic model: Record Record r=new Record("user").use(TestDB.DB); r.set("name", "jjyy").set("status",1) .save(); //SQL: SELECT * FROM `user` WHERE (`name` like 'jjyy%' AND `status` >= 0) // OR (`name` = 'zzg' AND `status` > 1) // ORDER BY `status` ASC for(Record x:r.WHERE() .field("name").like("jjyy%").field("status").ge(0) .OR() .field("name").eq("zzg").field("status").gt(1) .field("status").asc() .SELECT().select()){ System.out.println(x); } //SQL: DELETE FROM `user` WHERE `name` like 'jjyy%' AND `status` >= 0 r.WHERE() .field("name").like("jjyy%").field("status").ge(0) .delete(); //delete user.delete(); //SQL: DELETE FROM `user` WHERE `name`='china01' User.WHERE().name.eq("china01").delete(); //User.DELETE().deleteAll();
支持Java多行字符串编辑:(插件下载)
评论