微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Springboot获取前端反馈信息并存入数据库的实现代码

这篇文章主要介绍了Springboot获取前端反馈信息并存入数据库的实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

导入mybatis依赖

org.mybatis.spring.bootmybatis-spring-boot-starter2.0.1

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表单)

(ajax请求)

编写数据库信息

@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完整依赖

4.0.0org.springframework.bootspring-boot-starter-parent2.4.3com.wjryanan0.0.1-SNAPSHOTyananDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestcom.alibabadruid1.1.6org.springframework.bootspring-boot-configuration-processortrueorg.mybatis.spring.bootmybatis-spring-boot-starter2.0.1org.projectlomboklombokMysqLmysql-connector-java8.0.23tk.mybatismapper-spring-boot-starter2.1.5junitjunittestorg.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-maven-pluginorg.apache.maven.pluginsmaven-resources-plugin2.4.3

注:一个简单的实现获取前端反馈信息存入数据库操作,自行尝试,如果有报错,请看注解是否正确,还可能存在各种版本问题;

到此这篇关于Springboot获取前端反馈信息并存入数据库的实现代码文章就介绍到这了,更多相关Springboot数据库内容搜索编程之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程之家!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