2018-01-26 21:42
@昵称而已
好多个BaseModel ???
你指的是那种 get和set的 BaseXxxModel 吗?
这个简单啊, 让哪些BaseXxxModel 去继承你通用公共的BaseModel 就可以了。
比如我的:
public abstract class BaseAccount> extends BaseModel implements IBean {
setXxx
getXxx
}
2018-01-25 23:20
@215115704
比较简单的 递归调用函数体:
#define showMenu(me){
...html...
#@showMenu(me.menus)
...html...
}
如果你的函数体比较复杂 , 可以建一个java , 使用下面方式去调用,
通过使用addSharedObject方法,将某个具体对象添加为共享对象,可以全局进行使用,以下是代码示例:
- public void configEngine(Engine me) {
- me.addSharedObject("RESOURCE_HOST", "http://res.jfinal.com");
- me.addSharedObject("sk", new com.jfinal.kit.StrKit());
- }
以上代码中的第二行,添加了一个名为RESOURCE_HOST的共享对象,而第三行代码添加了一个名为sk的共享对象,以下是在模板中的使用例子:
- <img src="#(RESOURCE_HOST)/img/girl.jpg" />
- #if(sk.isBlank(title))
- ...
- #end
以上代码第一行中使用输出指令输出了RESOUCE_HOST这个共享变量,对于大型web应用系统,通过这种方式可以很方便地规划资源文件所在的服务器。以上第二行代码调用了名为sk这个共享变量的isBlank方法,使用方式符合开发者直觉。
注意:由于对象被全局共享,所以需要注意线程安全问题,尽量只共享常量以及无状态对象。
2018-01-25 22:35
JFinal ActiveRecord 天然支持表关联操作,并不需要学习新的东西,此为无招胜有招。表关联操作主要有两种方式:一是直接使用sql得到关联数据;二是在Model中添加获取关联数据的方法。
假定现有两张数据库表:user、blog,并且user到blog是一对多关系,blog表中使用user_id关联到user表。如下代码演示使用第一种方式得到user_name:
- public void relation() {
- String sql = "select b.*, u.user_name from blog b inner join user u on b.user_id=u.id where b.id=?";
- Blog blog = Blog.dao.findFirst(sql, 123);
- String name = blog.getStr("user_name");
- }
以下代码演示第二种方式在Blog中获取相关联的User以及在User中获取相关联的Blog:
- public class Blog extends Model<Blog>{
- public static final Blog dao = new Blog();
- public User getUser() {
- return User.dao.findById(get("user_id"));
- }
- }
- public class User extends Model<User>{
- public static final User dao = new User();
- public List<Blog> getBlogs() {
- return Blog.dao.find("select * from blog where user_id=?", get("id"));
- }
- }
2018-01-23 22:32
Db.tx() 源码搬过来,我们一起来瞅瞅
/**
* Execute transaction.
* @param config the Config object
* @param transactionLevel the transaction level
* @param atom the atom operation
* @return true if transaction executing succeed otherwise false
*/
boolean tx(Config config, int transactionLevel, IAtom atom) {
Connection conn = config.getThreadLocalConnection();
if (conn != null) { // Nested transaction support
try {
if (conn.getTransactionIsolation() < transactionLevel)
conn.setTransactionIsolation(transactionLevel);
boolean result = atom.run();
if (result)
return true;
throw new NestedTransactionHelpException("Notice the outer transaction that the nested transaction return false"); // important:can not return false
}
catch (SQLException e) {
throw new ActiveRecordException(e);
}
}
Boolean autoCommit = null;
try {
conn = config.getConnection();
autoCommit = conn.getAutoCommit();
config.setThreadLocalConnection(conn);
conn.setTransactionIsolation(transactionLevel);
conn.setAutoCommit(false);
boolean result = atom.run();
if (result)
conn.commit();
else
conn.rollback();
return result;
} catch (NestedTransactionHelpException e) {
if (conn != null) try {conn.rollback();} catch (Exception e1) {LogKit.error(e1.getMessage(), e1);}
LogKit.logNothing(e);
return false;
} catch (Throwable t) {
if (conn != null) try {conn.rollback();} catch (Exception e1) {LogKit.error(e1.getMessage(), e1);}
throw t instanceof RuntimeException ? (RuntimeException)t : new ActiveRecordException(t);
} finally {
try {
if (conn != null) {
if (autoCommit != null)
conn.setAutoCommit(autoCommit);
conn.close();
}
} catch (Throwable t) {
LogKit.error(t.getMessage(), t); // can not throw exception here, otherwise the more important exception in previous catch block can not be thrown
} finally {
config.removeThreadLocalConnection(); // prevent memory leak
}
}
}