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