当前位置:网站首页>August 15, 2021 another week

August 15, 2021 another week

2022-06-13 05:32:00 zrllllll

summary

The second week of living outside , Everything is completely adapted to , Our task will be up in a week , The basic functions have been realized . But a problem bothered me for several days ,SpringBoot Integrate SpringSecurity Oauth2 jwt , because Oauth2 It's not maintained anymore , I don't know how to configure , 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 .
Sum up the harvest of the project .
Used in this project jpa, instead of mybatis 了 , Think jpa Quite convenient . Some of the grammars I encountered , To sum up .
jpa It is operated through the mapping relationship between database tables and entity classes , You can manipulate the database by manipulating entity classes .

JPA(Java Persistence API)Java Persistence API, It's a set of bases ORM Ideological norms .

ORM(Object Relational Mapping) Object relation mapping . It realizes a mapping relationship between entity class objects and tables in the database . We can manipulate entity class objects (JavaBean) To manipulate database tables , So as to realize the CRUD Operation and no need to focus on SQL sentence .ORM It mainly involves two mapping relationships :1、 Mapping relationship between entity class and table ;2、 Mapping relationship between attributes in entity class and fields in table . image Mybatis frame : An incomplete ORM frame , Developers need to write a part of SQL sentence ;Hibernate frame : It's a complete one ORM frame , You need to write SQL sentence

standard : Just as the name suggests, it only defines but does not implement , So you can see JPA The interior of is composed of a series of interfaces and abstract classes , I didn't realize it myself .

in summary :JPA That is, many implementations ORM A specification of the ideological framework , There are only interfaces and abstract classes inside , There is no implementation class .( 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_followee") It declares that this is an entity class , Corresponding to tb_followee surface   It can also be used. @Entiy("tb_followee") Instead of 
@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;
//  Focus on people id
    @Column(name="f_fid")
    private Integer ffid;
}
// The entity class must be given an alias when querying  (:userId) Is the required parameter 
	@Query(value = "select f from Favorite f where f.favoriteUid = (:userId)" )
	List<Favorite> findAllFavorites(Integer userId);
	// When deleting or updating , Have to add @Modifying Annotation to declare the jpql Statement is an update or delete operation ,@Transactional Annotation to open a transaction for a statement ,jpa The update or deletion of must be supported by transactions, or an error will be reported .
	@Modifying
    @Transactional
    @Query(value = "delete from Collect where collectId = ?1")
    int CancelCollect(Integer collectId);

The following problem of multi table joint query also made me work for a while , Multi table associated query queries the specified fields . We need to create an entity class to store the results . Pay attention to new com.three.pojo.ForumVo(?,?,?) Be sure to add new also The order of corresponding fields shall not 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 it sql sentence , Can be in @Query() Inside plus nativeQuery=true, here from You can no longer use entity class names .

	@Query(value = "SELECT f_fid FROM tb_followee WHERE f_uid= ?",nativeQuery = true)
    List<Integer> followees(Integer userId);
原网站

版权声明
本文为[zrllllll]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280510456554.html