1. 程式人生 > >Spring Boot使用JDBC方式連線MySQL

Spring Boot使用JDBC方式連線MySQL

首先去spring官網下載一個名為test的Spring Boot專案模板:https://start.spring.io/

然後在mysql中的testdb資料庫中新建一張名為test_user的表:

drop table if exists `test_user`;
create table `test_user` (
    `id` integer not null auto_increment primary key,
    `name` varchar(20),
    `password` varchar(20)
);
insert into test_user (`name`, `password`) values ('zifeiy', '123456'), ('apple', '654321');

專案中,首先在pom.xml中引入依賴:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

在application.properties中填寫MySQL連線相關的資訊:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

然後新建一個Java Bean:TestUser.java:

package com.zifeiy.test.po;

public class TestUser {
    private Integer id;
    private String name;
    private String password;
    
    // getters & setters
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

然後在測試類中編輯如下程式碼進行測試:

package com.zifeiy.test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit4.SpringRunner;

import com.zifeiy.test.po.TestUser;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {
    
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Test
    public void contextLoads() {
        String sql = "select * from test_user";
        List<TestUser> userList = (List<TestUser>) jdbcTemplate.query(sql, new RowMapper<TestUser>() {

            @Override
            public TestUser mapRow(ResultSet rs, int rowNum) throws SQLException {
                TestUser user = new TestUser();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                return user;
            }
            
        });
        System.out.println("查詢成功");
        for (TestUser user : userList) {
            System.out.println("id = " + user.getId() + " , name = " + user.getName() + " , password = " + user.getPassword());
        }
    }

}

在命令列中得到測試結果如下:

查詢成功
id = 1 , name = zifeiy , password = 123456
id = 2 , name = apple , password = 654321