我该如何解决它所说的 'queryForObject(java.lang.String, java.lang.Object[], java.lang.Class<T>)' 已弃用的问题
How do I fix this problem it say's 'queryForObject(java.lang.String, java.lang.Object[], java.lang.Class<T>)' is deprecated
我不确定如何解决这个问题,因为我上周四才开始做 spring 引导项目。不幸的是,今天是我项目的最后期限,坦率地说,我现在很恐慌:/
这是我的示例问题代码:
@Override
public Integer getCountByEmail(String email) {
return jdbcTemplate.queryForObject(SQL_COUNT_BY_EMAIL, new Object[]{email}, Integer.class);
}
@Override
public User findById(Integer userId) {
return null;
}
这里是JavaClass的整体代码:
package com.example.Spring.boot.Repositories;
import com.example.Spring.boot.Entity.User;
import com.example.Spring.boot.Exceptions.EtAuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.Statement;
@Repository
public class UserRepositoryImpl implements UserRepository {
private static final String SQL_CREATE = "INSERT INTO SB_USERS(USER_ID,
FIRST_NAME, LAST_NAME, EMAIL," +`"PASSWORD) VALUES(NEXTVAL('SB_USERS_SEQ'), ?, ?, ?, ?)";`
private static final String SQL_COUNT_BY_EMAIL = "SELECT COUNT(*) FROM SB_USERS WHERE EMAIL = ?";
private static final String ID = "SELECT USER_ID, FIRST_NAME, LAST_NAME, EMAIL, PASSWORD " +`"FROM SB_USERS WHERE USER_ID = ?";`
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public Integer create(String firstName, String lastName, String email, String password) throws EtAuthException {
try {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(SQL_CREATE, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, email);
ps.setString(4, password);
return ps;
}, keyHolder);
return (Integer) keyHolder.getKeys().get("USER_ID");
}catch (Exception e) {
throw new EtAuthException("Invalid details. Failed to create account");
}
}
@Override
public User findByEmailAndPassword(String email, String password) throws EtAuthException {
return null;
}
@Override
public Integer getCountByEmail(String email) {
return jdbcTemplate.queryForObject(SQL_COUNT_BY_EMAIL, new Object[]{email}, Integer.class);
}
@Override
public User findById(Integer userId) {
return null;
}
}
以下方法已被弃用,不得使用:
public <T> T queryForObject(String sql, @Nullable Object[] args, Class<T> requiredType)
改用以下等效方法:
<T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args)
示例:
return jdbcTemplate.queryForObject(SQL_COUNT_BY_EMAIL, Integer.class, email);
我不确定如何解决这个问题,因为我上周四才开始做 spring 引导项目。不幸的是,今天是我项目的最后期限,坦率地说,我现在很恐慌:/
这是我的示例问题代码:
@Override
public Integer getCountByEmail(String email) {
return jdbcTemplate.queryForObject(SQL_COUNT_BY_EMAIL, new Object[]{email}, Integer.class);
}
@Override
public User findById(Integer userId) {
return null;
}
这里是JavaClass的整体代码:
package com.example.Spring.boot.Repositories;
import com.example.Spring.boot.Entity.User;
import com.example.Spring.boot.Exceptions.EtAuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.Statement;
@Repository
public class UserRepositoryImpl implements UserRepository {
private static final String SQL_CREATE = "INSERT INTO SB_USERS(USER_ID,
FIRST_NAME, LAST_NAME, EMAIL," +`"PASSWORD) VALUES(NEXTVAL('SB_USERS_SEQ'), ?, ?, ?, ?)";`
private static final String SQL_COUNT_BY_EMAIL = "SELECT COUNT(*) FROM SB_USERS WHERE EMAIL = ?";
private static final String ID = "SELECT USER_ID, FIRST_NAME, LAST_NAME, EMAIL, PASSWORD " +`"FROM SB_USERS WHERE USER_ID = ?";`
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public Integer create(String firstName, String lastName, String email, String password) throws EtAuthException {
try {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(SQL_CREATE, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, email);
ps.setString(4, password);
return ps;
}, keyHolder);
return (Integer) keyHolder.getKeys().get("USER_ID");
}catch (Exception e) {
throw new EtAuthException("Invalid details. Failed to create account");
}
}
@Override
public User findByEmailAndPassword(String email, String password) throws EtAuthException {
return null;
}
@Override
public Integer getCountByEmail(String email) {
return jdbcTemplate.queryForObject(SQL_COUNT_BY_EMAIL, new Object[]{email}, Integer.class);
}
@Override
public User findById(Integer userId) {
return null;
}
}
以下方法已被弃用,不得使用:
public <T> T queryForObject(String sql, @Nullable Object[] args, Class<T> requiredType)
改用以下等效方法:
<T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args)
示例:
return jdbcTemplate.queryForObject(SQL_COUNT_BY_EMAIL, Integer.class, email);