当前位置:网站首页>jdbctemplate数据后台管理,不知道为什么添加用户的时候显示roleId为空
jdbctemplate数据后台管理,不知道为什么添加用户的时候显示roleId为空
2022-06-11 13:03:00 【CSDN问答】
jdbctemplate数据后台管理,不知道为什么添加用户的时候显示roleId为空,其他数据都可以正常添加
问题相关代码,请勿粘贴截图
/*用数组接收前台发送过来的角色表单数据 controller */@RequestMapping("/save")public String save(User user,long[] roleIds) { userService.save(user, roleIds); return "redirect:/user/list";}这是userdaoImpl
@Override
public Long save(final User user) {
final String saveUserSql = "insert into sys_user values(语句没问题,测试过 )";
// 创建PreparedStatementCreator
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
// 使用原始的jdbc完成PreparedStatement的组建
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
// PreparedStatement.RETURN_GENERATED_KEYS代表自动生成主键
PreparedStatement preparedStatement = connection.prepareStatement(saveUserSql, PreparedStatement.RETURN_GENERATED_KEYS);
preparedStatement.setObject(1, null);
preparedStatement.setObject(2, user.getUserName());
preparedStatement.setObject(3, user.getEmail());
preparedStatement.setObject(4, user.getPassword());
preparedStatement.setObject(5, user.getPhoneNum());
return preparedStatement;
}
};
// 通过该对象获得生成的主键
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
Long userId = null;
try {
//将用户信息存储到表中
jdbcTemplate.update(preparedStatementCreator, generatedKeyHolder);
userId = generatedKeyHolder.getKey().longValue(); } catch (Exception e) { e.printStackTrace(); } return userId;}/*
将存完后自动生成的ID直接返回给Service,再执行 用户—角色表进行存储
*/
@Override
public void saveUserAndRole(long userId, long[] roleIds) {
for (Long roleId : roleIds) {
jdbcTemplate.update("insert into sys_user_role values(?,?)", userId, roleId);
}
}
这是servicesImpl
/*
既要新建用户信息,也要建立用户与角色关系信息,要对两张表操作
*/
@Override
public void save(User user, long[] roleIds) {
//第一次先添加sys_user
Long userId = userDao.save(user);
//第二次添加sys_user_role
userDao.saveUserAndRole(userId,roleIds);
}
运行结果及报错内容
java.lang.NullPointerException
com.chinasofti.dao.impl.UserDaoImpl.saveUserAndRole(UserDaoImpl.java:80)
com.chinasofti.service.impl.UserServiceImpl.save(UserServiceImpl.java:64)
com.chinasofti.controller.UserController.save(UserController.java:75)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)
javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
我的解答思路和尝试过的方法
我想要达到的结果
边栏推荐
- [interface] view the interface path and check the interface
- Evolution of e-commerce development
- Log management system, summary in multiple ways
- 【bug解决】上传图片后,取消这次上传 再次执行上传,上次的图片还存在
- NFT市场怎么样 为什么NFT能如此火爆 怎么搭建NFT平台
- How to synchronize openstack RDO source to local for offline installation
- Security mechanism of verification code in seckill
- kubernetes 二进制安装(v1.20.15)(七)加塞一个工作节点
- live share使用体验
- Seckill multi-level cache ----- product details page
猜你喜欢

How can mechanical equipment manufacturing enterprises manage outsourcing with the help of ERP system?

Log management system, summary in multiple ways

馆客多游泳馆会员管理系统可以实现哪些场景?

Gb28181 protocol has become the mainstream in the market. How to choose the appropriate security monitoring video solution?

Another way to achieve family reunion, 2022 flagship projection nut j10s is planted with grass

How does the beauty salon management system solve the three problems of store operation?

4. Locksupport and thread interruption

【信号去噪】基于稀疏性 (BEADS) 实现色谱基线估计和去噪附matlab代码和论文

What are the ways for badminton halls to generate income
Go语言学习之WaitGroup用法详解
随机推荐
How does the beauty salon management system solve the three problems of store operation?
Unity game protection "big training", read and understand the game's pre defense
shader着色器
App manual signature of openharmony standard system
苹果将造搜索引擎?
Development of smart contract DAPP system for TRX wave field chain
What are the advantages of comprehensive venues?
4. Locksupport and thread interruption
Clear the selected data in the modal box after the modal box is closed
@How to resolve controller and requestmapping
關於分布式鎖的續命問題——基於Redis實現的分布式鎖
Seckill multi-level cache ----- product details page
火山引擎云数据库 veDB 在字节内部的业务实践
. 4 literal and variable
4K投影儀哪款性價比最高,當貝X3 Pro高亮128G存儲618值得看
Venue floor efficiency is so low? The key lies in these two aspects
Imx6ul development board porting EMMC startup process of mainline u-boot
经营体育馆有哪些要素?
【backtrader源码解析46】cerebro.py代码注释(枯燥,backtrader核心代码之一,推荐阅读,注释仅供参考)
PKI/TLS瑞士军刀之cfssl