Zamiana beans z xml na adnotacje

0

Jak poprawnie zamienić następujące beans z xml na adnotacje?

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  
  
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
<property name="username" value=""></property>  
<property name="password" value=""></property>  
</bean>  
  
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  
  
<bean id="dao" class="com.javatpoint.dao.EmpDao">  
<property name="template" ref="jt"></property>  
</bean>  
</beans>  

Próbowałem tak:

	@Bean
    public DriverManagerDataSource ds() {
        DriverManagerDataSource driverClassName = new DriverManagerDataSource();
        driverClassName.setDriverClassName("com.mysql.jdbc.Driver");
        driverClassName.setUsername("root");
        driverClassName.setPassword("12345");
        driverClassName.setUrl("jdbc:mysql://localhost:3306/websystique");
        return driverClassName;
    }

	@Bean
    public JdbcTemplate jt() {
        return new JdbcTemplate(ds());
    }

    @Bean
    public EmpDao dao() {
        EmpDao temp = new EmpDao();
        temp.setTemplate(jt());
        return temp;
    }

ale cos tu zrobiłem źle i wywala błąd Request processing failed; nested exception is java.lang.NullPointerException] with

a tak wygląda klasa EmpDao

public class EmpDao {
    JdbcTemplate template;

    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public List<Emp> getEmployeesByPage(int pageid, int total){
        String sql="select * from Emp limit "+(pageid-1)+","+total;

        return template.query(sql,new RowMapper<Emp>(){
            public Emp mapRow(ResultSet rs, int row) throws SQLException {
                Emp e=new Emp();
                e.setId(rs.getInt(1));
                e.setName(rs.getString(2));
                e.setSalary(rs.getFloat(3));
                return e;
            }
        });
    }
}
0
  1. A reszta tej klasy z konfiguracją? Jest tam chociaż jakieś @Confirguration?
  2. W tym EmpDao masz ty jakieś @Named? O @Inject na tym polu/konstruktorze nie pytam, bo widzę że nie masz...
0

Jednak dziala nazwa tabeli byla z wielkiej litery :). Dzieki, pozdrawiam.

1 użytkowników online, w tym zalogowanych: 0, gości: 1