数据库之扑朔迷离 2019-09-07
/** * SponsorApplication for 广告赞助商/投递服务启动类 * 添加注解{@link EnableFeignClients}之后,当前微服务就可以调用别的微服务, * 但是当前服务是广告提供,不需要调用别的微服务,在此只是为了在dashboard中监控 * {@link EnableCircuitBreaker} 也是为了dashboard监控 * * @author <a href="mailto:[email protected]">Isaac.Zhang</a> * @since 2019/6/15 */ @EnableDiscoveryClient //开启服务发现Eureka Client @EnableCircuitBreaker //开启断路器 @EnableFeignClients //开启feign client,使其可以通过HTTP调用其他微服务 @SpringBootApplication public class SponsorApplication { public static void main(String[] args) { SpringApplication.run(SponsorApplication.class, args); } }
server: port: 7000 servlet: context-path: /ad-sponsor #http请求的根路径(请求前缀,在handle的mapping之前,需要127.0.0.1/ad-sponsor/XXXX) spring: application: name: mscx-ad-sponsor jpa: show-sql: true #执行时是否打印sql语句,方便调试 hibernate: ddl-auto: none properties: hibernate.format_sql: true open-in-view: false #控制是否在懒加载时,有可能会找不到bean报错 datasource: username: *** url: jdbc:mysql://127.0.0.1:3306/advertisement?useSSL=false&autoReconnect=true password: *** tomcat: max-active: 4 #最大连接数 min-idle: 2 #最小空闲连接数 initial-size: 2 #默认初始化连接数 eureka: client: service-url: defaultZone: http://server1:7777/eureka/,http://server2:8888/eureka/,http://server3:9999/eureka/
实体类和数据库表一般是一一对应,通常称之为entity
,以用户表为例:Lombok传送门
/** * AdUser for 数据库ad_user表对应的实体类 * {@link Basic} 标示为数据库的字段信息,如果需要一个不属于数据库的字段,标注为 {@link Transient} * * @author <a href="mailto:[email protected]">Isaac.Zhang</a> * @since 2019/6/15 */ @Data @AllArgsConstructor @NoArgsConstructor @Entity @Table(name = "ad_user") public class AdUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id", nullable = false) private Long userId; @Basic //不写的话,默认就是@Basic,表示是数据库表的一个字段 //@Transient //如果打上@Transient 注解,表明当前字段不是表中的字段 @Column(name = "user_name", nullable = false) private String userName; @Basic @Column(name = "token", nullable = false) private String token; @Basic @Column(name = "user_status", nullable = false) private Integer userStatus; @Basic @Column(name = "create_time", nullable = false) private Date createTime; @Basic @Column(name = "update_time", nullable = false) private Date updateTime; /** * 创建用户时所需的必填字段 * * @param user_name 用户名称 * @param token token */ public AdUser(String user_name, String token) { this.userName = user_name; this.token = token; this.userStatus = CommonStatus.VALID.getStatus(); this.createTime = new Date(); this.updateTime = this.createTime; } } --- /** * CommonStatus for 通用状态枚举 * * @author <a href="mailto:[email protected]">Isaac.Zhang</a> */ @Getter public enum CommonStatus { VALID(1, "有效"), INVALID(0, "无效状态"); private Integer status; private String desc; CommonStatus(Integer status, String desc) { this.status = status; this.desc = desc; } }
其他的数据库对应实体类(AdUnit
,AdPlan
,AdCreative
,AdUnitDistrict
,AdUnitHobby
,AdUnitKeyword
,RelationshipCreativeUnit
),大家可以参考上面的例子自己实现,也可以去github上下载源码。
做一个好人。
博客园 | segmentfault | spring4all | csdn | 掘金 | OSChina | 简书 | 头条 | 知乎 | 51CTO