public class ActiveRecordTest {
public static void main(String[] args) {
DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost/jfinal", "root", "");
ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
//arp.addMapping("blog", Blog.class);
// 与web环境唯一的不同是要手动调用一次相关插件的start()方法
dp.start();
arp.start();
// 通过上面简单的几行代码,即可立即开始使用
new Blog().set("title", "title").set("content", "cxt text").save();
Blog.me.findById(123);
}
}
事务测试
boolean success = Db.tx(new IAtom() {
public boolean run() throws SQLException {
int count2 = Db.update("update account set cash = cash + ? where id = ?", 100, 2);
int count1 = Db.update("update account set cash = cash - ? where id = ?", 100, 1);
return count1==1&&count2==1;
}
});
System.out.println(success);
注解配置
@Before(Tx.class)
public void txtxtx(){
int count2 = Db.update("update account set cash = cash + ? where id = ?", 100, 2);
int count1 = Db.update("update account set cash = cash - ? where id = ?", 100, 1);
}
在主函数中调用
ActiveRecordTest activeRecordTest = Enhancer.enhance(ActiveRecordTest.class);
activeRecordTest.txtxtx();
必须使用Enhancer.enhance或Duang.duang,否则注解是无效的