Jpa的@Id和@GeneratedValue的使用
生活随笔
收集整理的這篇文章主要介紹了
Jpa的@Id和@GeneratedValue的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Jpa的@Id和@GeneratedValue的使用
import lombok.Data; import javax.persistence.*; import java.math.BigInteger;@Entity//將實(shí)體類首字母小寫 @Table(name = "user")//要連接的數(shù)據(jù)庫表名 @Data public class UserCopy {@Id@GeneratedValue(strategy= GenerationType.IDENTITY)// @GeneratedValue 用于標(biāo)注主鍵的生成策略,通過strategy 屬性指定。默認(rèn)情況下,JPA 自動選擇一個最適合底層數(shù)據(jù)庫的主鍵生成策略:SqlServer對應(yīng)identity // IDENTITY:采用數(shù)據(jù)庫ID自增長的方式來自增主鍵字段,Oracle 不支持這種方式;private Integer id;private String userId;private String password;private String userName;private String userType;private BigInteger createTime;private BigInteger updateTime;}SpringDataJPA之Repository接口
import com.hebutgo.refund.sampleUser.dto.UserListDTO; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param;import javax.transaction.Transactional; import java.util.List;public interface UserCopyRepository extends JpaRepository<UserCopy,Integer> {//<表名,id(主鍵)屬性類型是Integer>@Transactionalvoid deleteByUserId(String userId);UserCopy findByUserId(String userId);UserCopy findByUserIdAndPassword(String userId, String password);UserCopy findByUserName(String userName);// @Transactional // @Modifying // @Query(value = "update users set user_name=:userName where user_id=:userId ", nativeQuery = true) // void updateNameByUserIdNative(String userId, String userName); // // List<User> findByUserIdIn(List<String> userIds); // // User findByToken(String token); // // @Query(value = "select new com.hebutgo.refund.sampleUser.dto.UserListDTO(t1.id, t1.userId, t1.userName, t1.userType, t1.gender, t1.mobile, t1.community, t1.doorNum, t1.birthday,t1.serviceHours,t1.token) from User t1 where t1.userId = :userId and t1.password = :password") // List<UserListDTO> login(@Param(value = "userId")String userId, @Param(value = "password") String password);}總結(jié)
以上是生活随笔為你收集整理的Jpa的@Id和@GeneratedValue的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python操作mySql数据库封装类
- 下一篇: 三、华为鸿蒙HarmonyOS应用开发H