New to Java – what are JPA and Dao?

I'm new to Java and I'm trying to create a web project using a servlet

I was taught to do this:

>Create class com package. entity. User > create interface com package. dao. Userdao > create a class that implements userdao com package. dao. jpa. Jpauserdao > create EJB com using methods such as public list < user > package. service. UserService. Find all ()

I've heard that it's not necessary to create a Dao interface with JPA, but I'm completely lost. I don't know what I should do or what EJB is I just want to find all the users in my database and display their names according to java good practice

It's enough for my servlet and JSP

What would you recommend?

Solution

Dao stands for "data access object" It abstracts the concept of "getting content from data storage" You can use JDBC calls, JPA calls, or any other way to implement Dao objects Maybe it will call some remote web services Using DAO on JPA seems redundant. It does add a layer, but I think it's worth it

For example, you might have a use case of "show green eyed users"

Direct JPA:

List<User> users = entityManager.createQuery("select u  from User u where u.EyeColor = 'green'"");

With Dao, you have:

List<User> users = dao.UsersWithEyeColor("green");

Dao has several advantages here:

>It's easier to read. > It won't expose your database structure to the rest of your application > unit testing will be easier To get a course for green eyed users, just create a "mock" Dao It's easier than laughing at JPA

These are just some parameters for using DAO For a very simple small application, it may be too much overhead But for anything that gets bigger and needs to last for years, I think it's worth it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>