SpringBoot+Spring Data Jpa的简单使用

1.pom.xml添加依赖
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.配置文件配置

spring:
  #配置spring data jpa
  jpa:
    hibernate:
      ddl-auto: validate
    #显示sql
    show-sql: true
    #连接的数据库类型
    database: oracle

 

3.实体类

@Getter
@Setter
@ToString
@ApiModel(value = "User", description = "用户信息")
@Entity
@Table(name = "SYS_USER")
public class User extends BaseDomain implements Serializable {

   private static final long serialVersionUID = -3361134827184862067L;
   
   @ApiModelProperty(value = "Id")
   @Id
   @Column(name = "ID")
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQUENCE")
   @SequenceGenerator(name = "USER_SEQUENCE", sequenceName = "USER_SEQUENCE")
   private Integer id;
   
   @ApiModelProperty(value = "用户名")
   @Column(name = "USERNAME",columnDefinition = "nvarchar2")
    private String userName;
    
    @JsonIgnore
    @ApiModelProperty(value = "密码")
   @Column(name = "PASSWORD",columnDefinition = "nvarchar2")
    private String password;

    @ApiModelProperty(value = "登录名")
   @Column(name = "LOGINNAME",columnDefinition = "nvarchar2")
    private String loginName;
    
    @Resource
   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name = "ROLEID", referencedColumnName = "ID")
    @ApiModelProperty(value = "角色")
    private Role role;

}

 

@Getter
@Setter
@ApiModel(value = "Role", description = "角色信息")
@Entity
@Table(name = "SYS_ROLE")
public class Role extends BaseDomain implements Serializable {

   private static final long serialVersionUID = 3668357124757879207L;

   @ApiModelProperty(value = "Id")
   @Id
   @Column(name = "ID")
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ROLE_SEQUENCE")
   @SequenceGenerator(name = "ROLE_SEQUENCE", sequenceName = "ROLE_SEQUENCE")
   private Integer id;
   
   @ApiModelProperty(value = "角色名称, 超级管理员:admin;查看角色:view;发布角色:publisher")
   @Column(name = "ROLENAME",columnDefinition = "nvarchar2")
   private String roleName;

   @ApiModelProperty(value = "角色类型")
   @Column(name = "ROLETYPE",columnDefinition = "nvarchar2")
   private String roleType;

   @Transient
   @ApiModelProperty(value = "角色权限")
   private List<Authority> authorities;

   @ApiModelProperty(value = "角色描述")
   @Column(name = "DESCRIPTION",columnDefinition = "nvarchar2")
   private String description;

   @ApiModelProperty(value = "状态")
   @Column(name = "STATUS",columnDefinition = "nvarchar2")
   private String status;
   
   public Role() {
      super();
   }
   
   public Role(Integer roleId) {
      this.id = roleId;
   }
   
   /**
    * 专用于新建角色的构造器 
    */
   public Role(String roleName, String roleType, String description) {
      super();
      this.roleName = roleName;
      this.roleType = roleType;
      this.description = description;
      this.status = Status.ENABLED;
      }
}

4.Repository的方法

@Repository
public interface TestMapper extends JpaRepository<User,Integer> {

    List<User> findByUserNameContaining(String userName);

}

注意:@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY) 懒加载会导致查询失败
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值