万物之源-> 官方文档
官网全是英文,一下是凭借个人理解以及大量时间总结的笔记,供大家参考
// <!-- 简单查询语句 -->
// @Query 代表等价的 JPQL 语句
// @Query from User u where u.userId=:userId
public List<User> findByUserId(String userId);
// @Query from User u where u.userId=:userId and u.userName=:userName
public List<User> findByUserIdAndUserName(String userId,String userName);
// @Query from User u where u.userId=:userId or u.userName=:userName
public List<User> findByUserIdOrUserName(String userId,String userName);
// @Query from User u where u.userName=:userName
public List<User> findByUserNameIs(String userName);
// @Query from User u where u.userName=:userName
public List<User> findByUserNameEquals(String userName);
// @Query from User u where u.age between :begin and :end
public List<User> findByAgeBetween(Integer begin,Integer end);
// @Query from User u where u.age < :age
public List<User> findByAgeLessThan(Integer age);
// @Query from User u where u.age <= :age
public List<User> findByAgeLessThanEqual(Integer age);
// @Query from User u where u.age > :age
public List<User> findByAgeGreaterThan(Integer age);
// @Query from User u where u.age >= :age
public List<User> findByAgeGreaterThanEqual(Integer age);
// @Query from User u where u.birthday > :date
public List<User> findByBirthdayAfter(String date);
// @Query from User u where u.birthday < :date
public List<User> findByBirthdayBefore(String date);
// @Query from User u where u.age is null
public List<User> findByAgeIsNull();
// @Query from User u where u.password like :password
// 注意 传入password需要加%password% 否则 不是模糊查询
public List<User> findByPasswordLike(String password);
// @Query from User u where u.password not like :password
public List<User> findByPasswordNotLike(String password);
// @Query from User u where u.userName like %:userName
public List<User> findByUserNameStartingWith(String userName);
// @Query from User u where u.userName like %:userName%
public List<User> findByUserNameContaining(String userName);
// @Query from User u where u.userName like :userName%
public List<User> findByUserNameEndingWith(String userName);
// @Query from User u where u.userName = :userName order by u.age desc
public List<User> findByUserNameOrderByAgeDesc(String userName);
// @Query from User u where u.age <> :age
public List<User> findByAgeNot(Integer age);
// @Query from User u where u.userName in (userName.....)
public List<User> findByUserNameIn(Collection<String> userNames);
// @Query from User u where u.userName not in (userName.....)
public List<User> findByUserNameNotIn(Collection<String> userNames);
// @Query from User u where UPPER(u.userName) = UPPER(:userName)
public List<User> findByUserNameIgnoreCase(String userName);
// <!-- 删除语句 注意 执行更新和保存调用 save()方法 -->
@Modifying
@Transactional
// @Query delete from User u where u.userName = :userName
public long deleteByUserName(String userName);
一对一,一对多,多对多demo
package jpa.entity;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column
private String userId;
@Column
private String userName;
@Column
private String password;
@Column
private Integer age;
@Column
private Date birthday;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "information_id")
private Information information;
@JsonIgnore
// CascadeType.PRESIST 级联持久化(保存)操作(持久保存拥有方实体时,也会持久保存该实体的所有相关数据。)
// CascadeType.REMOVE 级联删除操作(删除一个实体时,也会删除该实体的所有相关数据。)
// CascadeType.MERGE 级联更新(合并)操作(将分离的实体重新合并到活动的持久性上下文时,也会合并该实体的所有相关数据。)
// CascadeType.REFRESH 级联刷新操作 (只会查询获取操作)
// CascadeType.ALL 包含以上全部级联操作
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Address> addresses ;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
//@JoinTable描述了多对多关系的数据表关系。name属性指定中间表名称,joinColumns定义中间表与Teacher表的外键关系。
//中间表user_commodity的joinUserId列是Teacher表的主键列对应的外键列,inverseJoinColumns属性定义了中间表与另外一端的外键关系。
@JoinTable(name = "user_commodity",
joinColumns = { @JoinColumn(name = "joinUserId", referencedColumnName = "userId") },
inverseJoinColumns = { @JoinColumn(name = "joinCommodityId", referencedColumnName = "commodityId") })
private Set<Commodity> commodities;
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", age=" + age
+ ", birthday=" + birthday + ", information=" + information + ", addresses=" + addresses + "]";
}
public void addAddress(Address address){
address.setUser(this);
this.getAddresses().add(address);
}
public void addCommodity(Commodity commodity){
this.commodities.add(commodity);
}
}
package jpa.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "information")
public class Information {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column
private String informationId;
@Column
private String phone;
@Column
private String idCard;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@Override
public String toString() {
return "Information [informationId=" + informationId + ", phone=" + phone + ", idCard=" + idCard + "]";
}
}
package jpa.entity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "commodity")
public class Commodity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column
private String commodityId;
@Column
private Double price;
@ManyToMany(mappedBy = "commodities")
private List<User> users = new ArrayList<User>();
public void addUser(User user){
this.users.add(user);
}
}
package jpa.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column
private String addressId;
@Column
private String province;
@Column
private String city;
@Column
private String detailedInformation;
/*
* optional属性的默认值是true
* optional=false 时join 查询关系为inner join
* optional=true 时join 查询关系为left join
*/
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER ,cascade=CascadeType.ALL ,optional=false)
@JoinColumn(name = "user_id")
private User user;
@Override
public String toString() {
return "Address [addressId=" + addressId + ", province=" + province + ", city=" + city
+ ", detailedInformation=" + detailedInformation + "]";
}
}