这是我的控制器
package ledger.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import ledger.dao.EmployeeDao;
import ledger.model.EmployeeModel;
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@RequestMapping(value = "employee/save", method = RequestMethod.POST)
public ModelAndView employeeAdd(@ModelAttribute("employee")EmployeeModel employee) {
employeeDao.save(employee);
return new ModelAndView("redirect:/employee");
}
@RequestMapping(value="employee/delete/{id}",method = RequestMethod.GET)
public ModelAndView deleteEmployee(@PathVariable int id) {
employeeDao.delete(id);
return new ModelAndView("redirect:/employee");
}
@RequestMapping(value="employee/view",method = RequestMethod.GET)
public ModelAndView view(@PathVariable int id){
List<EmployeeModel> list = employeeDao.getEmployee();
ModelAndView mav = new ModelAndView();
mav.addObject("list",list);
mav.addObject("Employee",new EmployeeController());
mav.setViewName("Employee");
return mav;
}
}
这是我的Dao
package ledger.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import ledger.model.EmployeeModel;
import ledger.controllers.EmployeeController;
public class EmployeeDao {
JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
public int save(EmployeeModel g) {
String sql = "insert into TBL_EMPLOYEE (EMP_FNAME,EMP_MNAME,EMP_LNAME,EMP_AGE,EMP_CONTACTNUMBER,EMP_ADDRESS,EMP_EMAIL_ADDRESS) values('"+g.getEmp_fname()+"','"+g.getEmp_mname()+"','"+g.getEmp_lname()+"','"+g.getEmp_age()+"','"+g.getEmp_contact_number()+"','"+g.getEmp_address()+"','"+g.getEmp_email_address()+"')";
return template.update(sql);
}
public int delete(int id) {
String sql = "delete from TBL_EMPLOYEE where EMP_ID = "+id+"";
return template.update(sql);
}
public List<EmployeeModel> getEmployee() {
return template.query("select * from TBL_EMPLOYEE ", new RowMapper<EmployeeModel>(){
public EmployeeModel mapRow(ResultSet rs, int row) throws SQLException{
EmployeeModel g = new EmployeeModel();
g.setEmp_id(rs.getInt(1));
g.setEmp_fname(rs.getString(2));
g.setEmp_mname(rs.getString(3));
g.setEmp_lname(rs.getString(4));
g.setEmp_age(rs.getInt(5));
g.setEmp_contact_number(rs.getInt(6));
g.setEmp_address(rs.getString(7));
g.setEmp_email_address(rs.getString(8));
return g;
}
});
}
}
这是我的模特
package ledger.model;
import java.util.List;
public class EmployeeModel {
private int emp_id;
private String emp_fname;
private String emp_mname;
private String emp_lname;
private int emp_age;
private int emp_contact_number;
private String emp_address;
private String emp_email_address;
public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
public String getEmp_fname() {
return emp_fname;
}
public void setEmp_fname(String emp_fname) {
this.emp_fname = emp_fname;
}
public String getEmp_mname() {
return emp_mname;
}
public void setEmp_mname(String emp_mname) {
this.emp_mname = emp_mname;
}
public String getEmp_lname() {
return emp_lname;
}
public void setEmp_lname(String emp_lname) {
this.emp_lname = emp_lname;
}
public int getEmp_age() {
return emp_age;
}
public void setEmp_age(int emp_age) {
this.emp_age = emp_age;
}
public int getEmp_contact_number() {
return emp_contact_number;
}
public void setEmp_contact_number(int emp_contact_number) {
this.emp_contact_number = emp_contact_number;
}
public String getEmp_address() {
return emp_address;
}
public void setEmp_address(String emp_address) {
this.emp_address = emp_address;
}
public String getEmp_email_address() {
return emp_email_address;
}
public void setEmp_email_address(String emp_email_address) {
this.emp_email_address = emp_email_address;
}
}