Java Spring Boot + druid + mybatis + PostgreSQL一分鍾搭建完成


打開IDEA 初始化Spring Boot 項目,記得鈎上 lombok

 

在pom.xml 下添加

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

 

在application.properties 下添加,注意數據庫名

# 日志的打印級別
logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=trace



# 大象數據庫
spring.datasource.druid.url=jdbc:postgresql://localhost:5432/你的數據庫名
spring.datasource.druid.username=你的用戶名
spring.datasource.druid.password=你的密碼
spring.datasource.druid.driver-class-name=org.postgresql.Driver



# 配置Druid的連接池
spring.datasource.druid.initialSize=10
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.minIdle=1
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=true
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxOpenPreparedStatements=20
spring.datasource.druid.validationQuery=SELECT 1
spring.datasource.druid.validation-query-timeout=500
spring.datasource.druid.filters=stat

 創建大象數據庫表

 CREATE TABLE HEXINFO(
    -- SERIAL 自增主鍵
   ID             SERIAL PRIMARY KEY  NOT NULL,
    --  摘要
   HEX            TEXT                        NOT NULL,
   SPAWNTIME      TIMESTAMP                   NOT NULL,
   INFO           TEXT                        NOT NULL,
   NODEID         TEXT                        NOT NULL,
   CHUNKID        INT                         NOT NULL,
);

-- 查詢最大值語句,等下用的到
 select * from hexinfo where id in(select max(id) from hexinfo);
-- 插入一條信息
INSERT INTO HEXINFO (HEX,SPAWNTIME,INFO,NODEID) VALUES('000000000001',current_timestamp,'oxoxoxox','123456');

在項目路徑下創建 3 個 文件

第一個 Hexinfo.java

package cn.edu.zzuli.fabriciec;


import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
public class Hexinfo {
    private Integer id;
    private String hex;
    private Date spawntime;
    private String info;
    private String nodeid;
    private Integer chunkid;
}

 第二個 HexinfoMapper.java

package cn.edu.zzuli.fabriciec;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface HexinfoMapper {
    @Select("select * from hexinfo where id in(select max(id) from hexinfo);")  // 這里是使用Mapper
    List<Hexinfo> getMax();
}

 第三個 HexinfoController.java

package cn.edu.zzuli.fabriciec;


import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin // 跨域請求
@Data
@AllArgsConstructor
@RestController
public class HexinfoController {
    private HexinfoMapper hexinfoMapper;
    @GetMapping("/max")
    public List<Hexinfo> getByMax() {
        //return hexinfoService.getByMax();
        return hexinfoMapper.getMax();
    }
}

 啟動項目

訪問端口 localhost:8080/max 

 返回和這段類似就是成功

	
0	
id	22
hex	"fec6d532f885902bf5136588f0ebd52e0b8f13fc365ec65d763c150000000000"
spawntime	"2020-04-28T04:02:12.420+0000"
info	"I-Love-Java"
nodeid	"java-point"
chunkid	666

 

可能不理解為什么這么寫 安利一個教程,我是在這里學的不過自己用的數據庫還是比較喜歡大象。為啥用大象?emmm....

http://blog.didispace.com/spring-boot-learning-2x/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM