[talk through spring MVC] what kind of Java Dao layer is most suitable for our project

JOOQ framework provides a language based on generated entities. Using this language, you can create compile time safe queries. JOOQ can generate dialects for many databases. It also cleans up boilerplate code, such as closing connections.

1.  `UserRecord user = newUserRecord();`

2.  `user.setId(1);`

3.  `user.setName("Peter");`

4.  `Result<UserRecord> books1 = DSL.using(configuration)`

5.  `.selectFrom(USERS)`

6.  `.where(condition(user))`

7.  `.fetch();`

MyBatis: simple form supporting query

Object relational mapping or ORM provides another way to communicate with the database. The idea is to create a mapping between Java objects (entities) and their corresponding tables in the code. One of the representative ORM is the MyBatis framework.

MyBatis is a lightweight framework that uses queries to create mappings between entities and tables (rather than bean structures, as in JPA providers, such as Hibernate). Therefore, the framework uses queries and provides ORM features. Here, you can see a short example (no configuration file):

1.  `// Bean mapping`

2.  `publicinterfaceBlogMapper{`

3.  `@Select("SELECT * FROM blog WHERE id = #{id}")`

4.  `Blog selectBlog(int id);`

5.  `}`

6.  `// Fetching data`

7.  `BlogMapper mapper = session.getMapper(BlogMapper.class);`

8.  `Blog blog = mapper.selectBlog(101);`

Hibernate and Spring Data

Both technologies support JPA (Java persistence application programming interface). This means that both solutions can be deployed to the application server. The JPA standard requires mapping between tables / columns and Java objects (entities). For example, a user table can be mapped to the following entities:

1.  `@Data// this is not hibernate annotation, it's lombok getter/setter generator`

2.  `@Entity`

3.  `@Table(name = "USERS")`

4.  `publicclassUser{`

5.  `@Id`

6.  `@Column(name = "id")`

7.  `privateInteger id;`

8.  `@Column(name = "name")`

9.  `privateString name;`

10.  `}`

Hibernate

This is the most popular ORM framework with many built-in features. It was first released about 20 years ago. Hibernate also supports HQL language for custom SQL queries.

1.  `Session session = sessionFactory.openSession();`

2.  `User oldUser  = (User) session.get(User.class, 1); //get user`

3.  `User newUser = newUser(123,"John");`

4.  `session.save(developer); //add user`

5.  `//HQL example`

6.  `Query query = session.createQuery("FROM Users");`

7.  `List users = query.list();`

Spring Data

On top of JPA entities, Spring data provides a rich CRUD API and an expression query language. The main advantage of Spring data is that it only needs 2-3 lines to implement. The generated API is based on method naming transformations.

1.  `//Implementation, just by extending CrudRepository interface`

2.  `publicinterfaceUserRepositoryextendsCrudRepository<User, Long> {`

3.  `User findByName(String name);`

4.  `User findById(long id);`

5.  `@Query("SELECT u.ID FROM USERS u WHERE like ?1") //custom expression`

6.  `List<Integer> findByUserName(String name);`

7.  `}`

8.  `//It's how to use this repository:`

9.  `User johnUser = userRepository.findByName("John");`

10.  `User johnUser = userRepository.findById(id);`

11.  `List<Integer> usersIdsWithOVPostfix = userRepository.findByUserName("%OV%");`

summary

So far, the article has finally come to an end. To sum up, we talked about the following three parts that should be paid attention to in the process of resume making, and gave some suggestions respectively:

  1. Technical ability: first write the ability required by the position, then write the bonus ability, not the irrelevant ability;
  2. Project experience: write only STAR projects and follow the STAR rule;
  3. Resume impression: resume follows three principles: clear, brief, necessary, targeted, not overseas investment;

And the last welfare time for everyone: Resume Template + Java interview questions + popular technology series tutorial video
Stamp here to get the information in the article for free

204888/java-p7)**

[external chain picture transferring... (img-QWDhdsRp-1628143265339)]

[external chain picture transferring... (IMG hebnkoyi-1628143265341)]

[external chain picture transferring... (img-afp po5h3-1628143265342)]

Keywords: Java Back-end Interview Programmer

Added by eleven0 on Wed, 05 Jan 2022 11:26:54 +0200