SSM获取表单数据插入数据库并返回插入记录的ID值
以下指示插入操作以及获取记录值的ID的部分操作代码!!!
首先是简单的表单实现
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user.do" method="POST">
<label for="username">
username:
<input type="text" id="username" name="username"/>
</label>
<!-- label的作用是包含相关的文本框 -->
<label for="password">
password:
<input type="text" id="password" name="password"/>
</label> <label for="birthday">
birthday:
<input type="text" id="birthday" name="birthday"/>
</label> <label for="gender">
gender:
<input type="text" id="gender" name="gender"/>
</label> <button>提交</button>
</form>
</body>
</html>
User类为:
package com.example.edu.model; public class User {
private int id;
private String username;
private String password;
private String birthday;
private String gender; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getBirthday() {
return birthday;
} public void setBirthday(String birthday) {
this.birthday = birthday;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday='" + birthday + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
获取表单数据的方式有:
1.直接在controller层的方法参数中写入表单的参数(也就是表单的name?)
@RequestMapping(value = "signin", method = RequestMethod.POST)
public String loging(@RequestParam(value = "username") String username, String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "redirect:success.do";
}
注意到上面使用到了 @RequestParam(value = "xxx"),这是用来解决当表单的参数和controller层方法的参数命名不一致的情况,比如表单的username的表单的name不再命名为username ,而是命名为myname,这时如果直接使用
public String loging( String username, String password)
的话就会出错,可以使用@RequestParam(value = "xxx")来解决这个问题,如下,就是说将表单上myname获得的数据传递到username上来?(好吧,我不知道怎样表达好一点!)
public String loging(@RequestParam(value = "myname") String username, String password)
2.通过一个实体类(或者说bean)来接收
@RequestMapping(value = "user", method = RequestMethod.POST)
public String getUser(@ModelAttribute User users) { System.out.println(users); //此时只是获取到了表单的输入数据,尚未将数据插入数据库,因此得到的id默认为0
int id = userService.insert(users); //插入数据库并返回记录的id值
System.out.println("The id is:" + id);
return "redirect:success.do";
}
注意方法参数中的 @ModelAttribute User users 。
3.还有其他的方法,自己没有实现,推荐一篇博客:https://www.cnblogs.com/yyxyqh/p/7749990.html
接下来就是获取插入数据库的记录的id了。(关于已成功获取数据但在插入数据库失败可以点这里)
@Override
public int insert(User user) {
userMapper.insert(user);
return user.getId(); //返回插入后的id值
}
大概就是要在你具体的insert方法中,执行完后要使用getId来获取插入记录的id(每个人定义的接口不同,所以抱歉只放出上面的几行代码,不过我觉得核心是在你定义的insert方法中使用类似于 user.getId(); 来获取id值的这个操作。)
作个记录,以防自己某天忘了!
SSM获取表单数据插入数据库并返回插入记录的ID值的更多相关文章
- 解决ssm项目表单数据提交到数据库乱码问题
问题:在ssm整合的项目里,从前台页面获取表单数据存到数据库中乱码 先说解决办法然后分析:问题出在form表单的提交方式上,我的web.xml配置过滤器都已经指定了编码集,为什么没有生效?原因是,对于 ...
- Struts2封装获取表单数据方式
一.属性封装 1.创建User实体类` package cn.entity; public class User { private String username; private String p ...
- strus2中获取表单数据 两种方式 属性驱动 和模型驱动
strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...
- 初学者易上手的SSH-struts2 02Action获取表单数据-通配符
在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符. 第一步,在WebContent下面新建一个log ...
- Action 中获取表单数据的三种方式
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905 冷血之心的博客) Action 中获取表单提交数据 ...
- Struts2_day02--Struts2封装获取表单数据方式
Struts2封装获取表单数据方式 原始方式获取表单封装到实体类对象 属性封装(会用) 1 直接把表单提交属性封装到action的属性里面 2 实现步骤 (1)在action成员变量位置定义变量 - ...
- MyBatis插入数据之后返回插入记录的id
MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...
- 让 Python 的1、数据库查询返回字典记录--- 2、利用zip函数将两个列表(list)组成字典(dict)
让 Python 的数据库查询返回字典记录: https://yanbin.blog/python-database-query-return-dictionary-result/#more-9179 ...
- C# 批量插入表SQLSERVER SqlBulkCopy往数据库中批量插入数据
#region 帮助实例:SQL 批量插入数据 多种方法 /// <summary> /// SqlBulkCopy往数据库中批量插入数据 /// </summary> /// ...
随机推荐
- String字面量
public class assa{ static String ee = "aa";//ee指向常量池中的aa static String ff = new String(&qu ...
- HTTPclient 4.2.2 传参数和文件流
package com.http; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodin ...
- awt多线程聊天
public class ChatServer { boolean started = false; ServerSocket serverSocket = null; public void sta ...
- kafka7 探索生产者同步or异步发送消息
1.生产者:在发送完消息后,收到回执确认. 主要是在SimpleProducer.java中修改了发送消息的2行代码,用到了回调函数,修改如下: //发送消息 ProducerRecord<St ...
- 110A
#include <stdio.h> #include<string.h> #define MAXSIZE 30 int main() { char digits[30]; m ...
- ORA-00444: background process DBRM failed while starting
SQL> startup 报错:ORA-00444: background process DBRM failed while startingORA-00020:maximum number ...
- pip 更改国内镜像
2 pip 更改国内镜像 pip 默认不使用国内镜像,但是我们可以自己设置 -[pypi 镜像使用帮助] 临时使用 pip install -i https://pypi.tuna.tsinghua. ...
- webpack 常用命令
1 初始化package.json npm init -y 2 全局安装webpack npm install webpack -g 3 安装webpack依赖 npm install webpack ...
- C# Newtonsoft.Json JsonSerializerSettings 全局序列化设置
Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings(); JsonC ...
- C# AsyncCallback异步回调用法示例
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...