gpt4 book ai didi

java - 连接到 postgres Spring MVC 时出现 NullPointerException

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

我写了这段代码:

Controller :

@Controller
@RequestMapping("/groups")
public class GroupController {

@Autowired
private GroupService groupService;

@RequestMapping
public String list(Model model) {
model.addAttribute("groups", groupService.getAllGroups());
return "groups";
}
}

数据库连接

    public class dbConnect {

public dbConnect(){}

private JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void DatabaseConnection(){
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl("jdbc:postgres://xxx");
driverManagerDataSource.setUsername("x");
driverManagerDataSource.setPassword("xss");
this.jdbcTemplate=new JdbcTemplate(driverManagerDataSource);
}
}

群组仓库接口(interface)

public interface GroupRepository {

List<Group> getAllGroups();
}

群组 repo 实现

@Repository
public class InMemoryGroupRepository implements GroupRepository {

public InMemoryGroupRepository(){}

private dbConnect data;

//@Autowired
public InMemoryGroupRepository(dbConnect dbConnect) {
this.data = dbConnect;
}


public List<Group> getAllGroups() {
return data.getJdbcTemplate().query("SELECT id_grupy, nazwa, id_egzaminatora, haslo, egzaminatorzy_id_egzaminatora FROM grupy", new RowMapper<Group>() {
public Group mapRow(ResultSet rs, int rowNum)
throws SQLException {
Group group = new Group();
group.setId_grupy(rs.getInt(1));
group.setNazwa(rs.getString(2));
group.setId_egzaminatora(rs.getInt(3));
group.setHaslo(rs.getString(4));
group.setEgzaminatorzy_id_egzaminatora(rs.getInt(5));
return group;
}
});
}
}

群组服务接口(interface)

public interface GroupService {
List<Group> getAllGroups();
}

群组服务实现

@Service
public class GroupServiceImpl implements GroupService {

@Autowired
private GroupRepository groupRepository;

public List<Group> getAllGroups() {
return groupRepository.getAllGroups();
}
}

当我尝试输入“/groups”时,出现此错误: error

我认为问题是无法建立数据库连接。我们排除了几乎所有可能的错误。我们尝试使用 ArrayList 而不是使用数据库,效果很好。我该如何修复它?

最佳答案

您的数据库配置有错误。 dbConnect 类不是由 Spring 管理的,因此您会得到 NPE,因为它没有注入(inject)到您的存储库中。我建议使用下面的配置:

@Configuration
class PersistenceConfig {

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgres://xxx");
dataSource.setUsername("x");
dataSource.setPassword("xss");

return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

}

和存储库实现:

@Repository
public class GroupPostgresRepository implements GroupRepository {

private final JdbcTemplate jdbcTemplate;

public GroupPostgresRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<Group> getAllGroups() {
final String sql = "SELECT id_grupy, nazwa, id_egzaminatora, haslo, egzaminatorzy_id_egzaminatora FROM grupy";

return jdbcTemplate.query(sql, (rs, rowNum) -> {
Group group = new Group();
group.setId_grupy(rs.getInt(1));
group.setNazwa(rs.getString(2));
group.setId_egzaminatora(rs.getInt(3));
group.setHaslo(rs.getString(4));
group.setEgzaminatorzy_id_egzaminatora(rs.getInt(5));
return group;
});
}

}

关于java - 连接到 postgres Spring MVC 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44446639/

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