SpringBoot Data Jpa基本使用
- 导入springboot data jpa依赖
- 创建实体映射类
@Entity 用来标注一个数据库对应的实体,数据库中创建的表名默认和类名一致
@Table(name="",catalog="",schema="") 用来标注一个数据库对应的实体,
主键需要加上注解 @Id注解
如果是自动生成需要设置生成柜子 @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "menu_name") 设置属性对应的数据库字段名称
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "sys_menu")
public class SysMenu {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "menu_id")
private long menuId;
@Column(name = "menu_name")
private String menuName;
@Column(name = "parent_id")
private long parentId;
@Column(name = "order_num")
private long orderNum;
@Column(name = "url")
private String url;
@Column(name = "is_delete")
private String isDelete;
@Column(name = "target")
private String target;
@Column(name = "menu_type")
private String menuType;
@Column(name = "visible")
private String visible;
@Column(name = "perms")
private String perms;
@Column(name = "icon")
private String icon;
@Column(name = "create_by")
private String createBy;
@Column(name = "create_time")
private Date createTime;
@Column(name = "update_by")
private String updateBy;
@Column(name = "update_time")
private Date updateTime;
@Column(name = "remark")
private String remark;
}
- 新建一个dao接口 继承JpaRepository<SysMenu,Long> , JpaSpecificationExecutor**
接口
JpaRepository<T,TD> 封装了简单的crud方法
JpaSpecificationExecutor**
封装了高级查询方法 其中 T**是实体对象 TD**是主建类型
@Repository
public interface SysMenuDAO extends JpaRepository<SysMenu, Long>, JpaSpecificationExecutor<SysMenu> {
}
- 然后直接使用 调用方法crud