1 | 2
Source Code
Domain Class
Issue.java
Dao Layer
IssuesDao.java (an interface)
IssuesDaoImpl.java
Service Layer
IssuesService.java (an interface)
IssuesServiceImpl.java
Client
IssueTracker.java
ApplicationContext.xml
Note that our dao class IssuesDaoImpl extend the DaoSupport class provided by Spring namely JdbcDaoSupport. Due to which we do not need to write following code in our dao as we did in example of JdbcTemplate:
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
To obtain the JdbcTemplate object we have used getJdbcTemplate() method provided by JdbcDaoSupport Class.
Also note that in applicationcontext.xml, we have directly inject datasource into our dao instead of injecting template into it.
The description of update(), mapRow() and query() methods is same as mentioned in JdbcTemplate example.
Output
1 | 2
Source Code
Domain Class
Issue.java
package examples.springjdbc.domain;
public class Issue{
private int tid;
private String assigned_to;
private String assigned_by;
private String status;
public void setTid(int tid){
this.tid = tid;
}
public int getTid(){
return tid;
}
public void setAssigned_to(String assigned_to){
this.assigned_to = assigned_to;
}
public String getAssigned_to(){
return assigned_to;
}
public void setAssigned_by(String assigned_by){
this.assigned_by = assigned_by;
}
public String getAssigned_by(){
return assigned_by;
}
public void setStatus(String status){
this.status = status;
}
public String getStatus(){
return status;
}
}
public class Issue{
private int tid;
private String assigned_to;
private String assigned_by;
private String status;
public void setTid(int tid){
this.tid = tid;
}
public int getTid(){
return tid;
}
public void setAssigned_to(String assigned_to){
this.assigned_to = assigned_to;
}
public String getAssigned_to(){
return assigned_to;
}
public void setAssigned_by(String assigned_by){
this.assigned_by = assigned_by;
}
public String getAssigned_by(){
return assigned_by;
}
public void setStatus(String status){
this.status = status;
}
public String getStatus(){
return status;
}
}
Dao Layer
IssuesDao.java (an interface)
package examples.springjdbc.dao;
import examples.springjdbc.domain.Issue;
import java.util.List;
public interface IssuesDao{
public void addIssue(Issue issue);
public List<Issue> getIssuesByAssignee(String asignee);
public Issue getIssuesById(int tid);
public int updateIssue(Issue issue);
public void deleteIssue(int tid);
}
import examples.springjdbc.domain.Issue;
import java.util.List;
public interface IssuesDao{
public void addIssue(Issue issue);
public List<Issue> getIssuesByAssignee(String asignee);
public Issue getIssuesById(int tid);
public int updateIssue(Issue issue);
public void deleteIssue(int tid);
}
IssuesDaoImpl.java
package examples.springjdbc.dao;
import examples.springjdbc.domain.Issue;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
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 List<Issue> getIssuesByAssignee(String assignee){
String sql="select * from issue where assigned_by=?";
RowMapper<Issue> myRowMapper = new RowMapper<Issue>(){
public Issue mapRow(ResultSet rs, int rowNum)throws SQLException{
Issue issue = new Issue();
issue.setTid(rs.getInt("tid"));
issue.setAssigned_to(rs.getString("assigned_to"));
issue.setAssigned_by(rs.getString("assigned_by"));
issue.setStatus(rs.getString("status"));
return issue;
}
};
List<Issue> issueList = getJdbcTemplate().query(sql,myRowMapper,assignee);
return issueList;
}
public Issue getIssuesById(int tid){
String sql="select * from issue where tid=?";
RowMapper<Issue> myRowMapper = new RowMapper<Issue>(){
public Issue mapRow(ResultSet rs, int rowNum)throws SQLException{
Issue issue = new Issue();
issue.setTid(rs.getInt("tid"));
issue.setAssigned_to(rs.getString("assigned_to"));
issue.setAssigned_by(rs.getString("assigned_by"));
issue.setStatus(rs.getString("status"));
return issue;
}
};
Issue issue = getJdbcTemplate().queryForObject(sql,myRowMapper,tid);
return issue;
}
public int updateIssue(Issue issue){
String sql = "update issue set assigned_to=?,assigned_by=?,status=? where tid=?";
int rows=getJdbcTemplate().update(sql,issue.getAssigned_to(),issue.getAssigned_by(),issue.getStatus(),issue.getTid());
return rows;
}
public void deleteIssue(int tid){
String sql = "delete from issue where tid=?";
getJdbcTemplate().update(sql,tid);
}
}
import examples.springjdbc.domain.Issue;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
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 List<Issue> getIssuesByAssignee(String assignee){
String sql="select * from issue where assigned_by=?";
RowMapper<Issue> myRowMapper = new RowMapper<Issue>(){
public Issue mapRow(ResultSet rs, int rowNum)throws SQLException{
Issue issue = new Issue();
issue.setTid(rs.getInt("tid"));
issue.setAssigned_to(rs.getString("assigned_to"));
issue.setAssigned_by(rs.getString("assigned_by"));
issue.setStatus(rs.getString("status"));
return issue;
}
};
List<Issue> issueList = getJdbcTemplate().query(sql,myRowMapper,assignee);
return issueList;
}
public Issue getIssuesById(int tid){
String sql="select * from issue where tid=?";
RowMapper<Issue> myRowMapper = new RowMapper<Issue>(){
public Issue mapRow(ResultSet rs, int rowNum)throws SQLException{
Issue issue = new Issue();
issue.setTid(rs.getInt("tid"));
issue.setAssigned_to(rs.getString("assigned_to"));
issue.setAssigned_by(rs.getString("assigned_by"));
issue.setStatus(rs.getString("status"));
return issue;
}
};
Issue issue = getJdbcTemplate().queryForObject(sql,myRowMapper,tid);
return issue;
}
public int updateIssue(Issue issue){
String sql = "update issue set assigned_to=?,assigned_by=?,status=? where tid=?";
int rows=getJdbcTemplate().update(sql,issue.getAssigned_to(),issue.getAssigned_by(),issue.getStatus(),issue.getTid());
return rows;
}
public void deleteIssue(int tid){
String sql = "delete from issue where tid=?";
getJdbcTemplate().update(sql,tid);
}
}
Service Layer
IssuesService.java (an interface)
package examples.springjdbc.service;
import examples.springjdbc.domain.Issue;
import java.util.List;
public interface IssuesService{
public void createIssue(Issue issue);
public List<Issue> listByAssignee(String asignee);
public Issue getIssuesById(int tid);
public int updateIssue(Issue issue);
public void deleteIssue(int tid);
}
import examples.springjdbc.domain.Issue;
import java.util.List;
public interface IssuesService{
public void createIssue(Issue issue);
public List<Issue> listByAssignee(String asignee);
public Issue getIssuesById(int tid);
public int updateIssue(Issue issue);
public void deleteIssue(int tid);
}
IssuesServiceImpl.java
package examples.springjdbc.service;
import examples.springjdbc.domain.Issue;
import examples.springjdbc.dao.IssuesDao;
import java.util.List;
public class IssuesServiceImpl implements IssuesService{
private IssuesDao issueDao;
public void setIssueDao(IssuesDao issueDao){
this.issueDao=issueDao;
}
public void createIssue(Issue issue){
issueDao.addIssue(issue);
}
public List<Issue> listByAssignee(String asignee){
List<Issue> list= issueDao.getIssuesByAssignee(asignee);
return list;
}
public Issue getIssuesById(int tid){
Issue issue= issueDao.getIssuesById(tid);
return issue;
}
public int updateIssue(Issue issue){
int row=issueDao.updateIssue(issue);
return row;
}
public void deleteIssue(int tid){
issueDao.deleteIssue(tid);
}
}
import examples.springjdbc.domain.Issue;
import examples.springjdbc.dao.IssuesDao;
import java.util.List;
public class IssuesServiceImpl implements IssuesService{
private IssuesDao issueDao;
public void setIssueDao(IssuesDao issueDao){
this.issueDao=issueDao;
}
public void createIssue(Issue issue){
issueDao.addIssue(issue);
}
public List<Issue> listByAssignee(String asignee){
List<Issue> list= issueDao.getIssuesByAssignee(asignee);
return list;
}
public Issue getIssuesById(int tid){
Issue issue= issueDao.getIssuesById(tid);
return issue;
}
public int updateIssue(Issue issue){
int row=issueDao.updateIssue(issue);
return row;
}
public void deleteIssue(int tid){
issueDao.deleteIssue(tid);
}
}
Client
IssueTracker.java
package examples.springjdbc.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import examples.springjdbc.domain.Issue;
import examples.springjdbc.service.IssuesService;
public class IssueTracker{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
IssuesService iService = (IssuesService) appContext.getBean("iService");
/*Creating new Issues*/
Issue issue = new Issue();
issue.setAssigned_to("Mr. X");
issue.setAssigned_by("Mr. A");
issue.setStatus("Open");
iService.createIssue(issue);
System.out.println("Issue Created...");
issue = new Issue();
issue.setAssigned_to("Mr. Y");
issue.setAssigned_by("Mr. B");
issue.setStatus("Open");
iService.createIssue(issue);
System.out.println("Issue Created...");
issue = new Issue();
issue.setAssigned_to("Mr. Z");
issue.setAssigned_by("Mr. B");
issue.setStatus("Open");
iService.createIssue(issue);
System.out.println("Issue Created...");
/*Listing Issues*/
List<Issue> issuesList=iService.listByAssignee("Mr. B");
System.out.println("Issues assigned by Mr. B:");
System.out.println("==========================");
for(Issue i:issuesList){
System.out.print("Id:"+i.getTid());
System.out.print(" Assigned By :"+i.getAssigned_by());
System.out.print(" Assigned To:"+i.getAssigned_to());
System.out.println(" Status:"+i.getStatus());
}
System.out.println("==========================");
/*Updating the issue*/
System.out.println("Enter Id of the issue to be updated: ");
int op=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
op= Integer.parseInt(br.readLine());
}catch(IOException e){
System.out.println(e);
}
issue=iService.getIssuesById(op);
issue.setStatus("closed");
iService.updateIssue(issue);
System.out.println("Issue status Updated...");
/*deleting the issue*/
System.out.println("Enter Id of the issue to be deleted: ");
try{
op= Integer.parseInt(br.readLine());
}catch(IOException e){
System.out.println(e);
}
iService.deleteIssue(op);
System.out.println("Issue Deleted...");
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import examples.springjdbc.domain.Issue;
import examples.springjdbc.service.IssuesService;
public class IssueTracker{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
IssuesService iService = (IssuesService) appContext.getBean("iService");
/*Creating new Issues*/
Issue issue = new Issue();
issue.setAssigned_to("Mr. X");
issue.setAssigned_by("Mr. A");
issue.setStatus("Open");
iService.createIssue(issue);
System.out.println("Issue Created...");
issue = new Issue();
issue.setAssigned_to("Mr. Y");
issue.setAssigned_by("Mr. B");
issue.setStatus("Open");
iService.createIssue(issue);
System.out.println("Issue Created...");
issue = new Issue();
issue.setAssigned_to("Mr. Z");
issue.setAssigned_by("Mr. B");
issue.setStatus("Open");
iService.createIssue(issue);
System.out.println("Issue Created...");
/*Listing Issues*/
List<Issue> issuesList=iService.listByAssignee("Mr. B");
System.out.println("Issues assigned by Mr. B:");
System.out.println("==========================");
for(Issue i:issuesList){
System.out.print("Id:"+i.getTid());
System.out.print(" Assigned By :"+i.getAssigned_by());
System.out.print(" Assigned To:"+i.getAssigned_to());
System.out.println(" Status:"+i.getStatus());
}
System.out.println("==========================");
/*Updating the issue*/
System.out.println("Enter Id of the issue to be updated: ");
int op=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
op= Integer.parseInt(br.readLine());
}catch(IOException e){
System.out.println(e);
}
issue=iService.getIssuesById(op);
issue.setStatus("closed");
iService.updateIssue(issue);
System.out.println("Issue status Updated...");
/*deleting the issue*/
System.out.println("Enter Id of the issue to be deleted: ");
try{
op= Integer.parseInt(br.readLine());
}catch(IOException e){
System.out.println(e);
}
iService.deleteIssue(op);
System.out.println("Issue Deleted...");
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<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>
<!-- No Need To Declare Template
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource" />
</bean>
-->
<bean id="myIssueDao" class="examples.springjdbc.dao.IssuesDaoImpl">
<!-- <property name="jdbcTemplate" ref="myJdbcTemplate"/>-->
<property name="dataSource" ref="myDataSource"/>
</bean>
<bean id="iService" class="examples.springjdbc.service.IssuesServiceImpl">
<property name="issueDao" ref="myIssueDao"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<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>
<!-- No Need To Declare Template
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource" />
</bean>
-->
<bean id="myIssueDao" class="examples.springjdbc.dao.IssuesDaoImpl">
<!-- <property name="jdbcTemplate" ref="myJdbcTemplate"/>-->
<property name="dataSource" ref="myDataSource"/>
</bean>
<bean id="iService" class="examples.springjdbc.service.IssuesServiceImpl">
<property name="issueDao" ref="myIssueDao"/>
</bean>
</beans>
Note that our dao class IssuesDaoImpl extend the DaoSupport class provided by Spring namely JdbcDaoSupport. Due to which we do not need to write following code in our dao as we did in example of JdbcTemplate:
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
To obtain the JdbcTemplate object we have used getJdbcTemplate() method provided by JdbcDaoSupport Class.
Also note that in applicationcontext.xml, we have directly inject datasource into our dao instead of injecting template into it.
The description of update(), mapRow() and query() methods is same as mentioned in JdbcTemplate example.
Output
Issue Created...
Issue Created...
Issue Created...
Issues assigned by Mr. B:
==========================
Id:131 Assigned By :Mr. B Assigned To:Mr. Z Status:Open
Id:133 Assigned By :Mr. B Assigned To:Mr. Y Status:Open
Id:134 Assigned By :Mr. B Assigned To:Mr. Z Status:closed
Id:136 Assigned By :Mr. B Assigned To:Mr. Y Status:Open
Id:137 Assigned By :Mr. B Assigned To:Mr. Z Status:Open
==========================
Enter Id of the issue to be updated:
137
Issue status Updated...
Enter Id of the issue to be deleted:
136
Issue Deleted...
Issue Created...
Issue Created...
Issues assigned by Mr. B:
==========================
Id:131 Assigned By :Mr. B Assigned To:Mr. Z Status:Open
Id:133 Assigned By :Mr. B Assigned To:Mr. Y Status:Open
Id:134 Assigned By :Mr. B Assigned To:Mr. Z Status:closed
Id:136 Assigned By :Mr. B Assigned To:Mr. Y Status:Open
Id:137 Assigned By :Mr. B Assigned To:Mr. Z Status:Open
==========================
Enter Id of the issue to be updated:
137
Issue status Updated...
Enter Id of the issue to be deleted:
136
Issue Deleted...
1 | 2
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