实际上没多少技术含量,就是spring data jdbc+mysql 数据访问而且,主要就是几个参数配置
spring boot 项目结构
使用官方的初始化器
- 代码结构
├── HELP.md
├── demo.iml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ └── mongobi
│ │ └── demo
│ │ ├── CntArticleDAO.java
│ │ ├── DemoApplication.java
│ │ └── RestApi.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── dalong
└── mongobi
└── demo
└── DemoApplicationTests.java
- 代码说明
RestApi.java 为简单查询,CntArticleDAO.java 为数据访问,application.properties 为jdbc 配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalong.mongobi</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CntArticleDAO.java
package com.dalong.mongobi.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public class CntArticleDAO {
@Autowired
JdbcTemplate jdbcTemplate;
public List<Map<String, Object>> search() {
String sql = "select * from cntArticle";
return jdbcTemplate.queryForList(sql);
}
}
RestApi.java
package com.dalong.mongobi.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestApi {
@Autowired
CntArticleDAO cntArticleDAO;
@RequestMapping("/cns")
public Object users(){
return cntArticleDAO.search();
}
}
启动效果
mvn spring-boot:run
主要说明
jdbc 的版本以及ssl 配置参数,是最重要的,按照官方的介绍8以及最新的是不可以的(测试也是),支持ssl 的
jdbc 配置为jdbc:mysql://localhost:3307/appdemos?useSSL=true
,同时官方也提供了一个auth 的扩展,可以参
考https://docs.mongodb.com/bi-connector/master/reference/auth-plugin-jdbc/
参考资料
https://docs.mongodb.com/bi-connector/master/reference/auth-plugin-jdbc/
https://github.com/rongfengliang/mongobi-java