Home Java javaTutorial How to write the javaweb business layer

How to write the javaweb business layer

May 31, 2019 pm 01:11 PM
java

Business layer

Service layer: Reference the corresponding Dao database operation, where you can write the code you need (such as simple judgment).

How to write the javaweb business layer

The service layer calls various DAO business operations. For example, if you have a business that is added and then modified. Then you call the add and modify operations of dao respectively, including some data conversion and logical judgment in the service layer, and dao is just adding, deleting, modifying and checking. And transactions are generally placed in the service layer.
The Service layer and the DAO layer may both operate on the database, and their specific differences are:

dao and service correspond to

Generally, Hibernate DAO only operates one POJO object, so one DAO corresponds to one POJO object. The Service layer is for transaction management (declarative transaction management) when processing multiple POJO objects (that is, data operations on multiple tables). The Service layer (the implementation class of its interface) is injected with multiple DAO objects to complete its data operations.

The presence or absence of Service
My opinion on this point may not be correct. There are two models in my mind for building the business layer:
Mode 1 is Service DAO, that is, DAO only performs CRUD and similar simple operations (called function points and does not include business logic). Service is combined into business logic by calling one or more function points in DAO. Service The quantity should be determined by the functional module.
In this model, the business logic is placed in the Service, and the boundaries of the transaction should also be controlled in the Service. Of course, controlling transactions directly in the Service will introduce non-business logic code. Fortunately, Spring's AOP can solve this Problem, this is also one of the reasons for the introduction of Spring.
If we talk about the shortcomings, it is that the operations on some objects are simple CRUD, and the Service layer seems cumbersome.

Mode 2 is Service BO, and BO = DAO business method, add business methods on the basis of the original DAO to form a BO object. It should be noted that business methods in BO are often targeted at one entity object. If they need to span multiple entity objects, the method should be placed in Service.

For example,

A simple bank account management system creates an account BO object, which can contain password changes. , withdraw money and other business methods (it is not difficult to see that these methods only operate on a single account object). Now we need to add a transfer method, which should be placed in Service.

What is the relationship between Service and BO here? Another example:

Take the national administrative agencies as an example: the Grain Bureau is responsible for collecting grain, selling seeds, etc., and the Ministry of Construction is responsible for approving land sales, building roads, etc. These are all matters within the administrative department. Son. Suddenly there is a flood in a certain place. During the disaster relief, the Grain Bureau needs to open a warehouse to release grain, and the Ministry of Construction builds temporary houses. How to coordinate the two departments? It is necessary to establish a special disaster relief committee, which will allocate resources from the two parts. The two parts here are BO, and the disaster relief committee is Service. I don’t know if I expressed my meaning accurately, haha. Mode 1 has clear boundaries when dividing Service and DAO, but it will bring some unnecessary code. The partitioning of Mode 2 is relatively complex, but it can improve coding efficiency.

Of course, in small-scale applications, it is acceptable to use DAO or BO without Service.

Whether there are interfaces between Service and DAO

The interface is a contract, which can have multiple implementations. Therefore, the presence or absence of interfaces depends on whether the specific implementation needs to be diversified. If it is determined that there is only one implementation of a DAO or a Service, then there is little meaning in abstracting the interface. However, some large applications may require multiple implementations of DAO and Service (such as the account DAO in the example above, which may require a Hibernate implementation, a CMP implementation and a JDO implementation). In order to hide the specific implementation class from the upper level, you need Use interfaces.

Hide the creation process of specific implementation classes. There are two methods: one is the practical factory method, which comes at the cost of a large amount of code (one factory for each DAO and Service). The second is to use Spring's IoC to implement dependency injection without writing additional code. This is also the second reason for introducing Spring.

The above is the detailed content of How to write the javaweb business layer. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles