gpt4 book ai didi

Java Spring MVC 返回多个值

转载 作者:行者123 更新时间:2023-12-02 03:54:21 25 4
gpt4 key购买 nike

我想在执行查询后从函数返回两个值,即结果和消息

@Repository
@Transactional(value = "myTransactionManager")
public class UserFunctionsDAOImpl implements UserFunctionsDAO {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}

@Override
public List<Object[]> registerUser(Users user) {
// TODO Auto-generated method stub
ReturnData returnData = new ReturnData();

Query query = this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM Users WHERE emailID = :email_ID OR mobileNo = :mobile_No");
query.setString("email_ID", user.getEmailID());
query.setString("mobile_No", user.getMobileNo());

if(((long)query.uniqueResult()) > 0)
{
returnData.setResult(false);
returnData.setMessage("Email or Mobile number already exists");
return (List<Object[]>);
}
else
{
Query query = this.sessionFactory.getCurrentSession().createSQLQuery("");
query.executeUpdate();
returnData.setResult(true);
returnData.setMessage("User Registered Successfully");
return (List<Object>[])returnData;
}
}

}

public class ReturnData
{
Boolean result;
String message;

//getters and setters
}

错误

java.lang.ClassCastException: com.pir.model.ReturnData cannot be cast to java.util.List

如何解决这个错误?

最佳答案

您返回的对象无法转换为该方法的返回类型。

重写方法如下:

@Override
public ReturnData registerUser(Users user) {
// TODO Auto-generated method stub
ReturnData returnData = new ReturnData();

Query query = this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM Users WHERE emailID = :email_ID OR mobileNo = :mobile_No");
query.setString("email_ID", user.getEmailID());
query.setString("mobile_No", user.getMobileNo());

if(((long)query.uniqueResult()) > 0)
{
returnData.setResult(false);
returnData.setMessage("Email or Mobile number already exists");
return returnData;
}
else
{
Query query = this.sessionFactory.getCurrentSession().createSQLQuery("");
query.executeUpdate();
returnData.setResult(true);
returnData.setMessage("User Registered Successfully");
return returnData;
}
}

关于Java Spring MVC 返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com