JFinal增删改查加分页查询

上传者: 39822647 | 上传时间: 2025-04-15 11:06:48 | 文件大小: 7.79MB | 文件类型: ZIP
**JFinal 增删改查与分页查询详解** JFinal 是一款基于 Java 的轻量级 Web 开发框架,以其高效、简洁的特性深受开发者喜爱。本篇将深入讲解如何利用 JFinal 实现数据库的增删改查(CRUD)操作,并结合分页查询,帮助你更好地理解和应用 JFinal 在实际项目中的功能。 1. **JFinal 框架简介** JFinal 采用了 MVC(Model-View-Controller)设计模式,提供了丰富的 API 和插件,使得开发过程更为便捷。它通过 AOP(面向切面编程)实现了拦截器,能够轻松处理请求、事务管理等常见任务。 2. **环境配置** 开始前,确保你已经安装了 JDK、MySQL 数据库,并且在项目中引入了 JFinal 的依赖。JFinal 使用 Maven 或 Gradle 进行管理,配置完成后,可以通过 `mvn compile` 或 `gradle build` 命令进行构建。 3. **数据库连接** JFinal 配置数据库连接主要通过 `Config` 类的 `setDevMode(true)` 方法开启开发模式,然后设置数据源。例如: ```java config.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"); config.setUsername("root"); config.setPassword("password"); ``` 4. **模型定义** 在 JFinal 中,模型类继承 `Model` 类,可以实现自动映射表。例如,定义一个 `User` 模型对应数据库中的 `user` 表: ```java public class User extends Model { public static final User me = new User(); } ``` 5. **增删改查操作** - **增加(Create)**: 使用 `save()` 方法插入新记录。 ```java User user = new User().set("name", "John").set("email", "john@example.com"); user.save(); ``` - **删除(Delete)**: 通过主键 ID 删除记录,使用 `deleteById(id)` 方法。 ```java User.me.deleteById(1); ``` - **修改(Update)**: 使用 `update()` 方法更新已有记录。 ```java User user = User.me.findById(1); user.set("email", "newemail@example.com"); user.update(); ``` - **查询(Query)**: 可以通过 `find()`、`findFirst()` 等方法进行查询。 ```java List users = User.me.findAll(); User firstUser = User.me.findFirst(); ``` 6. **分页查询** JFinal 提供了 `Page` 类来实现分页功能。我们需要计算总条数和每页条数,然后创建 `Page` 对象并执行查询。 ```java int pageNumber = 1; int pageSize = 10; int totalCount = User.me.count(); Page page = User.me.page(pageNumber, pageSize); List userList = page.getList(); ``` 这样,`page` 对象包含了当前页的数据,以及 `pageNumber`、`pageSize`、`totalCount` 等分页信息,方便在视图层渲染。 7. **控制器与视图** 在 JFinal 中,控制器类继承 `Controller`,处理 HTTP 请求。通过 `render()` 方法返回视图。例如,一个简单的用户列表页面: ```java public class UserController extends Controller { public void list() { Page page = User.me.page(getParaToInt("page"), 10); setAttr("page", page); render("list.html"); } } ``` 视图文件(如 `list.html`)通常使用模板引擎如 Beanie 或 FreeMarker 来生成 HTML。 8. **总结** JFinal 的简单易用性体现在其对 CRUD 操作的直接支持和对分页的便捷处理。结合注释良好的代码,你可以快速掌握 JFinal 的基本用法,并以此为基础开发更复杂的 Web 应用。通过实践和理解,你会发现 JFinal 是一个强大的工具,能够提升开发效率,同时降低了项目的维护成本。

文件下载

资源详情

