SpringBoot连接MySQL设置

本文详细介绍了如何在SpringBoot项目中配置并使用MySQL数据库。包括pom.xml文件中引入jdbc和MySQL连接依赖,application.properties或application.yml配置数据源信息,以及通过SpringBoot测试类进行功能验证。

pom.xml文件配置

  • 引入jdbc支持
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
  • 引入MySQL连接依赖包
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  • src/main/resources/application.properties配置数据源信息
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

其中test为数据库名称,3306为默认的端口号。

  • src/main/resources/application.yml配置数据源信息
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf8
    username: root
    password: ljf123456

二者选其一。

使用SpringBoot测试类进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SpringbootTestApplicationTests {
    @Autowired
    private UserService userService;

    @Before
    public void setUp() {
        userService.deleteAllUsers();
        System.out.println("清空user表");
    }

    @Test
    public void test() throws Exception {
        System.out.println("插入5个用户");
        userService.create("a", 1);
        userService.create("b", 2);
        userService.create("c", 3);
        userService.create("d", 4);
        userService.create("e", 5);
        // 查数据库,应该有5个用户
        Assert.assertEquals(5, userService.getAllUsers().intValue());
        // 删除两个用户
        userService.deleteByName("a");
        userService.deleteByName("e");
        // 查数据库,应该有5个用户
        Assert.assertEquals(3, userService.getAllUsers().intValue());
    }
}

UserService类实现

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(String name, Integer age) {
        jdbcTemplate.update("insert into USER(NAME, AGE ) values(?, ?)", name, age);
    }

    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from USER where NAME = ?", name);
    }

    @Override
    public Integer getAllUsers() {
        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
    }

    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from USER");
    }

UserService 接口

public interface UserService {
    void create(String name, Integer age);
    void deleteByName(String name);
    Integer getAllUsers();
    void deleteAllUsers();
}

在命令行查询的结果

asd.png

### 如何在Spring Boot中配置和连接MySQL数据库 #### 配置数据源属性 为了使Spring Boot应用能够访问MySQL数据库,需编辑`application.properties`或`application.yml`文件来指定必要的数据库连接参数。具体设置如下所示: 对于`application.properties`文件: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your-database?useSSL=false&serverTimezone=UTC spring.datasource.username=your-username spring.datasource.password=your-password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 而对于采用YAML格式的应用程序,则应按照以下方式编写配置项[^3]。 #### 连接池配置选项 默认情况下,Spring Boot选用HikariCP作为其内置的数据源实现工具;不过也支持其他类型的连接池组件,比如Tomcat JDBC Pool或是Apache DBCP等替代方案。针对这些不同的连接池解决方案,可以自定义一系列关于性能调优方面的参数,例如最大活跃连接数目、最小闲置连接数量等等[^2]。 #### 使用Actuator监控连接状态 借助于Spring Boot Actuator模块所提供的端点功能,开发者可以获得有关当前运行环境中JDBC连接池状况的相关统计信息,这有助于及时发现潜在的问题并作出相应的优化措施[^1]。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 上述代码展示了最基础的Spring Boot启动类结构,在此基础上完成对MySQL的支持只需简单几步即可达成目标。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值