数据更新不了
92875 INFO [http-nio-8080-exec-12] com.damu.servlet.UsersFindByIdServlet - 获取到查询参数id--》6 92875 DEBUG [http-nio-8080-exec-12] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection 92875 DEBUG [http-nio-8080-exec-12] org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 846707034 from pool. 92875 DEBUG [http-nio-8080-exec-12] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 92875 DEBUG [http-nio-8080-exec-12] com.damu.entity.Users.findUsers - ==> Preparing: select * from users where id=? 92875 DEBUG [http-nio-8080-exec-12] com.damu.entity.Users.findUsers - ==> Parameters: 6(Integer) 92883 DEBUG [http-nio-8080-exec-12] com.damu.entity.Users.findUsers - <== Total: 1 92883 DEBUG [http-nio-8080-exec-12] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 92883 DEBUG [http-nio-8080-exec-12] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 92883 DEBUG [http-nio-8080-exec-12] org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 846707034 to pool. 92883 INFO [http-nio-8080-exec-12] com.damu.servlet.UsersFindByIdServlet - 查询数完成,查询到的数据:Users{id=6, username='imooc1', userpass='2121', nickname='summer_eye', age=12121, gender='12121', phone='121212', email='337806034@qq.com', createTime=Sat May 19 18:08:14 CST 2018, updateTime=Sat May 19 18:08:14 CST 2018, lastLogin=Sat May 19 18:08:14 CST 2018, userStatus=0, remark='null'} org.apache.ibatis.exceptions.PersistenceException: 113051 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection ### Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1 ### The error may involve defaultParameterMap 113051 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 846707034 from pool. 113051 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 113051 DEBUG [http-nio-8080-exec-13] com.damu.entity.Users.updateUser - ==> Preparing: update users SET nickname = ?, age = ?, gender = ?, email = ?, phone = ?, updateTime = ?, remark = ? where id = ? ### The error occurred while setting parameters 113051 DEBUG [http-nio-8080-exec-13] com.damu.entity.Users.updateUser - ==> Parameters: summer_eye(String), 11(Integer), 222(String), 333(String), 337806034@qq.com(String), 2018-05-21 15:56:52.905(Timestamp), 大木木(String), 6(Integer) 113059 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Rolling back JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] ### SQL: update users SET nickname = ?, age = ?, gender = ?, email = ?, phone = ?, updateTime = ?, remark = ? where id = ? 113059 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] ### Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1 113059 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) 113059 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 846707034 to pool. at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200) at com.damu.dao.UserDAO.updateUsers(UserDAO.java:72) at com.damu.servlet.UsersUpdateServlet.doPost(UsersUpdateServlet.java:35) at com.damu.servlet.UsersUpdateServlet.doGet(UsersUpdateServlet.java:19) at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 113067 INFO [http-nio-8080-exec-14] com.damu.servlet.UsersFindByIdServlet - 获取到查询参数id--》6 113067 DEBUG [http-nio-8080-exec-14] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection 113067 DEBUG [http-nio-8080-exec-14] org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 846707034 from pool. 113067 DEBUG [http-nio-8080-exec-14] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) 113067 DEBUG [http-nio-8080-exec-14] com.damu.entity.Users.findUsers - ==> Preparing: select * from users where id=? at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 113067 DEBUG [http-nio-8080-exec-14] com.damu.entity.Users.findUsers - ==> Parameters: 6(Integer) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 113067 DEBUG [http-nio-8080-exec-14] com.damu.entity.Users.findUsers - <== Total: 1 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 113067 DEBUG [http-nio-8080-exec-14] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 113067 DEBUG [http-nio-8080-exec-14] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 113067 DEBUG [http-nio-8080-exec-14] org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 846707034 to pool. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 113067 INFO [http-nio-8080-exec-14] com.damu.servlet.UsersFindByIdServlet - 查询数完成,查询到的数据:Users{id=6, username='imooc1', userpass='2121', nickname='summer_eye', age=12121, gender='12121', phone='121212', email='337806034@qq.com', createTime=Sat May 19 18:08:14 CST 2018, updateTime=Sat May 19 18:08:14 CST 2018, lastLogin=Sat May 19 18:08:14 CST 2018, userStatus=0, remark='null'} at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1354) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960) at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:388) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) at com.sun.proxy.$Proxy7.execute(Unknown Source) at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46) at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198) ... 27 more 134753 INFO [http-nio-8080-exec-8] com.damu.servlet.UsersFindByIdServlet - 获取到查询参数id--》1 134753 DEBUG [http-nio-8080-exec-8] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection 134753 DEBUG [http-nio-8080-exec-8] org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 846707034 from pool. 134753 DEBUG [http-nio-8080-exec-8] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 134753 DEBUG [http-nio-8080-exec-8] com.damu.entity.Users.findUsers - ==> Preparing: select * from users where id=? 134753 DEBUG [http-nio-8080-exec-8] com.damu.entity.Users.findUsers - ==> Parameters: 1(Integer) 134753 DEBUG [http-nio-8080-exec-8] com.damu.entity.Users.findUsers - <== Total: 1 134753 DEBUG [http-nio-8080-exec-8] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 134753 DEBUG [http-nio-8080-exec-8] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 134753 DEBUG [http-nio-8080-exec-8] org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 846707034 to pool. 134761 INFO [http-nio-8080-exec-8] com.damu.servlet.UsersFindByIdServlet - 查询数完成,查询到的数据:Users{id=1, username='damu', userpass='damu', nickname='大慕', age=null, gender='null', phone='null', email='null', createTime=Tue May 16 00:00:00 CST 2017, updateTime=null, lastLogin=null, userStatus=0, remark='null'} 171626 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection 171626 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 846707034 from pool. 171626 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171634 DEBUG [http-nio-8080-exec-17] com.damu.entity.Users.updateUser - ==> Preparing: update users SET nickname = ?, age = ?, gender = ?, email = ?, phone = ?, updateTime = ?, remark = ? where id = ? 171634 DEBUG [http-nio-8080-exec-17] com.damu.entity.Users.updateUser - ==> Parameters: 大慕(String), 21(Integer), 男(String), 123456(String), 123@123.com(String), 2018-05-21 15:57:51.48(Timestamp), 大木木(String), 1(Integer) 171642 DEBUG [http-nio-8080-exec-17] com.damu.entity.Users.updateUser - <== Updates: 1 171642 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171706 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171706 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171706 DEBUG [http-nio-8080-exec-17] org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 846707034 to pool. 171714 INFO [http-nio-8080-exec-13] com.damu.servlet.UsersFindByIdServlet - 获取到查询参数id--》1 171714 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection 171714 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 846707034 from pool. 171714 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171714 DEBUG [http-nio-8080-exec-13] com.damu.entity.Users.findUsers - ==> Preparing: select * from users where id=? 171714 DEBUG [http-nio-8080-exec-13] com.damu.entity.Users.findUsers - ==> Parameters: 1(Integer) 171722 DEBUG [http-nio-8080-exec-13] com.damu.entity.Users.findUsers - <== Total: 1 171722 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171722 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3277b95a] 171722 DEBUG [http-nio-8080-exec-13] org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 846707034 to pool. 171722 INFO [http-nio-8080-exec-13] com.damu.servlet.UsersFindByIdServlet - 查询数完成,查询到的数据:Users{id=1, username='damu', userpass='damu', nickname='大慕', age=21, gender='男', phone='123@123.com', email='123456', createTime=Tue May 16 00:00:00 CST 2017, updateTime=Mon May 21 15:57:51 CST 2018, lastLogin=null, userStatus=0, remark='大木木'}
UsersUpdateServlet
package com.damu.servlet; import com.damu.dao.UserDAO; import com.damu.entity.Users; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Date; @WebServlet("/updateusers") public class UsersUpdateServlet extends HttpServlet { UserDAO userDAO=new UserDAO(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取用户要更新的数据 String id=req.getParameter("id"); String nickname=req.getParameter("nickname"); String age=req.getParameter("age"); String gender=req.getParameter("gender"); String email=req.getParameter("email"); String phone=req.getParameter("phone"); String remark=req.getParameter("remark"); //创建用户对象 Users user=new Users(Integer.parseInt(id),nickname,Integer.parseInt(age),gender,email,phone,new Date(),remark); //提交更新 userDAO.updateUsers(user); //提交更新后的用户数据 resp.sendRedirect("/detail?id="+user.getId()); } }
UserDAO
/** * 增加一个新用户数据到数据库的方法 */ public Users updateUsers(Users user) { try { //返回值:是insert执行过程中影响的行数 getSession().update("updateUser",user); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return user; }
UsersFindByIdServlet
package com.damu.servlet; import com.damu.dao.UserDAO; import com.damu.entity.Users; import org.apache.log4j.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/detail") public class UsersFindByIdServlet extends HttpServlet { /** * 创建对应的日志记录对象 * 通过不同的级别进行日志的记录【DEBUG\WARM\INFO\LOG】 */ private Logger log=Logger.getLogger(UsersFindByIdServlet.class); private UserDAO userDAO = new UserDAO(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); log.info("获取到查询参数id--》"+id); Users users = userDAO.findById(Integer.parseInt(id)); log.info("查询数完成,查询到的数据:"+users); req.setAttribute("user", users); req.getRequestDispatcher("detail.jsp").forward(req, resp); } }
Users
package com.damu.entity; import java.util.Date; /** * Created by mouwe on 2017/5/17. * resource:mybatis-config.xml * InputStream * SqlSessionFactory * SqlSession * 执行我们的配置好的SQL语句 */ public class Users { private Integer id; // 用户编号 // private String username; // 登录账号 private String name; //登陆账号 private String userpass; // 登录密码 private String nickname; // 用户昵称 private Integer age; // 用户年龄 private String gender; // 用户性别 private String phone; // 联系方式 private String email; // 用户邮箱 private Date createTime; // 创建时间 private Date updateTime; // 账号最后修改时间 private Date lastLogin; // 用户最后登录时间 private Integer userStatus; // 用户账号状态 0 正常 1 锁定 2 删除 private String remark; // 用户备注信息 public Users() { } public Users(Integer id) { this.id = id; } public Users(Integer id, String nickname, Integer age, String gender, String phone, String email, Date updateTime, String remark) { this.id = id; this.nickname = nickname; this.age = age; this.gender = gender; this.phone = phone; this.email = email; this.updateTime = updateTime; this.remark = remark; } public Users(Integer id, String name, String userpass, String nickname, Integer age, String gender, String phone, String email, Date createTime, Date updateTime, Date lastLogin, String remark, Integer userStatus) { this.id = id; // this.username = username; this.name = name; this.userpass = userpass; this.nickname = nickname; this.age = age; this.gender = gender; this.phone = phone; this.email = email; this.createTime = createTime; this.updateTime = updateTime; this.lastLogin = lastLogin; this.userStatus = userStatus; this.remark = remark; } public Users(String name, String userpass, String nickname, Integer age, String gender, String email, String phone, Date createTime, Date updateTime, Date lastLogin, Integer userStatus) { this.name = name; this.userpass = userpass; this.nickname = nickname; this.age = age; this.gender = gender; this.phone = phone; this.email = email; this.userStatus = userStatus; this.createTime = createTime; this.updateTime = updateTime; this.lastLogin = lastLogin; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } // public String getUsername() { //// return username; //// } //// //// public void setUsername(String username) { //// this.username = username; //// } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Date getLastLogin() { return lastLogin; } public void setLastLogin(Date lastLogin) { this.lastLogin = lastLogin; } public Integer getUserStatus() { return userStatus; } public void setUserStatus(Integer userStatus) { this.userStatus = userStatus; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "Users{" + "id=" + id + ", username='" + name + '\'' + ", userpass='" + userpass + '\'' + ", nickname='" + nickname + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", phone='" + phone + '\'' + ", email='" + email + '\'' + ", createTime=" + createTime + ", updateTime=" + updateTime + ", lastLogin=" + lastLogin + ", userStatus=" + userStatus + ", remark='" + remark + '\'' + '}'; } }
detail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>慕课网用户管理中心</title> <link rel="stylesheet" href="lib/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <script src="lib/2.2.4/jquery-1.12.4.min.js"></script> <script src="lib/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="page-header"> <h1>慕课网后台管理系统 <small>用户数据管理中心</small> </h1> </div> </div> <div class="row"> <div class="jumbotron"> <h1>MyBatis基础入门课程</h1> <p>通过一个项目来完成基础部分的学习</p> <p><a class="btn btn-primary btn-lg" href="#" role="button">查看更多,请上慕课网</a></p> </div> </div> <c:set var="user" value="${user}"></c:set> <div class="row"> <div class="col-md-8 col-md-offset-2"> <form class="form-horizontal" action="${pageContext.request.contextPath}/updateusers"> <input type="hidden" name="id" value="${user.id}"> <div class="form-group"> <label class="col-sm-2 control-label">用户账号</label> <div class="col-sm-10"> <%--<p class="form-control-static">${user.username}</p>--%> <p class="form-control-static">${user.name}</p> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">登陆密码</label> <div class="col-sm-10"> <p class="form-control-static">************</p> </div> </div> <div class="form-group"> <label for="nickname" class="col-sm-2 control-label">昵称</label> <div class="col-sm-10"> <input type="text" class="form-control" id="nickname" value="${user.nickname}" name="nickname" placeholder="请输入昵称"> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄</label> <div class="col-sm-10"> <input type="text" class="form-control" id="age" value="${user.age}" name="age" placeholder="请输入年龄"> </div> </div> <div class="form-group"> <label for="gender" class="col-sm-2 control-label">性别</label> <div class="col-sm-10"> <input type="text" class="form-control" id="gender" value="${user.gender}" name="gender" placeholder="请输入性别"> </div> </div> <div class="form-group"> <label for="phone" class="col-sm-2 control-label">联系方式</label> <div class="col-sm-10"> <input type="text" class="form-control" id="phone" value="${usre.phone}" name="phone" placeholder="请输入联系方式"> </div> </div> <div class="form-group"> <label for="email" class="col-sm-2 control-label">邮箱</label> <div class="col-sm-10"> <input type="text" class="form-control" id="email" name="email" value="${user.email}" placeholder="请输入邮箱"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">账号创建时间</label> <div class="col-sm-10"> <p class="form-control-static"> <fmt:formatDate value="${user.createTime}"></fmt:formatDate></p> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">最后修改时间</label> <div class="col-sm-10"> <p class="form-control-static"> <fmt:formatDate value="${user.updateTime}"></fmt:formatDate></p> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">最后登陆时间</label> <div class="col-sm-10"> <p class="form-control-static"><fmt:formatDate value="${user.lastLogin}"></fmt:formatDate></p> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">用户状态</label> <div class="col-sm-10"> <c:if test="${user.userStatus == 0}"> <p class="form-control-static">正常</p> </c:if> <c:if test="${user.userStatus == 1}"> <p class="form-control-static">锁定</p> </c:if> <c:if test="${user.userStatus == 2}"> <p class="form-control-static">删除</p> </c:if> </div> </div> <div class="form-group"> <label for="remark" class="col-sm-2 control-label">备注</label> <div class="col-sm-10"> <input type="text" class="form-control" id="remark" value="${user.remark}" name="remark" placeholder="请输入备注"> </div> <div class="form-group"> <input type="submit" value="提交数据更新" class="btn btn-primary"> </div> </div> </form> </div> </div> </div> </body> </html>
mapper/usersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- mapper 用于定义一个映射配置文件的根节点 namespace 属性是用来配置命名空间,主要进行session级别的缓存管理 命名空间默认情况下,使用我们当前操作的实体类的全路径 --> <mapper namespace="com.damu.entity.Users"> <!--<select id="findUsers" resultType="com.damu.entity.Users">--> <!--select * from users--> <!--<if test="id!=null">--> <!--where id=#{id}--> <!--</if>--> <!--</select>--> <select id="findUsers" resultMap="forUsers"> select * from users <if test="id!=null"> where id=#{id} </if> </select> <!-- 自定义映射关系集合:主要包含对于一些自定义的操作的配置,如不一致的属性和字段--> <resultMap id="forUsers" type="com.damu.entity.Users"> <!--<id column="" property=""></id> --> <!-- result配置:主要配置普通属性,column表示配置的是数据库字段名称 property配置的是实体类的属性名称--> <result column="username" property="name"></result> </resultMap> <sql id="user_fields"> username,userpass,nickname,age,gender,email,phone,createTime,updateTime,lastLogin,userstatus,remark </sql> <!--useGeneratedKeys 取值范围true|false 默认值是:false。 含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。--> <!-- 主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值不是数据库表中的字段名 keyProperty="对应的主键的对象"--> <insert id="addUser" useGeneratedKeys="true" keyProperty="id"> insert into users(<include refid="user_fields"></include> ) value (#{name},#{userpass},#{nickname},#{age},#{gender},#{email},#{phone},#{createTime},#{updateTime},#{lastLogin},#{userStatus},#{remark}) </insert> <update id="updateUser"> update users <set> <if test="name != null">username = #{name},</if> <if test="userpass != null">userpass = #{userpass},</if> <if test="nickname != null">nickname = #{nickname},</if> <if test="age != null">age = #{age},</if> <if test="gender != null">gender = #{gender},</if> <if test="email != null">email = #{email},</if> <if test="phone != null">phone = #{phone},</if> <if test="createTime != null">createTime = #{createTime},</if> <if test="updateTime != null">updateTime = #{updateTime},</if> <if test="lastLogin != null">lastlogin = #{lastLogin},</if> <if test="userStatus != null">userStatus = #{userStatus},</if> <if test="remark != null">remark = #{remark},</if> </set> where id = #{id} </update> </mapper>
正在回答 回答被采纳积分+1
创建User时,调用的构造函数是Users user = new Users(username, userpass, nickname, Integer.parseInt(age), gender, email,phone, new Date(), new Date(), new Date(), 0);
而实际的构造函数是Users(String name, String userpass, String nickname, Integer age, String gender, String phone, String email, Date createTime, Date updateTime, Date lastLogin, Integer userStatus)
phone 和 email 的位置错了
而提供的sql语句,phone VARCHAR(13) COMMENT '联系方式',
email VARCHAR(20) COMMENT '用户邮箱',
phone是13是字符,email是20个字符,那么当填的email大于13个的时候,程序运行就会报错了,这个user根本插入不到数据库
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星