2 qq 34052254 qq_34052254 于 2017.01.09 09:37 提问

求个基于SPRING MVC的于oracle 数据库的CRUD泛型实例

基础的连接已经学会了。现在求各位大大指教下泛型的CRUD怎么写。

public class Layer_tbl {
    public int layerKeyID_grp;
    public String layerElementID_grp;
    public String layerId_grp;
    public int projectId_grp;
    public int layerPid_grp;
}


上面是实体类。需要实现的方法 通常的增删改 单个查询和表全展示。
谢谢0 0.

1个回答

engchina
engchina   Rxr 2017.01.09 10:30
已采纳

example refer url:http://www.javatpoint.com/spring-mvc-crud-example

Emp.java

package com.javatpoint.beans;  

public class Emp {  
private int id;  
private String name;  
private float salary;  
private String designation;  

public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public float getSalary() {  
    return salary;  
}  
public void setSalary(float salary) {  
    this.salary = salary;  
}  
public String getDesignation() {  
    return designation;  
}  
public void setDesignation(String designation) {  
    this.designation = designation;  
}  

}  

EmpDao.java

package com.javatpoint.dao;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.List;  
import org.springframework.jdbc.core.BeanPropertyRowMapper;  
import org.springframework.jdbc.core.JdbcTemplate;  
import org.springframework.jdbc.core.RowMapper;  
import com.javatpoint.beans.Emp;  

public class EmpDao {  
JdbcTemplate template;  

public void setTemplate(JdbcTemplate template) {  
    this.template = template;  
}  
public int save(Emp p){  
    String sql="insert into Emp99(name,salary,designation)   
          values('"+p.getName()+"',"+p.getSalary()+",'"+p.getDesignation()+"')";  
    return template.update(sql);  
}  
public int update(Emp p){  
    String sql="update Emp99 set name='"+p.getName()+"', salary="+p.getSalary()+",   
          designation='"+p.getDesignation()+"' where id="+p.getId()+"";  
    return template.update(sql);  
}  
public int delete(int id){  
    String sql="delete from Emp99 where id="+id+"";  
    return template.update(sql);  
}  
public Emp getEmpById(int id){  
    String sql="select * from Emp99 where id=?";  
    return template.queryForObject(sql, new Object[]{id},new BeanPropertyRowMapper<Emp>(Emp.class));  
}  
public List<Emp> getEmployees(){  
    return template.query("select * from Emp99",new RowMapper<Emp>(){  
        public Emp mapRow(ResultSet rs, int row) throws SQLException {  
            Emp e=new Emp();  
            e.setId(rs.getInt(1));  
            e.setName(rs.getString(2));  
            e.setSalary(rs.getFloat(3));  
            e.setDesignation(rs.getString(4));  
            return e;  
        }  
    });  
}  
}  

是你想要的吗?

Csdn user default icon
上传中...
上传图片
插入图片