目录

SpringBoot-中怎么链接数据库并获取数据

SpringBoot 中怎么链接数据库并获取数据

在Spring框架中,有多种方式可以链接数据库并获取数据。常见的方式包括使用Spring JDBC、Spring Data JPA和Spring Data JDBC。以下是每种方式的简要说明和示例代码。

1. 使用Spring JDBC

Spring JDBC提供了一组方便的类,用于简化数据库访问。以下是使用Spring JDBC的步骤:

配置数据源

首先,需要配置数据源。这可以在application.propertiesapplication.yml中完成。

  
# application.properties
  
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
  
spring.datasource.username=yourusername
  
spring.datasource.password=yourpassword
  
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  

配置JdbcTemplate Bean

然后,在你的配置类中配置JdbcTemplate Bean。

  
import org.springframework.context.annotation.Bean;
  
import org.springframework.context.annotation.Configuration;
  
import org.springframework.jdbc.core.JdbcTemplate;
  
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
  
public class DatabaseConfig {

@Bean
  
public DataSource dataSource() {
  
DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
  
dataSource.setUrl("jdbc:mysql://localhost:3306/yourdatabase");
  
dataSource.setUsername("yourusername");
  
dataSource.setPassword("yourpassword");
  
return dataSource;
  
}

@Bean
  
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
  
return new JdbcTemplate(dataSource);
  
}
  
}
  

使用JdbcTemplate进行数据库操作

最后,在你的服务类中使用JdbcTemplate进行数据库操作。

  
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 java.sql.ResultSet;
  
import java.sql.SQLException;
  
import java.util.List;

@Service
  
public class UserService {

@Autowired
  
private JdbcTemplate jdbcTemplate;

public List<User> getAllUsers() {
  
String sql = "SELECT * FROM users";
  
return jdbcTemplate.query(sql, new UserRowMapper());
  
}

private static final class UserRowMapper implements RowMapper<User> {
  
@Override
  
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
  
User user = new User();
  
user.setId(rs.getInt("id"));
  
user.setName(rs.getString("name"));
  
user.setEmail(rs.getString("email"));
  
return user;
  
}
  
}
  
}
  

2. 使用Spring Data JPA

Spring Data JPA简化了JPA基于Hibernate等实现的持久层开发。

配置数据源

application.propertiesapplication.yml中配置数据源。

  
# application.properties
  
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
  
spring.datasource.username=yourusername
  
spring.datasource.password=yourpassword
  
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  
spring.jpa.hibernate.ddl-auto=update
  
spring.jpa.show-sql=true
  

创建实体类

创建一个与数据库表对应的实体类。

  
import javax.persistence.Entity;
  
import javax.persistence.GeneratedValue;
  
import javax.persistence.GenerationType;
  
import javax.persistence.Id;

@Entity
  
public class User {

@Id
  
@GeneratedValue(strategy = GenerationType.IDENTITY)
  
private Long id;
  
private String name;
  
private String email;

// getters and setters
  
}
  

创建Repository接口

创建一个继承自JpaRepository的Repository接口。

  
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
  
}
  

使用Repository接口

在服务类中使用UserRepository进行数据库操作。

  
import org.springframework.beans.factory.annotation.Autowired;
  
import org.springframework.stereotype.Service;

import java.util.List;

@Service
  
public class UserService {

@Autowired
  
private UserRepository userRepository;

public List<User> getAllUsers() {
  
return userRepository.findAll();
  
}
  
}
  

3. 使用Spring Data JDBC

Spring Data JDBC是另一种简化数据库访问的方法。

配置数据源

同样,在application.propertiesapplication.yml中配置数据源。

  
# application.properties
  
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
  
spring.datasource.username=yourusername
  
spring.datasource.password=yourpassword
  
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  
spring.data.jdbc.repositories.enabled=true
  

创建实体类

创建一个与数据库表对应的实体类。

  
import org.springframework.data.annotation.Id;
  
import org.springframework.data.relational.core.mapping.Table;

@Table("users")
  
public class User {

@Id
  
private Long id;
  
private String name;
  
private String email;

// getters and setters
  
}
  

创建Repository接口

创建一个继承自CrudRepository的Repository接口。

  
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {
  
}
  

使用Repository接口

在服务类中使用UserRepository进行数据库操作。

  
import org.springframework.beans.factory.annotation.Autowired;
  
import org.springframework.stereotype.Service;

import java.util.List;

@Service
  
public class UserService {

@Autowired
  
private UserRepository userRepository;

public List<User> getAllUsers() {
  
return (List<User>) userRepository.findAll();
  
}
  
}