[{"title":"( 75 个子文件 7.79MB ) JFinal增删改查加分页查询","children":[{"title":"jfinal","children":[{"title":"src","children":[{"title":"main","children":[{"title":"webapp","children":[{"title":"WEB-INF","children":[{"title":"lib","children":null,"spread":false},{"title":"web.xml <span style='color:#111;'> 669B </span>","children":null,"spread":false}],"spread":true},{"title":"view","children":[{"title":"editUser.jsp <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"listUser.jsp <span style='color:#111;'> 1.15KB </span>","children":null,"spread":false},{"title":"addUser.jsp <span style='color:#111;'> 1.28KB </span>","children":null,"spread":false}],"spread":true},{"title":"static","children":[{"title":"js","children":[{"title":"jquery.js <span style='color:#111;'> 90.46KB </span>","children":null,"spread":false},{"title":"bootstrap.min.js <span style='color:#111;'> 27.17KB </span>","children":null,"spread":false},{"title":"jquery-2.1.0.min.js <span style='color:#111;'> 81.66KB </span>","children":null,"spread":false}],"spread":true},{"title":"fonts","children":[{"title":"glyphicons-halflings-regular.ttf <span style='color:#111;'> 40.27KB </span>","children":null,"spread":false},{"title":"glyphicons-halflings-regular.eot <span style='color:#111;'> 19.81KB </span>","children":null,"spread":false},{"title":"glyphicons-halflings-regular.svg <span style='color:#111;'> 61.38KB </span>","children":null,"spread":false},{"title":"glyphicons-halflings-regular.woff <span style='color:#111;'> 22.75KB </span>","children":null,"spread":false}],"spread":true},{"title":"css","children":[{"title":"bootstrap.min.css <span style='color:#111;'> 97.21KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"resoures","children":[{"title":"db.properties <span style='color:#111;'> 175B </span>","children":null,"spread":false}],"spread":true},{"title":"java","children":[{"title":"com","children":[{"title":"demo","children":[{"title":"common","children":[{"title":"PropertiesConfig.java <span style='color:#111;'> 2.71KB </span>","children":null,"spread":false}],"spread":true},{"title":"model","children":[{"title":"User.java <span style='color:#111;'> 221B </span>","children":null,"spread":false}],"spread":true},{"title":"controller","children":[{"title":"UserController.java <span style='color:#111;'> 1.33KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"target","children":[{"title":"jfinal","children":[{"title":"WEB-INF","children":[{"title":"classes","children":[{"title":"com","children":[{"title":"demo","children":[{"title":"common","children":[{"title":"Config.class <span style='color:#111;'> 2.43KB </span>","children":null,"spread":false},{"title":"PropertiesConfig.class <span style='color:#111;'> 2.94KB </span>","children":null,"spread":false}],"spread":true},{"title":"model","children":[{"title":"User.class <span style='color:#111;'> 524B </span>","children":null,"spread":false},{"title":"Blog.class <span style='color:#111;'> 889B </span>","children":null,"spread":false},{"title":"base","children":[{"title":"BaseBlog.class <span style='color:#111;'> 1.87KB </span>","children":null,"spread":false}],"spread":true},{"title":"MappingKit.class <span style='color:#111;'> 714B </span>","children":null,"spread":false}],"spread":true},{"title":"controller","children":[{"title":"UserController.class <span style='color:#111;'> 1.72KB </span>","children":null,"spread":false},{"title":"StartController.class <span style='color:#111;'> 558B </span>","children":null,"spread":false},{"title":"HellowordController.class <span style='color:#111;'> 603B </span>","children":null,"spread":false},{"title":"BlogController.class <span style='color:#111;'> 382B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"db.properties <span style='color:#111;'> 175B </span>","children":null,"spread":false}],"spread":true},{"title":"lib","children":[{"title":"jetty-server-8.1.8.jar <span style='color:#111;'> 116.54KB </span>","children":null,"spread":false},{"title":"jstl-1.2.jar <span style='color:#111;'> 404.53KB </span>","children":null,"spread":false},{"title":"mysql-connector-java-5.1.6.jar <span style='color:#111;'> 686.78KB </span>","children":null,"spread":false},{"title":"jetty-servlet-8.1.8.v20121106.jar <span style='color:#111;'> 94.95KB </span>","children":null,"spread":false},{"title":"jetty-io-8.1.8.v20121106.jar <span style='color:#111;'> 100.87KB </span>","children":null,"spread":false},{"title":"jetty-security-8.1.8.v20121106.jar <span style='color:#111;'> 87.75KB </span>","children":null,"spread":false},{"title":"cglib-nodep-3.2.5.jar <span style='color:#111;'> 344.66KB </span>","children":null,"spread":false},{"title":"javax.servlet-3.0.0.v201112011016.jar <span style='color:#111;'> 195.69KB </span>","children":null,"spread":false},{"title":"jfinal-3.3.jar <span style='color:#111;'> 631.02KB </span>","children":null,"spread":false},{"title":"jetty-util-8.1.8.v20121106.jar <span style='color:#111;'> 273.95KB </span>","children":null,"spread":false},{"title":"c3p0-0.9.1.2.jar <span style='color:#111;'> 596.47KB </span>","children":null,"spread":false},{"title":"jetty-xml-8.1.8.v20121106.jar <span style='color:#111;'> 38.20KB </span>","children":null,"spread":false},{"title":"jetty-continuation-8.1.8.v20121106.jar <span style='color:#111;'> 20.64KB </span>","children":null,"spread":false},{"title":"jetty-server-8.1.8.v20121106.jar <span style='color:#111;'> 331.04KB </span>","children":null,"spread":false},{"title":"jetty-http-8.1.8.v20121106.jar <span style='color:#111;'> 92.27KB </span>","children":null,"spread":false}],"spread":false},{"title":"web.xml <span style='color:#111;'> 669B </span>","children":null,"spread":false}],"spread":true},{"title":"view","children":[{"title":"editUser.jsp <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"listUser.jsp <span style='color:#111;'> 1.15KB </span>","children":null,"spread":false},{"title":"addUser.jsp <span style='color:#111;'> 1.28KB </span>","children":null,"spread":false}],"spread":true},{"title":"META-INF","children":[{"title":"MANIFEST.MF <span style='color:#111;'> 98B </span>","children":null,"spread":false}],"spread":true},{"title":"static","children":[{"title":"js","children":[{"title":"jquery.js <span style='color:#111;'> 90.46KB </span>","children":null,"spread":false},{"title":"bootstrap.min.js <span style='color:#111;'> 27.17KB </span>","children":null,"spread":false},{"title":"jquery-2.1.0.min.js <span style='color:#111;'> 81.66KB </span>","children":null,"spread":false}],"spread":true},{"title":"fonts","children":[{"title":"glyphicons-halflings-regular.ttf <span style='color:#111;'> 40.27KB </span>","children":null,"spread":false},{"title":"glyphicons-halflings-regular.eot <span style='color:#111;'> 19.81KB </span>","children":null,"spread":false},{"title":"glyphicons-halflings-regular.svg <span style='color:#111;'> 61.38KB </span>","children":null,"spread":false},{"title":"glyphicons-halflings-regular.woff <span style='color:#111;'> 22.75KB </span>","children":null,"spread":false}],"spread":true},{"title":"css","children":[{"title":"bootstrap.min.css <span style='color:#111;'> 97.21KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"generated-sources","children":[{"title":"annotations","children":null,"spread":false}],"spread":true},{"title":"classes","children":[{"title":"com","children":[{"title":"demo","children":[{"title":"common","children":[{"title":"Config.class <span style='color:#111;'> 2.43KB </span>","children":null,"spread":false},{"title":"PropertiesConfig.class <span style='color:#111;'> 2.94KB </span>","children":null,"spread":false}],"spread":true},{"title":"model","children":[{"title":"User.class <span style='color:#111;'> 524B </span>","children":null,"spread":false},{"title":"Blog.class <span style='color:#111;'> 889B </span>","children":null,"spread":false},{"title":"base","children":[{"title":"BaseBlog.class <span style='color:#111;'> 1.87KB </span>","children":null,"spread":false}],"spread":true},{"title":"MappingKit.class <span style='color:#111;'> 714B </span>","children":null,"spread":false}],"spread":true},{"title":"controller","children":[{"title":"UserController.class <span style='color:#111;'> 1.72KB </span>","children":null,"spread":false},{"title":"StartController.class <span style='color:#111;'> 558B </span>","children":null,"spread":false},{"title":"HellowordController.class <span style='color:#111;'> 603B </span>","children":null,"spread":false},{"title":"BlogController.class <span style='color:#111;'> 382B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"db.properties <span style='color:#111;'> 175B </span>","children":null,"spread":false}],"spread":true},{"title":"jfinal.war <span style='color:#111;'> 3.80MB </span>","children":null,"spread":false}],"spread":true},{"title":".idea","children":[{"title":"libraries","children":[{"title":"javax_servlet_jsp_jstl_1_2_0_v201105211821.xml <span style='color:#111;'> 306B </span>","children":null,"spread":false}],"spread":true},{"title":"misc.xml <span style='color:#111;'> 640B </span>","children":null,"spread":false},{"title":"compiler.xml <span style='color:#111;'> 527B </span>","children":null,"spread":false},{"title":"uiDesigner.xml <span style='color:#111;'> 8.59KB </span>","children":null,"spread":false},{"title":"workspace.xml <span style='color:#111;'> 36.94KB </span>","children":null,"spread":false},{"title":"encodings.xml <span style='color:#111;'> 172B </span>","children":null,"spread":false}],"spread":true},{"title":"jfinal.iml <span style='color:#111;'> 155B </span>","children":null,"spread":false},{"title":"pom.xml <span style='color:#111;'> 3.05KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明