Springboot+MySQL+durid+JPA整合

java1234

共 5690字,需浏览 12分钟

 ·

2020-12-24 00:46

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

66套java从入门到精通实战课程分享

练习Springboot数据库的操作,JPA的使用

1.添加maven依赖

包括:MySQL连接驱动、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.配置文件配置数据库、连接池、JPA

#配置mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#resource数据源
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
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
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.数据源配置类

关联数据库、实体类(pojo)、Repository(Dao)

@Configuration
@EntityScan(basePackages = "com.xx.xx.pojo")
@EnableJpaRepositories(
        basePackages = "com.xx.xx.Repository",
        entityManagerFactoryRef = "resourceEntityManagerFactoryBean",
        transactionManagerRef = "resourceTransactionManager")
@EnableTransactionManagement

数据源配置bean的关系

4. pojo vs Repository

实体类

@Data
@Entity
@Table(name = "Server_Msg")
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("服务器信息")
public class ServerResourceMsg {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name = "id")
    @ApiModelProperty(value = "主键id")
    private int id;
    @ApiModelProperty(value = "区域")
    @Column(name = "region")
    private String region;
    @ApiModelProperty(value = "资源节点")
    @Column(name = "resource_node")
    private String resource_node;
    @ApiModelProperty(value = "IPMI IP")
    @Column(name = "ipmi_ip")
    private String ipmiIp;
    @ApiModelProperty(value = "项目所属")
    @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 = "内存")
    @Column(name = "memory")
    private String memory;
    @ApiModelProperty(value = "硬盘")
    @Column(name = "disk_capacity")
    private String disk_capacity;
    @ApiModelProperty(value = "网卡(块)")
    @Column(name = "mvc_num")
    private String mvc_num;
    @ApiModelProperty(value = "状态")
    @Column(name = "status")
    private String status;
    @ApiModelProperty(value = "致命警告")
    @Column(name = "fatal_warning")
    private String fatal_warning;
    @ApiModelProperty(value = "预警")
    @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 {
    //基于注解
    //依据区域和资源节点查询
    @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);
    // 资源节点查询
    @Query(value = "select * from Server_Msg where resource_node = ? ",nativeQuery = true)
    List FindServerByResourceNode(String resource_node);
    //依据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("填加服务器信息到数据库")
    public String AddServerMsg(@ApiParam("服务器信息") ServerResourceMsg serverResourceMsg)
    {   serverResourceMsgService.add(serverResourceMsg);
        return "success";
    }
    @GetMapping("/getbyid")
    @ApiOperation("依据id从数据库获取服务器信息")
    public ServerResourceMsg GetServerMsg(@ApiParam("id") int id)
    {   
        return serverResourceMsgService.FindById(id);
    }



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:

https://blog.csdn.net/weixin_44250623/article/details/111357179




粉丝福利:Java从入门到入土学习路线图

???

?长按上方微信二维码 2 秒


感谢点赞支持下哈 

浏览 23
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报