SpringBoot(7):Jdbc链接数据库

SpringBoot JDBC 数据库连接
本文介绍如何使用SpringBoot和JDBC连接MySQL数据库,并通过示例代码演示了配置属性文件、添加Maven依赖及实现数据库查询的过程。

Jdbc连接数据库

使用SpringBoot连接mySQL数据库有很多种方式,本文将使用JDBC去实现。

1.首先是属性配置文件

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/girl
    username: root
    password: 
    driver-class-name: com.mysql.jdbc.Driver
2.添加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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.springboot</groupId>
	<artifactId>study</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>SpringBoot-JDBC</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.7</java.version>
	</properties>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
3.代码实例

首先我们先完成Service层的代码,这一部分主要是使用JdbcTemplate来进行数据库操作,下面以查询为例:

package com.springboot.study.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.springboot.study.entity.Girl;

@Service
public class GirlService {
	
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<Girl> getGirlList(){
		String sql="select *from girl";
		return jdbcTemplate.query(sql, new RowMapper<Girl>(){
			@Override
			public Girl mapRow(ResultSet rs, int rowNum) throws SQLException {
				Girl girl=new Girl();
				girl.setId(rs.getInt("id"));
				girl.setAge(rs.getInt("age"));
				girl.setCup_size(rs.getString("cup_size"));
				return girl;
			}
		});
	}
	
}
然后是实体类:

Girl.java

package com.springboot.study.entity;

public class Girl {
	
	private int id;
	
	private int age;
	
	private String cup_size;

	public Girl(int id, int age, String cup_size) {
		super();
		this.id = id;
		this.age = age;
		this.cup_size = cup_size;
	}

	public Girl() {
		super();
	}

	public int getId() {
		return id;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getCup_size() {
		return cup_size;
	}

	public void setCup_size(String cup_size) {
		this.cup_size = cup_size;
	}

	@Override
	public String toString() {
		return "Girl [id=" + id + ", age=" + age + ", cup_size=" + cup_size + "]";
	}
}
再然后就是Controller的编写

MyController.java

package com.springboot.study.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.study.entity.Girl;
import com.springboot.study.service.GirlService;

@RestController
public class MyController {
	
	@Autowired
	private GirlService girlService;
	
	@RequestMapping("/girls")
	public List<Girl> getGirl(){
		return girlService.getGirlList();
	}
}

点击: http://localhost:8080/girls便可查询到数据

结果如下:


这样便完成了。


在Spring Boot的`application.properties`配置文件中,可以通过配置相关属性来链接数据库。`application.properties`是基于属性文件的配置格式,支持`key-value`形式的键值对,可在其中配置数据库连接等应用程序属性[^2]。 以常见的关系型数据库MySQL为例,在`application.properties`文件中配置数据库连接的示例如下: ```properties # 数据库连接的URL spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC # 数据库用户名 spring.datasource.username=your_username # 数据库密码 spring.datasource.password=your_password # 数据库驱动类 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 如果使用的是`application.yml`文件(YAML格式),配置示例如下: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: your_username password: your_password driver-class-name: com.mysql.cj.jdbc.Driver ``` 同时,还可以使用实体类结合`@ConfigurationProperties`注解的方式来配置数据库连接信息,示例代码如下: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.datasource") public class DatabaseProperties { private String url; private String username; private String password; private String driverClassName; // Getters and setters public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } // toString() @Override public String toString() { return "DatabaseProperties{" + "url='" + url + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + ", driverClassName='" + driverClassName + '\'' + '}'; } // 可以在这里添加其他的业务逻辑 } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值