欢迎来到山村网

JFinal如何配置springPlug?

2019-03-02 13:24:42浏览:314 来源:山村网   
核心摘要:jfinal 是 orm+mvc 而且有易与扩展的render plugin等机制。JFinal框架也整合了Spring框架,下面实现JFinal怎么去配置Spr

jfinal 是 orm+mvc 而且有易与扩展的render plugin等机制。

JFinal框架也整合了Spring框架,下面实现JFinal怎么去配置Spring框架。在JFinal中整合Spring使用到的类是SpringPlugin和IocInterceptor类。

Eclipse IDE for Java EE Developers 中

1、创建 Dynamic Web Project

2、修改 Default Output Folder,推荐输入 WebRootWEB-INFclasses

JFinal如何配置springPlug? 山村

特别注意:此处的 Default out folder 必须要与 WebRootWEB-INFclasses 目录
完全一致才可以使用 JFinal 集成的 Jetty 来启动项目。

3、修改 Content directory,推荐输入 WebRoot

注 意 : 此 处 也 可 以 使 用 默 认 值 WebContent , 但 上 一 步 中 的
WebRootWEB-INFclasses 则需要改成 WebContentWEB-INFclasses 才能对应上。

4、去官网下载最新的jar包(我这是JFinal-lib-1.9)

把jetty-server-8.1.8.jar 和JFinal-bin-1.4.jar放到项目 WEB-INFlib下,jetty-server-8.1.8.jar是开发时使用的运行环境,用tomact和生产环境下就不需要了

5、添加到web.xml

<filter><filter-name>jfinal</filter-name><filter-class>com.jfinal.core.JFinalFilter</filter-class><init-param><param-name>configClass</param-name><param-value>demo.DemoConfig</param-value></init-param></filter><filter-mapping><filter-name>jfinal</filter-name><url-pattern>publicbooleanlogin(Stringusername,Stringpassword);}
packagecom.tenghu.core.service.impl;importcom.tenghu.core.service.LoginService;publicclassLoginServiceImplimplementsLoginService{publicbooleanlogin(Stringusername,Stringpassword){if("admin".equals(username)&&"admin".equals(password)){returntrue;}returnfalse;}}

Spring配置文件:

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
<spanstyle="white-space:pre">default-autowire="byName"</span>><beanid="loginService"class="com.tenghu.core.service.impl.LoginServiceImpl"/></beans>

配置完成

JFinal Dao 集成到 Spring

最近公司其它部门的同事还有朋友都表示对jfinal有很大的兴趣,我发现最主要的一点是jfianl极简风格和牛x的开发效率让大家为之兴奋,尤其是jfinal的dao设计。至于没有在新项目中进行尝试,因为好多项目需要对事务尤其是多库事务上进行处理,而这点也让大家犯难了起来。公司目前的项目是基于springmvc+mybatis,所以我将jfinal dao 集成到spring上,利用spring 强大的事务抽象能力解决事务难题。

不说了,先上代码。。

?

1
2
3
4
5
6
7
8
9
10
11
12 <beanid="jFinalDaoConfig"class="com.jfinal.plugin.activerecord.JFinalDaoConfig"init-method="init">
<propertyname="configName"value="main"/>
<propertyname="dataSource"ref="dataSource"/>
<propertyname="dialect">
<beanclass="com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect"/>
</property>
<propertyname="modelsClasses">
<set>
<value>test.AAA</value>
</set>
</property>
</bean>

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 publicclassJFinalDaoConfig{
protectedfinalLoggerlog=Logger.getLogger(getClass());
publicvoidinit(){
if(null==dialect){
log.warn("Usingmysqldialectasdefault.");
dialect=newMysqlDialect();//默认mysql方言
}
//config与dataSource相关绑定
Configconfig=newConfig(configName,dataSource,dialect);
DbKit.addConfig(config);
Iterator<Class<Model>>iterModel=modelsClasses.iterator();
ClassmodelClass=null;
while(iterModel.hasNext()){
modelClass=iterModel.next();
ClasssuperClass=modelClass.getSuperclass();
if(null==superClass||superClass!=Model.class){
log.warn(modelClass+"shouldextendscom.jfinal.plugin.activerecord.Model");
continue;
}
DbKit.addModelToConfigMapping(modelClass,config);//model与config绑定
TableBindingtb=(TableBinding)modelClass.getAnnotation(TableBinding.class);//获取model对应的表信息
&nbnbsp;if(tb!=null){
Tabletable=null;
if(StrKit.notBlank(tb.pkName())){
table=newTable(tb.tableName(),tb.pkName(),modelClass);
}else{
table=newTable(tb.tableName(),modelClass);
}
tableList.add(table);
}
}
if(!tableList.isEmpty()){
TableBuilder.build(tableList,config);
}
Db.init();
}
privateList<Table>tableList=newArrayList<Table>();
privateStringconfigName;
privateDataSourcedataSource;
privateDialectdialect;
privateSet<Class<Model>>modelsClasses;
publicvoidsetConfigName(StringconfigName){
if(configName==null){
thrownewIllegalArgumentException("Confignamecannotbenull");
}
this.configName=configName;
}
publicvoidsetDataSource(DataSourcedataSource){
if(dataSource==null){
thrownewIllegalArgumentException("DataSourcecannotbenull");
}
this.dataSource=dataSource;
}
publicvoidsetDialect(Dialectdialect){
this.dialect=dialect;
}
publicvoidsetModelsClasses(Set<Class<Model>>modelsClasses){
this.modelsClasses=modelsClasses;
}
}

JFinalDaoConfig的作用就是将config与数据库绑定,模型与config进行绑定,这个类的作用我相信大家如果对jfinal比较熟悉,应该不难理解。

jfianl Model、DbPro 的获取和释放连接采用了spring的DataSourceUtils进行替换

//conn = config.getConnection();
conn = DataSourceUtils.getConnection(config.getDataSource());

JdbcUtils.closeStatement(pst);
DataSourceUtils.releaseConnection(conn, config.getDataSource());

由于jfianl某些类的可见性,JFinalDaoConfig需要放到com.jfinal.plugin.activerecord下

这样就可以利用spring的事务和jfinal dao的方便性了。

(责任编辑:豆豆)
下一篇:

串口显示乱码的原因有哪些?

上一篇:

python处理大数字的方法

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com