summary
After living outside for the second week, everything is completely adapted. Our task will be up in another week, and the basic functions have been realized. However, a problem bothered me for several days. SpringBoot integrated SpringSecurity Oauth2 jwt, because Oauth2 is no longer maintained and I don't know how to configure it. The cases found on the Internet are not quite right, so I haven't done anything these days. I have to take this down quickly. There's not much time left.
Summarize the achievements of the project.
This project uses jpa instead of mybatis. I think jpa is very convenient. Some of the grammars I encountered are summarized.
jpa operates through the mapping relationship between database tables and entity classes. You can operate the database by operating entity classes.
JPA (Java Persistence API) is a Java Persistence API, which is a set of specifications based on ORM.
ORM (Object Relational Mapping) Object Relational Mapping. It implements a mapping relationship between entity class objects and tables in the database. We can operate database tables by operating entity class objects (JavaBeans), so as to realize the CRUD operation of the database without paying attention to SQL statements. ORM mainly involves two mapping relationships: 1. The mapping relationship between entity classes and tables; 2. Mapping relationship between attributes in entity class and fields in table. Like Mybatis framework: an incomplete ORM framework requires developers to write some SQL statements; Hibernate framework: it is a complete ORM framework and needs to write SQL statements
Specification: as the name implies, it is only defined but not implemented. Therefore, it can be understood that JPA is composed of a series of interfaces and abstract classes, which are not implemented by itself.
To sum up: JPA is one of many specifications that implement the ORM ideological framework. There are only interfaces and abstract classes, but no implementation classes. (interface oriented programming)
The mapping relationship between the entity class and the database needs to be set in the entity class
@Data @AllArgsConstructor @NoArgsConstructor @Builder //@Entiy and @ table ("tb_follow") declare that this is an entity class, corresponding to TB in the database_ The follow table can also be replaced by @ entiy ("tb_follow") @Entity @Table(name = "tb_followee") public class Followee { // id primary key @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "f_id") private Integer fid; // User id @Column(name ="f_uid") private Integer userId; // Concerned person id @Column(name="f_fid") private Integer ffid; }
//During query operation, an alias (: userId) must be given to the entity class, which is a required parameter @Query(value = "select f from Favorite f where f.favoriteUid = (:userId)" ) List<Favorite> findAllFavorites(Integer userId);
//When deleting or updating, you must add the @ Modifying annotation to declare the jpql statement as an update or deletion operation, and the @ Transactional annotation to start a transaction for the statement. The update or deletion of jpa must be supported by a transaction, otherwise an error will be reported. @Modifying @Transactional @Query(value = "delete from Collect where collectId = ?1") int CancelCollect(Integer collectId);
I've been working on the problem of multi table associated query for a long time. Multi table associated query queries the specified fields. We need to create an entity class to store the results. Notice the new. Com in the statement three. pojo. Forumvo (?,?,?) must be preceded by new, and the order of the corresponding fields cannot be disordered.
//Query the post information in the post table and the user information corresponding to the post in the user table @Query(value = "select new com.three.pojo.ForumVo(u.userId,u.userName,f.forumId,f.forumTitle,f.forumContent,f.forumVisit,f.forumTime) from Users u left join Forum f on u.userId=f.forumUid where f.forumId = ?1 order by f.forumVisit desc") List<ForumVo> favoriteForum(List<Integer> forumId);
The created entity class is
@Data @AllArgsConstructor @NoArgsConstructor public class ForumVo { private Integer userId; private String userName; private Integer forumId; private String forumTitle; private String forumContent; private Integer forumVisit; private String forumTime; }
If you want to use sql statements, you can add nativeQuery=true to @ Query(). At this time, the entity class name can no longer be used after from.
@Query(value = "SELECT f_fid FROM tb_followee WHERE f_uid= ?",nativeQuery = true) List<Integer> followees(Integer userId);