Springboot+MySQL+durid+JPA整合
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達(dá)
66套java從入門到精通實(shí)戰(zhàn)課程分享
練習(xí)Springboot數(shù)據(jù)庫的操作,JPA的使用
1.添加maven依賴
包括:MySQL連接驅(qū)動、Druid連接池、JPA依賴、lombok、fastjson
????????
????????
????????????mysql
????????????mysql-connector-java
????????????8.0.19
????????????compile
????????
????????
????????
????????????com.alibaba
????????????druid
????????????1.1.10
????????
????????
????????
????????????org.springframework.boot
????????????spring-boot-starter-data-jpa
????????
????????
????????
????????????org.projectlombok
????????????lombok
????????????1.18.8
????????
????????
????????
????????????com.alibaba
????????????fastjson
????????????1.2.60
????????
2.配置文件配置數(shù)據(jù)庫、連接池、JPA
#配置mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#resource數(shù)據(jù)源
spring.datasource.druid.resource.jdbc-url=jdbc:mysql://localhost:3306/resource?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.druid.resource.username=root
spring.datasource.druid.resource.password=admin
#?初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.validationQuery=SELECT?'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
#?打開PSCache,并且指定每個連接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#?配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻
spring.datasource.filters=stat,wall,log4j
#?通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#?合并多個DruidDataSource的監(jiān)控數(shù)據(jù)
spring.datasource.useGlobalDataSourceStat=true
#jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
3.數(shù)據(jù)源配置類
關(guān)聯(lián)數(shù)據(jù)庫、實(shí)體類(pojo)、Repository(Dao)
@Configuration
@EntityScan(basePackages?=?"com.xx.xx.pojo")
@EnableJpaRepositories(
????????basePackages?=?"com.xx.xx.Repository",
????????entityManagerFactoryRef?=?"resourceEntityManagerFactoryBean",
????????transactionManagerRef?=?"resourceTransactionManager")
@EnableTransactionManagement
數(shù)據(jù)源配置bean的關(guān)系
4. pojo vs Repository
實(shí)體類
@Data
@Entity
@Table(name?=?"Server_Msg")
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("服務(wù)器信息")
public?class?ServerResourceMsg?{
????@Id
????@GeneratedValue(strategy=?GenerationType.IDENTITY)
????@Column(name?=?"id")
????@ApiModelProperty(value?=?"主鍵id")
????private?int?id;
????@ApiModelProperty(value?=?"區(qū)域")
????@Column(name?=?"region")
????private?String?region;
????@ApiModelProperty(value?=?"資源節(jié)點(diǎn)")
????@Column(name?=?"resource_node")
????private?String?resource_node;
????@ApiModelProperty(value?=?"IPMI?IP")
????@Column(name?=?"ipmi_ip")
????private?String?ipmiIp;
????@ApiModelProperty(value?=?"項(xiàng)目所屬")
????@Column(name?=?"affiliation")
????private?String?affiliation;
????@ApiModelProperty(value?=?"CPU(顆)")
????@Column(name?=?"cpu_num")
????private?String?cpu_num;
????@ApiModelProperty(value?=?"CPU(核)")
????@Column(name?=?"cpu_cores")
????private?String?cpu_cores;
????@ApiModelProperty(value?=?"內(nèi)存")
????@Column(name?=?"memory")
????private?String?memory;
????@ApiModelProperty(value?=?"硬盤")
????@Column(name?=?"disk_capacity")
????private?String?disk_capacity;
????@ApiModelProperty(value?=?"網(wǎng)卡(塊)")
????@Column(name?=?"mvc_num")
????private?String?mvc_num;
????@ApiModelProperty(value?=?"狀態(tài)")
????@Column(name?=?"status")
????private?String?status;
????@ApiModelProperty(value?=?"致命警告")
????@Column(name?=?"fatal_warning")
????private?String?fatal_warning;
????@ApiModelProperty(value?=?"預(yù)警")
????@Column(name?=?"early_warning")
????private?String?early_warning;
????@ApiModelProperty(value?=?"提醒")
????@Column(name?=?"remind")
????private?String?remind;
????@ApiModelProperty(value?=?"未知")
????@Column(name?=?"unknown")
????private?String?unknown;
}
繼承JpaRepository,可基于方法名或者注解
public?interface?ServerResourceMsgRepository?extends?JpaRepository?{
????//基于注解
????//依據(jù)區(qū)域和資源節(jié)點(diǎn)查詢
????@Query(value?=?"select?*?from?Server_Msg?where?region?=???and?resource_node?=???",nativeQuery?=?true)
????List?FindServer(String?region,?String?resource_node);
????@Query(value?=?"select?*?from?Server_Msg?where?region?=???",nativeQuery?=?true)
????List?FindServerByRegion(String?region);
????//?資源節(jié)點(diǎn)查詢
????@Query(value?=?"select?*?from?Server_Msg?where?resource_node?=???",nativeQuery?=?true)
????List?FindServerByResourceNode(String?resource_node);
????//依據(jù)IPMIIP查詢
????@Query(value?=?"select?*?from?Server_Msg?where?ipmi_ip?=???",nativeQuery?=?true)
????List?FindServerByIPMIIP(String?ipmi_ip);
????//基于方法名
????ServerResourceMsg?findById(int?id);
}
5 Service層封裝Repository
@Service
public?class?ServerResourceMsgService?{
????public?List?FindServerByIPMIIP(String?ipmiip){
????????return?serverResourceMsgRepository.FindServerByIPMIIP(ipmiip);
????}
????public?void?add(ServerResourceMsg?serverResourceMsg){
????????serverResourceMsgRepository.save(serverResourceMsg);
????}
????public?ServerResourceMsg?FindById(int?id){
????????return?serverResourceMsgRepository.findById(id);
????}
}
6.Controller接口
????@PostMapping("/add")
????@ApiOperation("填加服務(wù)器信息到數(shù)據(jù)庫")
????public?String?AddServerMsg(@ApiParam("服務(wù)器信息")?ServerResourceMsg?serverResourceMsg)
????{???serverResourceMsgService.add(serverResourceMsg);
????????return?"success";
????}
????@GetMapping("/getbyid")
????@ApiOperation("依據(jù)id從數(shù)據(jù)庫獲取服務(wù)器信息")
????public?ServerResourceMsg?GetServerMsg(@ApiParam("id")?int?id)
????{???
????????return?serverResourceMsgService.FindById(id);
????}
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/weixin_44250623/article/details/111357179
粉絲福利:Java從入門到入土學(xué)習(xí)路線圖
???

?長按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
評論
圖片
表情
