您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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如何獲取前端反饋信息并存入數據庫”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。