易筋SpringBoot 2.1 | 第六篇:JdbcTemplate访问MySQL

写作时间:2019-01-03
Spring Boot: 2.1 ,JDK: 1.8, IDE: IntelliJ IDEA, MySQL 8.0.13

说明

数据需要持久化保存,无论是客户端的SQLLITE,还是服务端的MySQL。这里记录最原始的访问数据库方式,JdbcTemplate访问MySQL. Spring 提供了一个模板类JdbcTemplate,能够容易跟数据库和数据库连接JDBC(Java DataBase Connectivity)打交道。 绝大多数的JDBC代码在写业务代码之前需要很多准备工作:获取数据库资源,连接管理,异常处理,常规错误校验. JdbcTemplate帮你处理上面的所有问题, 让开发者专注于业务.

MySQL数据库

  1. 首先需要到MySQL的官网下载社区版的安装文件。笔者下载的是最新版本8.0.13.
    易筋SpringBoot 2.1 | 第六篇:JdbcTemplate访问MySQL_第1张图片

  2. 接下来:下载 > 安装 > 设置root密码(mysql8.0以上密码策略限制必须要大小写加数字特殊符号)
    安装完成以后,安装路径在/usr/local,笔者的MySQL路径为:/usr/local/mysql-8.0.13-macos10.14-x86_64

  3. 在命令行输入mysql命令:

$ mysql
zsh: command not found: mysql
  1. 发现找不到命令,需要先设置环境变量,笔者的命令行环境为myzsh,设置如下:
$ cd $home
$ vim .zshrc
  1. 在最下面加上环境变量配置
# Mysql
export MYSQL_HOME="/usr/local/mysql-8.0.13-macos10.14-x86_64"
export PATH=$MYSQL_HOME/bin:$PATH
  1. 保存esc > :wq, 重新加载配置文件
$ source .zshrc
  1. 连接数据库:
$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.13 MySQL Community Server - GPL
  1. 新建数据库和用户(注意: -- 后面是注释)
mysql> create database db_example; -- Create the new database
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'%'; -- Gives all the privileges to the new user on the newly created database
  1. 显示数据库:
mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| db_example         |
+--------------------+
5 rows in set (0.00 sec)
  1. 选择数据库
mysql> use mysql;
Database changed
  1. 显示数据库中的表
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| component                 |
...

MySQL客户端

在命令行里面写sql不方便,可以下载客户端MySQL Workbench 8.0.13,查询例子如下,执行快捷键cmd+enter
易筋SpringBoot 2.1 | 第六篇:JdbcTemplate访问MySQL_第2张图片

忘记root密码也可以修改,建议保存好密码。
易筋SpringBoot 2.1 | 第六篇:JdbcTemplate访问MySQL_第3张图片

工程建立

参照教程【SpringBoot 2.1 | 第一篇:构建第一个SpringBoot工程】新建一个Spring Boot项目,名字叫demojdbctemplate, 在目录src/main/java/resources 下找到配置文件application.properties,重命名为application.yml

配置文件

pom.xml 依赖配置添加:

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.13version>
 dependency>

MySQL连接的配置mysql-connector-java信息来自官网 »

  1. 8.0.13 链接
  2. 所有版本链接

如果没有配置mysql-connector-java,或者配置的版本不对,会报错Unable to create initial connections of pool

数据库连接信息配置src/main/resources/application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_example
    username: springuser
    password: ThePassword

可能会遇到的错误:Warning about SSL connection when connecting to MySQL database
解决方法, 修改url的配置如下:url: jdbc:mysql://localhost:3306/db_example?autoReconnect=true&useSSL=false
上面配置会禁止SSL并抑制SSL错误.
参考:stackoverflow

代码实现

  1. 新建实体类com.zgpeace.demojdbctemplate.bean.Customer
package com.zgpeace.demojdbctemplate.bean;

public class Customer {
    private long id;
    private String firstName, lastName;

    public Customer(long id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

    // getters & setters omitted for brevity

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

  1. 完善启动类com.zgpeace.demojdbctemplate.DemojdbctemplateApplication
package com.zgpeace.demojdbctemplate;

import com.zgpeace.demojdbctemplate.bean.Customer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
public class DemojdbctemplateApplication implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(DemojdbctemplateApplication.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DemojdbctemplateApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Creating tables");

        jdbcTemplate.execute("DROP TABLE IF EXISTS customers");
        jdbcTemplate.execute("CREATE TABLE customers(" +
                "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");

        // Split up the array of whole names into an array of first/last names
        List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream()
                .map(name -> name.split(" "))
                .collect(Collectors.toList());

        // Use a Java 8 stream to print out each tuple of the list
        splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1])));

        // Uses JdbcTemplate's batchUpdate operation to bulk load data
        jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames);

        log.info("Querying for customer records where first_name = 'Josh':");

        jdbcTemplate.query("SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[] { "Josh"},
                (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"),
                        rs.getString("last_name"))).forEach(customer -> log.info(customer.toString()));


    }
}

实现功能解说:
1)注入jdbcTemplate对象
2)创建表customers
3)插入数据
4)查询满足条件firstName='Josh'的记录

  1. 运行工程, 控制台打印信息如下:
2019-01-03 10:51:35.301  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Creating tables
2019-01-03 10:51:35.303  INFO 23276 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-01-03 10:51:35.626  INFO 23276 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-01-03 10:51:35.723  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Inserting customer record for John Woo
2019-01-03 10:51:35.723  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Inserting customer record for Jeff Dean
2019-01-03 10:51:35.723  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Inserting customer record for Josh Bloch
2019-01-03 10:51:35.723  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Inserting customer record for Josh Long
2019-01-03 10:51:35.743  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Querying for customer records where first_name = 'Josh':
2019-01-03 10:51:35.755  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Customer[id=3, firstName='Josh', lastName='Bloch']
2019-01-03 10:51:35.755  INFO 23276 --- [           main] c.z.d.DemojdbctemplateApplication        : Customer[id=4, firstName='Josh', lastName='Long']

总结

恭喜你!实现了JdbcTemplate访问MySQL。 企业项目一般会用Mybatis或者JPA操作数据库,所以这里只要会用JdbcTemplate就好。

代码下载:
https://github.com/zgpeace/Spring-Boot2.1/tree/master/db/demojdbctemplate

参考:
https://spring.io/guides/gs/relational-data-access/
https://spring.io/guides/gs/accessing-data-mysql/
https://blog.csdn.net/forezp/article/details/70477821
https://www.jianshu.com/p/a8e4068a7a8a

你可能感兴趣的:(易实战Sprint,Boot,2.1)