官方说明:
JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。
JFinal有如下主要特点:
MVC架构,设计精巧,使用简单
遵循COC原则,零配置,无xml
独创Db + Record模式,灵活便利
ActiveRecord支持,使数据库开发极致快速
自动加载修改后的java文件,开发过程中无需重启web server
AOP支持,拦截器配置灵活,功能强大
Plugin体系结构,扩展性强
多视图支持,支持FreeMarker、JSP、Velocity
强大的Validator后端校验功能
功能齐全,拥有struts2的绝大部分功能
体积小仅248K,且无第三方依赖
开始进行JFinal集合DB示例
本机环境:Eclipse 4.4、Maven 3.0.5、Mysql 5.1、JDK 1.6、Tomcat 6.0
1、新建web项目:test
如图,Project name填写:test,Target runtime选择Apache Tomcat v6.0(此中间件为我本地Eclipse事先配置好的),web module version选择2.5,点击下一步:
3、默认,点击下一步:
默认,点击Finish:
如图,已创建成功web项目:
因需要结合Maven,将该web项目转换为Maven项目,右键项目,选择如下:
配置Maven pom.xml,如下:
配置JDK编译版本为1.6:
Java Compiler设置为1.6:
配置pom.xml,添加jfinal、mysql、c3p0、servlet相关依赖jar坐标,如下:
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> </dependencies>
修改编译输出目录为“WebContent/WEB-INF/classes”,修改jdk版本为1.6和编译采用的编码格式为utf-8:
<build> <sourceDirectory>src</sourceDirectory> <outputDirectory>WebContent/WEB-INF/classes</outputDirectory> <plugins> <!-- 打war包插件设置web目录为eclipse默认目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <!-- 设置WebContent目录为Web目录 --> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> <!-- 编译插件使用的jdk版本和编译采用的编码格式(默认为GBK) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build>
web.xml只设置welcom-file-list为index.jsp:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
2、创建包路径,包含action、conf、entity,如下:
创建Controller:TestController.java,继承Controller,添加方法index,如下:
package com.test.action; import java.util.List; import com.jfinal.core.Controller; import com.test.entity.Test; public class TestController extends Controller { public void index() { List<Test> list = Test.dao.find("select * from test"); this.renderJson(list); } }
创建数据库对应实体:Test.java,继承Model,如下:
package com.test.entity; import com.jfinal.plugin.activerecord.Model; /** * Generated by JFinal. */ @SuppressWarnings("serial") public class Test extends Model<Test> { public static final Test dao = new Test(); }
创建JFinal配置类:CommonConfig.java,如下:
package com.test.conf; import com.jfinal.config.Constants; import com.jfinal.config.Handlers; import com.jfinal.config.Interceptors; import com.jfinal.config.JFinalConfig; import com.jfinal.config.Plugins; import com.jfinal.config.Routes; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.c3p0.C3p0Plugin; import com.jfinal.render.ViewType; import com.test.action.TestController; import com.test.entity.Test; public class CommonConfig extends JFinalConfig { @Override public void configConstant(Constants constants) { // 设定为开发者模式 constants.setDevMode(false); constants.setViewType(ViewType.JSP); } @Override public void configHandler(Handlers handlers) { } @Override public void configInterceptor(Interceptors interceptors) { } @Override public void configPlugin(Plugins plugins) { C3p0Plugin cp = new C3p0Plugin("jdbc:mysql://localhost:3306/mydb", "root", "root"); plugins.add(cp); ActiveRecordPlugin arp = new ActiveRecordPlugin(cp); plugins.add(arp); arp.setDialect(new MysqlDialect()); arp.addMapping("test", "id", Test.class); } @Override public void configRoute(Routes routes) { routes.add("/", TestController.class); } }
web.xml添加JFinal过滤器并加载自定义JFinal配置类,如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>test</display-name> <filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>com.test.conf.CommonConfig</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
创建index.jsp,添加form表单请求TestController,如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>test</title> </head> <body> <form id="form1" method="post" name="form1"> </form> </body> <script type="text/javascript"> function submit() { document.getElementById("form1").action = "test/index?t=" + new Date().getTime(); document.getElementById("form1").submit(); } submit(); </script> </html>
浏览器访问:http://127.0.0.1/test,如下:
综上,JFinal结合mysql的示例到此已完成,此示例仅为JFinal简单示例,不能作为典型示例,仅仅作为入门的参考,还有待大家进行补充完善。