应用场景
将环形独立为starter后涉及到创建表数据,想要实现每一个项目在使用该starter项目的时候就把表结构导入到自己的项目中,使用jpa实现
实现步骤
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.对实体类加注解
java">package org.aimeng.em.entity;import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;import javax.persistence.*;import java.io.Serializable;
import java.time.LocalDateTime;@Data
@EqualsAndHashCode(callSuper = false)
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "EmMsg对象", description = "")
@Entity
@Table(name = "em_msg")
public class EmMsg implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@ApiModelProperty(value = "主键id")@TableId(value = "id", type = IdType.AUTO)private Long id;@ApiModelProperty(value = "经纪人id")
// @Column(name = "agent_id", columnDefinition = "bigint COMMENT '经纪人id'")private Long agentId;@ApiModelProperty(value = "类型(1通知、2代办)")private Integer type;@ApiModelProperty(value = "是否跳转(0是 1否)")private Integer isJump;@ApiModelProperty(value = "消息id")private String msgId;@ApiModelProperty(value = "额外字段")private String ext;@ApiModelProperty(value = "状态")private Integer status;@ApiModelProperty(value = "内容")private String content;@ApiModelProperty(value = "创建时间")@TableField(fill = FieldFill.INSERT)private LocalDateTime createTime;@ApiModelProperty(value = "更新时间")@TableField(fill = FieldFill.INSERT_UPDATE)private LocalDateTime updateTime;@Transient@TableField(exist = false)private String time;
}
解释:
- 在类上加上@Entity和@Table注解指明表名称
- 在主键上加上@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)表明主键自增
-
@Transient注解可以忽略某个字段不被创建
-
@Column(name = "agent_id", columnDefinition = "bigint COMMENT '经纪人id'") 该注解可以指定表名称和表类型、注释
3.配置参数
//update有则创建无则更新(注:正式环境中慎用)spring.jpa.hibernate.ddl-auto=update//输出sql,方便调试spring.jpa.show-sql=true//指定msqyl版本8spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
4.启动类加注解@EntityScan
java">@Slf4j
@SpringBootApplication
@EnableDubbo
@EnableScheduling
@CicadaScan
@ComponentScan(basePackages = {"vip.lspace.agent.*"})
public class LspaceAgentApplication {}
如果你的实体类位于不同的包中,而这个包不在 Spring Boot 应用主类所在的包及其子包中,你需要使用
@EntityScan
指定这些实体类所在的包