当前位置:网站首页>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);
边栏推荐
- powershell优化之一:提示符美化
- priority inversion problem
- Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
- Case - the ArrayList collection stores student objects and traverses them in three ways
- 通过命令行创建harbor镜像库
- Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
- Float type value range
- Case - the list set stores student objects and traverses them in three ways
- Bm1z002fj-evk-001 startup evaluation
- Basic operations of MySQL auto correlation query
猜你喜欢
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
How to Algorithm Evaluation Methods
Django uses redis to store sessions starting from 0
Use the browser to cut the entire page (take chrome as an example)
Install harbor (online offline)
MySQL log management and master-slave replication
Chapter 2 process management
redis
Recursion and recursion
Time display of the 12th Blue Bridge Cup
随机推荐
C language learning log 10.19
Case - traversing the directory (file class & recursive call)
Introduction to R language 4--- R language process control
C language learning log 2.19
ZABBIX wechat alarm
Case - grade sorting - TreeSet set storage
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
Comment procéder à l'évaluation des algorithmes
890. Find and Replace Pattern
MySQL advanced query
Information collection for network security (2)
MySQL built-in functions
Error: unmapped character encoding GBK
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
List collection concurrent modification exception
Recursion and recursion
Django uploads local binaries to the database filefield field
Jeffery0207 blog navigation
C language learning log 1.24
Dup2 use