简体   繁体   English

在JSF中使用CRUD的正确方法

[英]Correct way of using CRUD in JSF

I need advice on a few design principles regarding CRUD operations in my JSF project. 在我的JSF项目中,我需要一些有关CRUD操作的设计原则的建议。

A very simple example: 一个非常简单的例子:

I have a basic screen with a form that get submitted. 我有一个基本的屏幕,其中包含要提交的表单。 In my bean I declare a database connection in my method and a string object which I populate with my script. 在我的bean中,我在方法中声明了数据库连接,并在脚本中填充了字符串对象。 I modify the string to get the data that have been submitted in the form. 我修改字符串以获取已在表单中提交的数据。 This is the way I was taught do it, but I'm suspecting it's not based on solid principles. 这是我被教做的方法,但是我怀疑它不是基于扎实的原则。

So I decided to start using prepared statements. 因此,我决定开始使用准备好的语句。 Seems a bit better, but still not perfect in my mind. 似乎好一点,但在我看来仍然不完美。

My question is: instead of writing a new script for each CRUD method, is it better to perhaps create stored procedures instead, in my mind it looks like much neater code and perhaps has better readability. 我的问题是:与其为每个CRUD方法编写一个新脚本,不如创建存储过程是否更好,在我看来,它看起来像更整洁的代码,并且也许具有更好的可读性。

Or is there an entirely different way of doing things? 还是有一种完全不同的做事方式? The only concerns I have is a very fragile OLTP database. 我唯一关心的是一个非常脆弱的OLTP数据库。

Your JSF,s should always redirect to a servlet which calls a service method, where you write all your business logic and call your Data Access Object to execute required sql query. 您的JSF应该始终重定向到调用服务方法的servlet,在该服务方法中编写所有业务逻辑并调用数据访问对象以执行所需的sql查询。 U should never use your bean for database connection... You should use DataSource for your data base Connection. U永远不要将bean用于数据库连接...您应该将DataSource用于数据库连接。 And yes a simple preparedStatement is enough to do. 是的,一个简单的prepareStatement就足够了。 You should convert all your strings in your servlet only and then pass it to the next layer with the help of your bean which has your setters and getters for all your form fields.. And your DAo contains all the CRUD operations. 您应该只在servlet中转换所有字符串,然后在bean的帮助下将其传递到下一层,该bean具有用于所有表单字段的设置程序和获取程序。.并且DAo包含所有CRUD操作。

I don't like the idea of using stored procedures because they're hard to port and usually also hard to debug. 我不喜欢使用存储过程的想法,因为它们很难移植并且通常也很难调试。

I've been working for years with something like this 我已经用这种东西工作了多年

  1. JSF -> xhtml + @ViewScoped managed bean to accomodate the values JSF-> xhtml + @ViewScoped托管bean来容纳值
  2. Stateless EJB for transactional methods called from managed beans 从托管Bean调用的用于事务方法的无状态EJB
  3. Entity DAOs, called from EJBs, reusing basic CRUD methods with generics. 从EJB调用的实体DAO,将泛型使用了基本的CRUD方法。 I think JPA here is great, specially when they use metamodel type-safe criteria ( http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html ) 我认为JPA很棒,特别是当他们使用元模型类型安全标准( http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html )时

Nowadays, it´s been easier to work with lightweight JavaEE stacks such as apache TomEE than using prepared statements. 如今,使用轻量级JavaEE堆栈(如apache TomEE)比使用准备好的语句要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Java(Spring 框架)和内存数据库测试在 DAO 类中找到的 CRUD 方法的正确方法是什么? - What is the correct way of testing CRUD methods that are found in DAO classes using Java(Spring framework) and In-memory DB? 在Struts 2中使用嵌套域对象进行CRUD的正确方法是什么? - What's the correct way to do CRUD with nested domain objects in Struts 2? Eclipse、JSF、Java 中的 Crud 实现 - Crud implementation in Eclipse, JSF, Java JSF 1.1 Datatable CRUD操作 - JSF 1.1 Datatable CRUD operation 在JSF中从URL获取参数的正确方法是什么 - What is the correct way to get parameter from URL in JSF 绑定到昂贵的JSF属性的正确方法是什么? - What's the correct way of binding to a JSF property that is expensive? 使用 JSF、PrimeFaces 和 JPA:在不使用生成的 CRUD 类的情况下创建基本 Web 应用程序,Forms 等 - Using JSF, PrimeFaces and JPA: Create Basic WebApp without using Generated CRUD Classes, Forms, etc 正确使用线程的方式 - Correct way of using threads JSF 2.0:对于CRUD应用程序,使用DataModel <T>而不是List <T>是否有任何优势 - JSF 2.0: Is there any advantage in using DataModel<T> over List<T> for CRUD applications 使用多个ID时在JPQL中执行CRUD操作的正确方法是什么 - What is correct way to do CRUD operation in JPQL when working with multiple IDs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM