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/

 

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

上一篇:[源码分析]从"UDF不应有状态" 切入来剖析Flink SQL代码生成 (修订版)


下一篇:SQL注入实战之报错注入篇(updatexml extractvalue floor)