1 (Template Class) | 2 (Dao Support Class)
2. DaoSupport Classes
In the example we used in JdbcTemplate section, there is only one dao named IssuesDaoImpl.java. Suppose there are many Daos in your application.Then you have to repeat the following code in each of your dao:
But to avoid this spring provides Dao Support classes which your dao class will extend. The main benefit of Dao Support class is that it provides a method named getJdbcTemplate() which you can directly use in your dao to obtain the JdbcTemplate instead of writing above code snippet.
Like Templates, Dao Support classes are also of three types:
1) JdbcDaoSupport class (having method getJdbcTemplate())
2) SimpleJdbcDaoSupport class (having method getSimpleJdbcTemplate())
3) NamedParameterJdbcDaoSupport class (having method getNamedParameterJdbcTemplate())
Earlier in the Templates you inject properties as:
You can also do this here. But here you can also directly inject datasource into your dao instead of declaring template class in between and then use any getXXXTemplate() method to obtain template in your dao.
Thus, first declare datasource in applicationcontext as:
then inject it into your dao:
Note now you dont have to declare any setter method in dao and can obtain the template by using getJdbcTemplate() as:
Complete example is given here along with explanation.
1 (Template Class) | 2 (Dao Support Class)
Reference : Spring in Action (Third edition) by Craig Walls2. DaoSupport Classes
In the example we used in JdbcTemplate section, there is only one dao named IssuesDaoImpl.java. Suppose there are many Daos in your application.Then you have to repeat the following code in each of your dao:
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
But to avoid this spring provides Dao Support classes which your dao class will extend. The main benefit of Dao Support class is that it provides a method named getJdbcTemplate() which you can directly use in your dao to obtain the JdbcTemplate instead of writing above code snippet.
Like Templates, Dao Support classes are also of three types:
1) JdbcDaoSupport class (having method getJdbcTemplate())
2) SimpleJdbcDaoSupport class (having method getSimpleJdbcTemplate())
3) NamedParameterJdbcDaoSupport class (having method getNamedParameterJdbcTemplate())
Earlier in the Templates you inject properties as:
You can also do this here. But here you can also directly inject datasource into your dao instead of declaring template class in between and then use any getXXXTemplate() method to obtain template in your dao.
Thus, first declare datasource in applicationcontext as:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/examples" />
<property name="username" value="root" />
<!-- change 'root' with your password -->
<property name="password" value="root" />
</bean>
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/examples" />
<property name="username" value="root" />
<!-- change 'root' with your password -->
<property name="password" value="root" />
</bean>
then inject it into your dao:
<bean id="myIssueDao" class="examples.springjdbc.dao.IssuesDaoImpl">
<property name="dataSource" ref="myDataSource"/>
</bean>
<property name="dataSource" ref="myDataSource"/>
</bean>
Note now you dont have to declare any setter method in dao and can obtain the template by using getJdbcTemplate() as:
public class IssuesDaoImpl extends JdbcDaoSupport implements IssuesDao{
public void addIssue(Issue issue){
String sql = "insert into issue(assigned_to,assigned_by,status) values(?,?,?)";
getJdbcTemplate().update(sql,issue.getAssigned_to(),issue.getAssigned_by(),issue.getStatus());
}
...
}
public void addIssue(Issue issue){
String sql = "insert into issue(assigned_to,assigned_by,status) values(?,?,?)";
getJdbcTemplate().update(sql,issue.getAssigned_to(),issue.getAssigned_by(),issue.getStatus());
}
...
}
Complete example is given here along with explanation.
1 (Template Class) | 2 (Dao Support Class)
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment