一般查询实体的时候,都需要这么使用:

 /** *//**
/** *//**
 * 根据id查询
     * 根据id查询
 *
     * 
 * @return
     * @return
 */
     */
 public Emp queryEmpById(Integer id)
    public Emp queryEmpById(Integer id)

 
     {
{
 String sql = "select * from emp where empno = ?";
        String sql = "select * from emp where empno = ?";
 ParameterizedRowMapper<Emp> mapper = new ParameterizedRowMapper<Emp>()
        ParameterizedRowMapper<Emp> mapper = new ParameterizedRowMapper<Emp>()

 
         {
{

 public Emp mapRow(ResultSet rs, int rowNum) throws SQLException
            public Emp mapRow(ResultSet rs, int rowNum) throws SQLException

 
             {
{
 Emp emp = new Emp();
                Emp emp = new Emp();
 System.out.println("row:" + rowNum);
                System.out.println("row:" + rowNum);
 emp.setEmpno(rs.getInt("empno"));
                emp.setEmpno(rs.getInt("empno"));
 emp.setEname(rs.getString("ename"));
                emp.setEname(rs.getString("ename"));
 return emp;
                return emp;
 }
            }
 };
        };

 return this.getSimpleJdbcTemplate().queryForObject(sql, mapper, id);
        return this.getSimpleJdbcTemplate().queryForObject(sql, mapper, id);
 }
    }
能不能像Hibernate那样自动set这些值呢,用反射可以实现.
 package orm;
package orm;

 import java.lang.reflect.Field;
import java.lang.reflect.Field;
 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.SQLException;
import java.sql.SQLException;
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;


 /** *//**
/** *//**
 * 通用的Object包装类(类型问题,依然是个瓶颈,如果有好的解决方案请pm我)
 * 通用的Object包装类(类型问题,依然是个瓶颈,如果有好的解决方案请pm我)
 *
 * 
 * 功能:查询对象类型或对象集合时的通用包装类
 * 功能:查询对象类型或对象集合时的通用包装类
 *
 * 
 * @author zdw
 * @author zdw
 *
 * 
 */
 */
 @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked")
 public class ObjectMapper implements ParameterizedRowMapper
