我正在做一个项目,我希望使用saveEmployee()
类EmployeeDAO
类中的EmployeeManager
方法使用bean。
Employee.java
package com.tutorialspoint.test;
public class Employee {
EmployeeManager employeeManager;
private String fname;
private String lname;
private int dob;
private String add;
private int id;
public void setfname(String fname){
this.fname=fname;
}
public String getfname(){
return fname;
}
public void setlname(String lname){
this.lname=lname;
}
public String getlname(){
return lname;
}
public void setdob(int dob){
this.dob=dob;
}
public int getdob(){
return dob;
}
public void setadd(String add){
this.add=add;
}
public String getadd(){
return add;
}
public void setid(int id){
this.id=id;
}
public int getid(){
return id;
}
public void setEmployeeManager(EmployeeManager employeeManager) {
this.employeeManager = employeeManager;
}
}
EmployeeDAO.java
package com.tutorialspoint.test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.Serializable;
public class EmployeeDAO implements Serializable{
private static final long serialVersionUID = 1L;
//SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveEmployee(Employee e){
String fname = e.getfname();
String lname = e.getlname();
int dob=e.getdob();
String add=e.getadd();
int id = e.getid();
Object p[] = {fname, lname,dob,add,id};
String query="insert into employee values(?,?,?)";
return jdbcTemplate.update(query, p);
}
public int deleteEmployee(int id){
String sql ="delete from employee where id = ?";
return jdbcTemplate.update(sql, new Object[]{new Integer(id)});
}
public List<Employee> getAllEmployee(){
ResultSet rs = null;
PreparedStatement pst = null;
System.out.println("Connection starting.");
Connection con = getConnection();
String stm = "Select * from employee";
List<Employee> records = new ArrayList<Employee>();
try {
pst = con.prepareStatement(stm);
pst.execute();
rs = pst.getResultSet();
while(rs.next()){
Employee employee = new Employee();
employee.setfname(rs.getString(1));
employee.setlname(rs.getString(2));
employee.setdob(rs.getInt(3));
employee.setadd(rs.getString(4));
employee.setid(rs.getInt(5));
records.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
}
return records;
}
public Connection getConnection(){
Connection con = null;
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "postgres";
String password = "postgres";
try {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = DriverManager.getConnection(url, user, password);
System.out.println("Connection completed.");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
finally{
}
return con;
}
}
EmployeeManager.java
package com.tutorialspoint.test;
import org.springframework.stereotype.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tutorialspoint.test.EmployeeDAO;
import com.tutorialspoint.test.Employee;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
public class EmployeeManager {
//EmployeeDAO employeeDao=new EmployeeDAO();
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
**EmployeeDAO employeeDao= (EmployeeDAO) ctx.getBean("edao");
public void save(){
employeeDao.saveEmployee();
}**
List<Employee>list=employeeDao.getAllEmployee();
}
我的代码在上面。我想从saveEmployee()
EmployeeDAO
类中调用EmployeeManager
方法。我怎么能这样做?
答案 0 :(得分:0)
public class EmployeeManager {
public void save(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDAO employeeDao = (EmployeeDAO) ctx.getBean("edao");
List<Employee>list = employeeDao.getAllEmployee();
employeeDao.saveEmployee();
}
}