Springboot獲取前端反饋信息并存入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
導(dǎo)入mybatis依賴(lài)
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
yml實(shí)現(xiàn)mybatis依賴(lài)
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/yanan_user #寫(xiě)自己的數(shù)據(jù)庫(kù)名 username: root password: 123456 #自己的賬號(hào)密碼 mybatis: type-aliases-package: com.wjr.pojo configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
編寫(xiě)前端代碼
自行導(dǎo)入jQuery包,并調(diào)用
(form表單)
<form> <input type="text" name="name" placeholder="請(qǐng)輸入您的姓名" required=""> <input type="email" name="email" placeholder="請(qǐng)輸入您的郵箱" required=""> <input type="text" name="telephone" placeholder="請(qǐng)輸入您的聯(lián)系方式" required=""> <textarea name="message" placeholder="請(qǐng)您提出您的寶貴建議,我們將認(rèn)真閱讀" required=""></textarea> <input class="btn1" type="button" value="提交" onclick="send(this.form)"> </form>
(ajax請(qǐng)求)
<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>
編寫(xiě)數(shù)據(jù)庫(kù)信息
@Data //這里導(dǎo)入了lombok依賴(lài)
@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;
}
編寫(xiě)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);
}
編寫(xiě)接口(service層)
public interface FeedbackService {
int addFeedback(String name, String email, String telephone,String message);
}
編寫(xiě)接口實(shí)現(xiàn)(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);
}
}
接收信息并存入數(shù)據(jù)庫(kù)(controller層)
@Autowired
FeedbackServiceImpl feedbackServiceImpl;
@RequestMapping(value = "/jsonfb") //和ajax請(qǐng)求中url相對(duì)應(yīng)
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完整依賴(lài)
<?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>
注:一個(gè)簡(jiǎn)單的實(shí)現(xiàn)獲取前端反饋信息存入數(shù)據(jù)庫(kù)操作,自行嘗試,如果有報(bào)錯(cuò),請(qǐng)看注解是否正確,還可能存在各種版本問(wèn)題;
到此這篇關(guān)于Springboot獲取前端反饋信息并存入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Springboot數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
以用戶(hù)名注冊(cè)為例分析三種Action獲取數(shù)據(jù)的方式
這篇文章主要介紹了以用戶(hù)名注冊(cè)為例分析三種Action獲取數(shù)據(jù)的方式的相關(guān)資料,需要的朋友可以參考下2016-03-03
SpringBoot整合mybatis-plus快速入門(mén)超詳細(xì)教程
mybatis-plus 是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,本文給大家分享SpringBoot整合mybatis-plus快速入門(mén)超詳細(xì)教程,一起看看吧2021-09-09
springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼
阿里云推出了直傳,本文主要介紹了springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
Java 數(shù)組轉(zhuǎn)List的四種方式小結(jié)
本文主要介紹了四種將Java數(shù)組轉(zhuǎn)換為L(zhǎng)ist的方法,包括使用Arrays.asList、ArrayList構(gòu)造器、Collections.addAll以及JDK8的Stream,具有一定的參考價(jià)值,感興趣的可以了解一下2024-10-10
Elasticsearch Join字段類(lèi)型簡(jiǎn)單快速上手教程
這篇文章主要為大家介紹了Elasticsearch Join字段類(lèi)型簡(jiǎn)單快速上手教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)計(jì)全局接口訪問(wèn)次數(shù)詳解
這篇文章主要介紹了SpringBoot通過(guò)AOP實(shí)現(xiàn)對(duì)全局接口訪問(wèn)次數(shù)的統(tǒng)計(jì),文章從相關(guān)問(wèn)題展開(kāi)全文內(nèi)容詳情,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
關(guān)于FastJson?long?溢出問(wèn)題的小結(jié)
這篇文章主要介紹了關(guān)于FastJson?long?溢出問(wèn)題的小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-01-01

