Spring Boot JDBC 使用教程


總是要用到數據庫的嘛,曾經我一度以為,寫代碼,編程就是搞數據庫增刪改查,甚至你設計一個系統,大部分時候在為如何設計關系型數據庫努力,究其原因,是因為關系型數據庫是邏輯的主要呈現。

這個系列,主要是對 Spring Boot 的數據庫操作做一些示例程序展示。包括 mybatis、jpa操作、不同數據庫的鏈接方式、多數據源切換、分庫分表、自動編號問題、數據庫優化問題。

從本系列開始,都需要用到 mysql 數據庫 和其他一些參考的數據庫。請准備相關環節。

  • mysql 5.6+
  • jdk1.8+
  • spring boot 2.1.6
  • idea 2018.1

本項目源碼下載

1 准備數據庫

mysql 5.6+ 數據庫

導入數據表 數據表t_user下載

(注意:本人使用了 Navicat PreminumMysql 進行管理)

如果想學習如何在 Centos7 上部署 mysql 可以參考我這篇 阿里雲上的Centos 7.6的一次Nginx+Mysql+PHP7.3 部署https://www.cnblogs.com/fishpro/p/10651011.html)

字段 類型 主鍵 說明
id int 自動編號
user_name varchar(100) 用戶名
password varchar(255) 密碼
last_login_time date 最近登錄時間
sex tinyint 性別 0男 1女 2其他

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `last_login_time` datetime DEFAULT NULL,
  `sex` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=armscii8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
BEGIN;
INSERT INTO `t_user` VALUES (1, 'json', '123', '2019-07-27 16:01:21', 1);
INSERT INTO `t_user` VALUES (2, 'jack jo', '123', '2019-07-24 16:01:37', 1);
INSERT INTO `t_user` VALUES (3, 'manistal', '123', '2019-07-24 16:01:37', 1);
INSERT INTO `t_user` VALUES (4, 'landengdeng', '123', '2019-07-24 16:01:37', 1);
INSERT INTO `t_user` VALUES (5, 'max', '123', '2019-07-24 16:01:37', 1);
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

2 新建 Spring Boot 項目工程

  1. File > New > Project,如下圖選擇 Spring Initializr 然后點擊 【Next】下一步
  2. 填寫 GroupId(包名)、Artifact(項目名) 即可。點擊 下一步
    groupId=com.fishpro
    artifactId=jdbc
  3. 選擇依賴 Spring Web Starter 前面打鈎,勾選SQL選項的 JDBC、MySql。
  4. 項目名設置為 spring-boot-study-jdbc.

3 依賴引入 Pom.xml

    <dependencies>
		<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>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

4 Jdbc 配置

application.yml 配置(.properties文件類似)

server:
  port: 8086

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo_test?useSSL=false&useUnicode=true&characterEncoding=utf8
    username: root
    password: 123

5 編寫示例代碼

增加代碼 controller/UserController.java

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 獲取示例數據庫 t_user 的全部信息 
     * @return 返回 json 數據
     * */
    @GetMapping("/users")
    public Object   getUsers(){
        List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from t_user ");
        return  list;
    }

}

6 運行示例

運行 JdbcApplication 后在瀏覽器輸入 http://localhost:8086/api/user/users 輸出如下結果:

[{
	"id": 1,
	"user_name": "json",
	"password": "123",
	"last_login_time": "2019-07-27T21:01:21.000+0000",
	"sex": 1
}, {
	"id": 2,
	"user_name": "jack jo",
	"password": "123",
	"last_login_time": "2019-07-24T21:01:37.000+0000",
	"sex": 1
}, {
	"id": 3,
	"user_name": "manistal",
	"password": "123",
	"last_login_time": "2019-07-24T21:01:37.000+0000",
	"sex": 1
}, {
	"id": 4,
	"user_name": "landengdeng",
	"password": "123",
	"last_login_time": "2019-07-24T21:01:37.000+0000",
	"sex": 1
}, {
	"id": 5,
	"user_name": "max",
	"password": "123",
	"last_login_time": "2019-07-24T21:01:37.000+0000",
	"sex": 1
}]

7 問題思考

  1. JdbcTemplate 如何動態獲取數據庫
  2. JdbcTemplate 如何實現 POJO 操作
  3. JdbcTemplate 如何分表分庫
  4. JdbcTemplate 如何操作 Oracle 和 MSSQL Server

本項目源碼下載


免責聲明!

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



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