public class ObjectMapper implements ParameterizedRowMapper


 {
{
 private Class clazz;
    private Class clazz;

 public ObjectMapper(Class clazz)
    public ObjectMapper(Class clazz)

 
     {
{
 this.clazz = clazz;
        this.clazz = clazz;
 }
    }


 /** *//**
    /** *//**
 * 重写mapRow方法
     * 重写mapRow方法
 */
     */
 @Override
    @Override
 public Object mapRow(ResultSet rs, int rowNum) throws SQLException
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException

 
     {
{
 try
        try

 
         {
{
 Object obj = clazz.newInstance();
            Object obj = clazz.newInstance();
 Field fields[] = obj.getClass().getDeclaredFields();
            Field fields[] = obj.getClass().getDeclaredFields();
 for (int i = 0; i < fields.length; i++)
            for (int i = 0; i < fields.length; i++)

 
             {
{
 Field field = fields[i];
                Field field = fields[i];
 // 暴力访问
                // 暴力访问
 field.setAccessible(true);
                field.setAccessible(true);
 this.typeMapper(field, obj, rs);
                this.typeMapper(field, obj, rs);
 // 恢复默认
                // 恢复默认
 field.setAccessible(false);
                field.setAccessible(false);
 }
            }
 return obj;
            return obj;
 }
        }
 catch (Exception e)
        catch (Exception e)

 
         {
{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
 return null;
        return null;
 }
    }


 /** *//**
    /** *//**
 * 数据类型包装器
     * 数据类型包装器
 *
     * 
 * @param field
     * @param field
 *            目标属性
     *            目标属性
 * @param obj
     * @param obj
 *            目标对象
     *            目标对象
 * @param rs
     * @param rs
 *            结果集
     *            结果集
 * @throws Exception
     * @throws Exception
 */
     */
 private void typeMapper(Field field, Object obj, ResultSet rs)
    private void typeMapper(Field field, Object obj, ResultSet rs)
 throws Exception
            throws Exception

 
     {
{
 String type = field.getType().getName();
        String type = field.getType().getName();
 if (type.equals("java.lang.String"))
        if (type.equals("java.lang.String"))

 
         {
{
 field.set(obj, rs.getString(field.getName()));
            field.set(obj, rs.getString(field.getName()));
 }
        }
 else if (type.equals("int") || type.equals("java.lang.Integer"))
        else if (type.equals("int") || type.equals("java.lang.Integer"))

 
         {
{
 field.set(obj, rs.getInt(field.getName()));
            field.set(obj, rs.getInt(field.getName()));
 }
        }
 else if (type.equals("long") || type.equals("java.lang.Long"))
        else if (type.equals("long") || type.equals("java.lang.Long"))

 
         {
{
 field.set(obj, rs.getLong(field.getName()));
            field.set(obj, rs.getLong(field.getName()));
 }
        }
 else if (type.equals("boolean") || type.equals("java.lang.Boolean"))
        else if (type.equals("boolean") || type.equals("java.lang.Boolean"))

 
         {
{
 field.set(obj, rs.getBoolean(field.getName()));
            field.set(obj, rs.getBoolean(field.getName()));
 }
        }
 else if (type.equals("java.util.Date"))
        else if (type.equals("java.util.Date"))

 
         {
{
 field.set(obj, rs.getDate(field.getName()));
            field.set(obj, rs.getDate(field.getName()));
 }
        }
 }
    }
 }
}

dao:

 /** *//**
/** *//**
 * 查询操作 (自动setEmp类型所有值)
     * 查询操作 (自动setEmp类型所有值)
 *
     * 
 * @return
     * @return
 */
     */
 public List queryList()
    public List queryList()

 
     {
{
 return this.getJdbcTemplate().query("select * from emp",
        return this.getJdbcTemplate().query("select * from emp",
 new ObjectMapper(Emp.class));
                new ObjectMapper(Emp.class));
 }
    }

单个查询:
 public Emp queryEmpById2(Integer id)
public Emp queryEmpById2(Integer id)

 
     {
{
 String sql = "select * from emp where empno = ?";
        String sql = "select * from emp where empno = ?";
 ObjectMapper om = new ObjectMapper(Emp.class);
        ObjectMapper om = new ObjectMapper(Emp.class);
 return (Emp) this.getSimpleJdbcTemplate().queryForObject(sql, om, id);
        return (Emp) this.getSimpleJdbcTemplate().queryForObject(sql, om, id);
 }
    }
测试通过:
 7369
7369
 7499
7499
 7521
7521
 7566
7566
 7654
7654
 7698
7698
 7782
7782
 7788
7788
 7839
7839
 7844
7844
上面是我的一个简单封装,在Spring2.5中及以后版本,已经提供了便捷方法:

 /** *//**
/** *//**
 * 查询操作 (自动setEmp类型所有值)
     * 查询操作 (自动setEmp类型所有值)
 *
     * 
 * @return
     * @return
 */
     */
 public List queryList()
    public List queryList()

 
     {
{
 return this.getSimpleJdbcTemplate().query(
        return this.getSimpleJdbcTemplate().query(   
 "SELECT * from emp",
                "SELECT * from emp",   
 ParameterizedBeanPropertyRowMapper.newInstance(Emp.class));
                ParameterizedBeanPropertyRowMapper.newInstance(Emp.class));  
 }
    }
 
    

 /** *//**
    /** *//**
 * 根据id查询
     * 根据id查询
 *
     * 
 * @return
     * @return
 */
     */
 public Emp queryById(Integer id)
    public Emp queryById(Integer id)

 
     {
{
 return this.getSimpleJdbcTemplate().queryForObject(
        return this.getSimpleJdbcTemplate().queryForObject(   
 "SELECT * from emp where id = ?",
                "SELECT * from emp where id = ?",   
 ParameterizedBeanPropertyRowMapper.newInstance(Emp.class),7369);
                ParameterizedBeanPropertyRowMapper.newInstance(Emp.class),7369);  
 }
    }
这样就简单多了,也是用反射实现的.