这篇文章给大家分享的是有关Springboot如何获取前端反馈信息并存入数据库的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
导入mybatis依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
yml实现mybatis依赖
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/yanan_user #写自己的数据库名
username: root
password: 123456 #自己的账号密码
mybatis:
type-aliases-package: com.wjr.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
自行导入jQuery包,并调用
(form表单)
<form>
<input type="text" name="name" placeholder="请输入您的姓名" required="">
<input type="email" name="email" placeholder="请输入您的邮箱" required="">
<input type="text" name="telephone" placeholder="请输入您的联系方式" required="">
<textarea name="message" placeholder="请您提出您的宝贵建议,我们将认真阅读" required=""></textarea>
<input class="btn1" type="button" value="提交" onclick="send(this.form)">
</form>
(ajax请求)
<script>
function send(fdform){
alert(fdform);
var fdj = {name:fdform.name.value,email:fdform.email.value,telephone:fdform.telephone.value,message:fdform.message.value};
$.ajax({
url:"jsonfb",
data:fdj,
contentType:"application/json",
type:"GET"
})
}
</script>
编写数据库信息
@Data //这里导入了lombok依赖
@Table(name = "feedback")
public class Feedback {
@Id
//主键回填
@KeySql(useGeneratedKeys = true)
private int id;
private String name;
private String email;
private String telephone;
private String message;
}
编写insert方法(mapper层接口)
@Repository
public interface FeedbackMapper {
@Insert({"insert into feedback(name,email,telephone,message) values('${feedback.name}','${feedback.email}','${feedback.telephone}','${feedback.message}')"})
int add(@Param("feedback") Feedback feedback);
}
编写接口(service层)
public interface FeedbackService {
int addFeedback(String name, String email, String telephone,String message);
}
编写接口实现(serviceImpl层)
@Service
public class FeedbackServiceImpl implements FeedbackService{
@Autowired
private FeedbackMapper feedbackMapper;
@Override
public int addFeedback(String name, String email, String telephone,String message){
Feedback fb = new Feedback();
fb.setName(name);
fb.setMessage(message);
fb.setTelephone(telephone);
fb.setEmail(email);
return feedbackMapper.add(fb);
}
}
接收信息并存入数据库(controller层)
@Autowired
FeedbackServiceImpl feedbackServiceImpl;
@RequestMapping(value = "/jsonfb") //和ajax请求中url相对应
public String json(Feedback feedback){
System.out.println(feedback);
int f = feedbackServiceImpl.addFeedback(feedback.getName(), feedback.getEmail(), feedback.getTelephone(), feedback.getMessage());
return "contact";
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wjr</groupId>
<artifactId>yanan</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>yanan</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</build>
</project>
注:一个简单的实现获取前端反馈信息存入数据库操作,自行尝试,如果有报错,请看注解是否正确,还可能存在各种版本问题;
感谢各位的阅读!关于“Springboot如何获取前端反馈信息并存入数据库”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。