I am using the spring mvc3 in my applicatio,and in the dao layer I want to use jdbctemplate,however I do not know where to add the dao,in the controller?
For example:
@Controller
public class UserController{
private UserDao udao;
public String list(Model model){
udao=new UserDaoImple();
List<User> users=udao.list();
model.addAttrubut('users',users);
return "list";
}
}
The above code is just an example,I want to know where to create the userdao?
Also,since I want to use the jdbctemplate,and it is recommended that the jdbctemplate is created only once for one datasourece,so how to make all the daos use the same jdbctemplate?
You could use Spring IOC (dependency injection) to inject the DAO like this
or you could use the repository pattern, and create a central point for all the DAOs so you just go to the repository and ask for the DAO you need.
for that you would have to create singleton class that has all instances of all DAOs and when asked give give an instance to your class, so you don’t need to instantiate the Dao just do a
In my opinion, go for the Spring approach you will learn a very useful skill and it’s a lot easier to maintain when you know what you are doing.