boot spring 插入一条数据_SpringBoot-Insert

本文详细介绍了Spring Boot连接数据库并插入数据的过程。包括创建数据库表、新建项目、配置数据库连接信息,还分别创建了entity、dao、service、controller层的代码,最后完成项目搭建并进行测试运行,通过Postman验证数据插入是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot-Insert

SpringBoot连接数据库并插入数据

创建数据库表

在test数据库下新建test表如下,有id和name两个属性,其中id设为自增:

新建项目

Eclipse中创建Maven Project,输入相应的包名及项目名,这里的项目名是Insert,包名为com,接着使用如下代码替换pom.xml里面的内容:

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">

4.0.0

1.0.0

dms-get-ecall

0.0.1-SNAPSHOT

jar

springboot-restful

http://maven.apache.org

UTF-8

1.7

1.2.0

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-devtools

true

org.springframework.boot

spring-boot-starter-data-jpa

org.mybatis.spring.boot

mybatis-spring-boot-starter

${mybatis-spring-boot}

mysql

mysql-connector-java

org.springframework.boot

spring-boot-maven-plugin

true

在src/main下新建resources文件夹,新建application.properties文件,将如下内容复制并修改相关信息,主要内容是数据库的连接配置,包括url、数据库用户名、密码和之前创建的数据库表名:

banner.charset=UTF-8

server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

spring.http.encoding.force=true

spring.messages.encoding=UTF-8

server.port=8083

spring.datasource.url=jdbc:mysql://数据库地址:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false

spring.datasource.username=数据库用户名

spring.datasource.password=数据库密码

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

将com.Insert下的App.java代码更新如下:

package com.Select;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* Hello world!

*

*/

@SpringBootApplication

public class App

{

public static void main( String[] args )

{

SpringApplication.run(App.class, args);

System.out.println( "Hello World!" );

}

}

创建后的项目结构如下:

完成之后右键项目,选择Maven/Update Projects更新项目。

创建项目

entity层

在src/main/java下新建entity包,创建User实体类,该实体类封装的变量对应test数据库表中的属性,代码如下:

package com.Insert.entity;

//创建test表对应的实体类

public class User {

private int id;

private String name;

public User(String name2) {

// TODO Auto-generated constructor stub

this.name = name2;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

dao层

在src/main/java下新建dao包,新建UserDao接口,实现对数据库的插入操作:

package com.Insert.dao;

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

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

import com.Insert.entity.User;

//交给springboot管理的注解

@Repository

public class UserDao {

@Autowired

private JdbcTemplate jdbcTemplate;

public void Insert(User user) {

// TODO Auto-generated method stub

jdbcTemplate.update("insert into test(name) values(?)", user.getName());

}

}

service层

在src/main/java下新建service包,新建UserService接口,添加要用到的方法:

package com.Insert.service;

import com.Insert.entity.User;

public interface UserService {

void Insert(User user);

}

在src/main/java下新建serviceimpl包,新建UserServiceImpl类来实现接口,重写UserService中声明的方法:

package com.Insert.serviceimpl;

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

import org.springframework.stereotype.Service;

import com.Insert.dao.UserDao;

import com.Insert.entity.User;

import com.Insert.service.UserService;

@Service

public class UserServiceImpl implements UserService{

@Autowired

private UserDao userDao;

@Override

public void Insert(User user) {

// TODO 自动生成的方法存根

userDao.Insert(user);

}

}

controller层

在src/main/java下新建controller包,新建UserController类来实现调用:

package com.Insert.controller;

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

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.Insert.entity.User;

import com.Insert.serviceimpl.UserServiceImpl;

@RestController

public class UserController {

@Autowired

private UserServiceImpl userservice;

@RequestMapping("/insert")

public String Insert(String name) {

User user = new User(name);

userservice.Insert(user);

return "insert successfully!";

}

}

至此,整个项目已搭建完毕,整体的框架如下:

测试运行

右键App.java,选择运行方式-Java应用程序,如果控制台会出现Hello World!则表明映射成功,并且能正常运行。然后打开postman设置相关信息如下,点击send之后会成功显示insert successfully!:

接着去数据库查看数据是否成功插入(运行了好多次):

OVER!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值