Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据测试

最简单的springbootjdbc插入数据

jdbc插入数据库做测试数据,想上网抄下作业,结果都是乱七八糟的,决定自己写一篇最简单的jdbc给大家用用

1、新建springboot工程,什么都不用选,直接下一步下一步直到完成

2、导入包

只需要新加入两个包就行了

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

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

3、配置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sakila?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=zy5564@qq.com

4、测试

注意路径一定要正确是在test目录下测试

package com.example.springbootjdbc;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;


@SpringBootTest
class SpringbootjdbcApplicationTests {

    @Autowired    // 自动注入,spring boot会帮我们实例化一个对象
    private JdbcTemplate jdbcTemplate;

    @Test
    void contextLoads() {
    }
    
    @Test
    public void inser() {
        for (int i = 0; i < 10000; i++) {
            String sql = "insert into employees(id,fname,lname,hired,separated,job_code,store_id)  values (1,'zhangsan','lisi',now(),now(),1,100000000)";
            jdbcTemplate.execute(sql);
            System.out.println("成功插入"+i+"条数据");
        }

    }

}

完成...

执行..

成功..

增删改查都行~

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: python查找算法第一弹:顺序查找

下一篇: 不用再找懒人包了 | Windows安装苹果系统(dmg)原版

精华推荐