长沙专业网站优化定制,一级消防工程师考试难吗,手工制作书签,wordpress建站 产品详情页JFinal 是基于 Java 语言的极速 WEB ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。 官方文档#xff1a;http://www.jfinal.com/download?filejfinal-1.8-manual.pdf 官网#xff1a;http://www.jfinal.com/man
1.创建工…JFinal 是基于 Java 语言的极速 WEB ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。 官方文档http://www.jfinal.com/download?filejfinal-1.8-manual.pdf 官网http://www.jfinal.com/man
1.创建工程、准备Jar包 创建一个Maven项目然后Pom中导入JFinal等Jar包。 Pom.xml dependenciesdependencygroupIdcom.jfinal/groupIdartifactIdjfinal/artifactIdversion1.9/version/dependencydependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.0.1/versionscopeprovided/scopeoptionaltrue/optional/dependencydependencygroupIdfreemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.9/version/dependency!-- EHCACHE begin --dependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache/artifactIdversion2.8.1/version/dependencydependencygroupIdc3p0/groupIdartifactIdc3p0/artifactIdversion0.9.1.2/version/dependency!-- Logging with SLF4J LogBack --dependencygroupIdorg.slf4j/groupIdartifactIdslf4j-api/artifactIdversion1.7.5/versionscopecompile/scope/dependencydependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.0.13/versionscoperuntime/scope/dependency!-- mysql --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.34/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion3.8.1/versionscopetest/scope/dependency/dependencies
2.创建JFinal核心配置类 JFinal属于配置极少基本属于无XML零配置的框架它的所有配置都在一个继承JFinalConfig的核心配置类中。 Config.java
public class Config extends JFinalConfig {Overridepublic void configConstant(Constants me) {}//配置 JFinal 常量值Overridepublic void configHandler(Handlers me) {}Overridepublic void configInterceptor(Interceptors me) {}Overridepublic void configPlugin(Plugins me) {}Override public void configRoute(Routes me) {}//来配置 JFinal 访问路由
}3.修改Web.xml 将如下内容添加至 web.xml
filterfilter-namejfinal/filter-namefilter-classcom.jfinal.core.JFinalFilter/filter-classinit-paramparam-nameconfigClass/param-nameparam-valuecom.langmy.music.config.Config/param-value/init-param/filterfilter-mappingfilter-namejfinal/filter-nameurl-pattern/*/url-pattern
/filter-mapping
4.创建Controller访问第一个页面
(1) 创建IndexController
public class IndexController extends Controller{public void index(){renderFreeMarker(/index.html);//FreeMarker是模板引擎,访问普通的Html页面也可以。}
}
Controller需要继承JFinal的Controller类。
(2) 加入路由 在Config中加入IndexController的路由
public void config(Routes me) {me.add(/, IndexController.class);
}
路由规则如下 controllerKey指的是是config中配置的”/”然后在默认情况下会访问Controller会访问index方法。
在保证Webapp下有index.html的情况下打开浏览器打入http://localhost:8080/[项目名称]/ 就能进入index.html页面.
(3) ActionKey JFinal 在以上路由规则之外还提供了 ActionKey 注解可以打破原有规则。 在IndexController加入两个方法然后IndexController变成如下
public class IndexController extends Controller{public void index(){renderFreeMarker(/index.html);}public void showText(){renderText(Show Text);}ActionKey(actionKey)public void testActionKey(){renderText(Test ActionKey);}
}
打开浏览器分别访问http://localhost:8080/[项目名称]/showText和http://localhost:8080/[项目名称]/actionKey可以看到页面如下
5.路由拆分 JFinal 路由还可以进行拆分配置这对大规模团队开发特别有用以下是代码示例
public class FrontRoutes extends Routes {public void config() {add(/, IndexController.class);}
}
public class AdminRoutes extends Routes{Overridepublic void config() {// TODO 写后台的路由}
}
public class Config extends JFinalConfig {.....Overridepublic void configRoute(Routes me) {me.add(new FrontRoutes());me.add(new AdminRoutes());}
}
如上三段代码 FrontRoutes 类中配置了系统前端路由 AdminRoutes 配置了系统后端路由 Config.configRoute(…)方法将拆分后的这两个路由合并起来。使用这种拆分配置不仅 可以让 Config文件更简洁而且有利于大规模团队开发避免多人同时修改Config版本冲突。
6.数据库操作
(1) 新建数据库 新建mysql数据库testJFinal,新建一个表user(id,user_acc,name)。
(2) 新建Model类
public class User extends ModelUser{public static final User dao new User();
}
(3) 加入数据库操作插件与User与表user映射
public class Config extends JFinalConfig {Overridepublic void configPlugin(Plugins me) {//C3p0数据源插件C3p0Plugin cp new C3p0Plugin(jdbc:mysql://localhost/testJFinal,root, 19930815);me.add(cp);// ActiveRecord插件ActiveRecordPlugin arp new ActiveRecordPlugin(cp);me.add(arp);arp.addMapping(user, User.class);}
}
ActiveRecord 是 JFinal 最核心的组成部分之一通过 ActiveRecord 来操作数据库将极大地减少代码量极大地提升开发效率。ActiveRecord 是作为 JFinal 的 Plugin 而存在的所以使用时需要在 JFinalConfig 中配置ActiveRecordPlugin。
(4) 数据库操作 在IndexController中加入方法testDB。
public class IndexController extends Controller{....public void testDB() {ListUser users User.dao.find(select * from user);
// ListUser users User.dao.find(select * from user where id?,2);//分页查询
// PageUser users User.dao.paginate(1, 10, select *,from user where id?,2);for(User user :users.getList()) System.out.println(user.toString()user.getStr(user_acc));}//新增一条User数据/*User user new User();user.set(user_acc, accNew);user.set(name, nameNew);boolean flag user.save();System.out.println(flag);*/setAttr(user, users);renderFreeMarker(/test.html);}
}
以下是官方文档中对于数据库操作的一些示例
// 创建name属性为James,age属性为25的User对象并添加到数据库
new User().set(name, James).set(age, 25).save();
// 删除id值为25的User
User.dao.deleteById(25);
// 查询id值为25的User将其name属性改为James并更新到数据库
User.dao.findById(25).set(name, James).update();
// 查询id值为25的user, 且仅仅取name与age两个字段的值
User user User.dao.findById(25, name, age);
// 获取user的name属性
String userName user.getStr(name);
// 获取user的age属性
Integer userAge user.getInt(age);
// 查询所有年龄大于18岁的user
ListUser users User.dao.find(select * from user where age18);
// 分页查询年龄大于18的user,当前页号为1,每页10个user
PageUser userPage User.dao.paginate(1, 10, select *, from user
where age ?, 18);
7.ehcache缓存加入
(1) 加入EhCachePlugin EhCachePlugin 是 JFinal 集成的缓存插件通过使用 EhCachePlugin 可以提高系统的并发访问速度。 在Config的configPlugin中加入 me.add(new EhCachePlugin());
(2) 配置文件ehcache.xml
?xml version1.0 encodingUTF-8?
ehcache updateCheckfalse namedefaultCachediskStore pathjava.io.tmpdir/music/ehcache/default /!-- DefaultCache setting. --defaultCache maxEntriesLocalHeap100 eternalfalse timeToIdleSeconds300 timeToLiveSeconds600overflowToDisktrue maxEntriesLocalDisk100000 maxElementsInMemory500 / cache nameuserList maxElementsInMemory150 eternalfalse timeToLiveSeconds3600timeToIdleSeconds360 overflowToDisktrue/
/ehcache
ehcache中各种属性的配置请参考下一篇Blog 《ehcache 简单配置》。
(2) 缓存注解加入 CacheInterceptor 可以将 action 所需数据全部缓存起来 下次请求到来时如果 cache 存在则 直接使用数据并 render而不会去调用 action。此用法可使 action 完全不受 cache 相关代码所 污染即插即用以下是示例代码 EvictInterceptor 可以根据 CacheName 注解自动清除缓存。 在相应需要加入缓存的方法上加入
Before(CacheInterceptor.class)
CacheName(userList)
需要清除缓存的方法上加入
Before(EvictInterceptor.class)
CacheName(userList)
我在testDB中注释了save方法只剩下find方法加入缓存注解。 在浏览器第一次访问这个方法的时候会调用方法加入缓存第二次就不会再进方法。
8.LogBack日志加入
(1) 加入配置文件logback.xml
?xml version1.0 encodingUTF-8?
!-- configuration file for LogBack (slf4J implementation)
See here for more details: http://gordondickens.com/wordpress/2013/03/27/sawing-through-the-java-loggers/ --
configuration scantrue scanPeriod60 secondscontextListener classch.qos.logback.classic.jul.LevelChangePropagatorresetJULtrue/resetJUL/contextListener!-- To enable JMX Management --jmxConfigurator/appender nameSTDOUT classch.qos.logback.core.ConsoleAppender !-- 日志输出编码 -- EncodingUTF-8/Encoding layout classch.qos.logback.classic.PatternLayout !--格式化输出%d表示日期%thread表示线程名%-5level级别从左显示5个字符宽度%msg日志消息%n是换行符-- pattern%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n /pattern /layout /appender!-- 无用日志禁用 --root levelDEBUG appender-ref refSTDOUT / appender-ref refFILE / /root
/configuration
(2) 使用日志
//在类中加入
public static Logger LOG LoggerFactory.getLogger(IndexController.class);
//在方法中加入
if(LOG.isDebugEnabled()){LOG.debug(users.toString());}
源代码下载地址https://github.com/crazyBugLzy/JFinalLearn 文章转载自: http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn