SSM获取表单数据插入数据库并返回插入记录的ID值

以下指示插入操作以及获取记录值的ID的部分操作代码!!!

首先是简单的表单实现

 1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 6     <title>Insert title here</title>
 7 </head>
 8 <body>
 9 <form action="${pageContext.request.contextPath}/user.do" method="POST">
10     <label for="username">
11         username:
12         <input type="text" id="username" name="username"/>
13     </label>
14     <!-- label的作用是包含相关的文本框 -->
15     <label for="password">
16         password:
17         <input type="text" id="password" name="password"/>
18     </label>
19 
20     <label for="birthday">
21         birthday:
22         <input type="text" id="birthday" name="birthday"/>
23     </label>
24 
25     <label for="gender">
26         gender:
27         <input type="text" id="gender" name="gender"/>
28     </label>
29 
30     <button>提交</button>
31 </form>
32 </body>
33 </html>

User类为:

 1 package com.example.edu.model;
 2 
 3 public class User {
 4     private int id;
 5     private String username;
 6     private String password;
 7     private String birthday;
 8     private String gender;
 9 
10     public int getId() {
11         return id;
12     }
13 
14     public void setId(int id) {
15         this.id = id;
16     }
17 
18     public String getPassword() {
19         return password;
20     }
21 
22     public void setPassword(String password) {
23         this.password = password;
24     }
25 
26     public String getUsername() {
27         return username;
28     }
29 
30     public void setUsername(String username) {
31         this.username = username;
32     }
33 
34     public String getBirthday() {
35         return birthday;
36     }
37 
38     public void setBirthday(String birthday) {
39         this.birthday = birthday;
40     }
41 
42     public String getGender() {
43         return gender;
44     }
45 
46     public void setGender(String gender) {
47         this.gender = gender;
48     }
49 
50     @Override
51     public String toString() {
52         return "User{" +
53                 "id=" + id +
54                 ", username='" + username + '\'' +
55                 ", password='" + password + '\'' +
56                 ", birthday='" + birthday + '\'' +
57                 ", gender='" + gender + '\'' +
58                 '}';
59     }
60 }

 

获取表单数据的方式有:

1.直接在controller层的方法参数中写入表单的参数(也就是表单的name?)

1  @RequestMapping(value = "signin", method = RequestMethod.POST)
2     public String loging(@RequestParam(value = "username") String username, String password) {
3         System.out.println("username:" + username);
4         System.out.println("password:" + password);
5         return "redirect:success.do"; 
6     }

注意到上面使用到了  @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)来接收

1  @RequestMapping(value = "user", method = RequestMethod.POST)
2     public String getUser(@ModelAttribute User users) {
3 
4         System.out.println(users);  //此时只是获取到了表单的输入数据,尚未将数据插入数据库,因此得到的id默认为0
5         int id = userService.insert(users);  //插入数据库并返回记录的id值
6         System.out.println("The id is:" + id);
7         return "redirect:success.do";
8     }

 

注意方法参数中的 @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值的这个操作。)

 

作个记录,以防自己某天忘了!

 

转载于:https://www.cnblogs.com/Guhongying/p/10693994.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值