JRails Java ORM 终极解决方案
by
at 2010-10-10 15:43:02
original http://www.javaeye.com/topic/780453
特点:开发速度快,代码量少,对比于Hibernate开发速度快80%以上。
项目主页: http://www.jrails.org
Blog: http://jack.jrails.org
svn checkout http://jrails-core.googlecode.com/svn/trunk/
有兴趣投身开发团队的请留言给我。
- 简单示例 1
配置全局数据库连接 src/config/db.properties
System.db = global_db global_db.datasource =org.apache.commons.dbcp.BasicDataSource global_db.driver = org.gjt.mm.mysql.Driver global_db.url = jdbc:mysql://localhost:3306/demo global_db.username = root global_db.password = root声明数据表使用自动增长类型
可以是Auto或者UUID
System.primaryKey.type = Auto
为true则插入数据后自动填充主键增长值
System.primaryKey.call = true
表名通常使用下划线命名规定,如product_item
DROP TABLE IF EXISTS
person; CREATE TABLEperson(idint(11) NOT NULL AUTO_INCREMENT,namevarchar(10) DEFAULT NULL,ageint(11) DEFAULT NULL,sexchar(1) DEFAULT NULL,incomedouble DEFAULT NULL,phonevarchar(20) DEFAULT NULL,created_atdatetime DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Person 使用帕斯卡(pascal)命名法与表 person 绑定, 继承 ActiveRecord 实现键值映射
package app.model;import java.sql.SQLException; import javax.sql.DataSource; import java.util.Map;
import org.apache.log4j.Logger;
import org.rails.core.db.DataSourceHelper; import org.rails.core.model.ActiveRecord; import org.rails.core.model.ObjectNotFoundException;
public class Person extends ActiveRecord {
public static DataSource gloDS; static{ try { //获取db.properties设置的System.db gloDS = DataSourceHelper.getSystemDataSource(); } catch (Exception e) { Logger.getLogger("Person").error(e.getMessage(),e); } } public Person() { super(gloDS); } public Person(Object id) throws ObjectNotFoundException, SQLException { super(gloDS, id); } public Person(Map<String, Object> m) { super(gloDS, m); }}
CRUD 示例
package test;import java.sql.SQLException; import java.sql.Timestamp; import java.util.Date;
import org.rails.core.model.ObjectNotFoundException; import org.rails.core.model.attribute.AttributeException;
import app.model.Person; import junit.framework.TestCase;
public class CRUDTest extends TestCase {
public CRUDTest() { super(); } public CRUDTest(String name) { super(name); } /** * 测试插入操作 * @throws SQLException * @throws AttributeException */ public void testCreate() throws AttributeException, SQLException{ Person person = new Person(); person.put("name","刘备"); person.put("age",36); person.put("sex","M"); person.put("income",100000.89d); person.put("phone","020-13812345678"); person.put("created_at",new Timestamp(new Date().getTime())); assertEquals(true,person.create()); } /** * 测试查询操作,获取主键为 1 的对象 * @throws AttributeException * @throws SQLException * @throws ObjectNotFoundException */ public void testRead() throws AttributeException, SQLException, ObjectNotFoundException{ Person person = new Person(1); System.out.println(person); assertNotNull(person); } /** * 测试更新操作 * @throws AttributeException * @throws SQLException * @throws ObjectNotFoundException */ public void testUpdate() throws AttributeException, SQLException, ObjectNotFoundException{ Person person = new Person(1); person.put("name","刘备"); person.put("age",38); person.put("income",106000.00d); person.put("phone","010-888888888"); person.put("created_at",new Timestamp(new Date().getTime())); assertEquals(true,person.update()); } /** * 测试删除操作 * @throws ObjectNotFoundException * @throws SQLException */ public void testDelete() throws ObjectNotFoundException, SQLException{ Person person = new Person(1); assertEquals(true,person.delete()); }}
-
本文附件下载:
<li><a href="http://dl.javaeye.com/topics/download/d398e1ab-4c64-3dd3-aaf3-87a2ebb34988">crud_demo101010.rar</a> (1.3 MB)</li>
<br><br>
作者: <a href="http://jrails.javaeye.com">jrails</a>
<br>
声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
<br><br>
<span style="color:red">
<a href="http://www.javaeye.com/topic/780453" style="color:red">已有 <strong>0</strong> 人发表回复,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
JavaEye推荐