连接到postgres Spring MVC时出现NullPointerException

时间:2017-06-08 22:38:33

标签: java spring postgresql

我写了这段代码:

控制器:

@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);
    }
}

集团回购接口

public interface GroupRepository {

    List<Group> getAllGroups();
}

Group Repo Impl

@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;
            }
        });
    }
}

组服务接口

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

Group Service Impl

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupRepository groupRepository;

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

当我尝试输入&#34; / groups&#34;我收到此错误: error

我认为问题在于无法建立数据库连接。我们排除了几乎所有可能的错误我们尝试使用ArrayList而不是使用数据库,它运行良好。我该如何解决?

1 个答案:

答案 0 :(得分:1)

您的数据库配置中有错误。 dbConnect类不是由Spring管理的,因此您可以获得NPE,因为它不会被注入您的存储库。我建议使用以下配置:

@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;
        });
    }

}