spring 中的 RowMapper
来源:互联网 发布:淘宝客白菜qq群推广 编辑:程序博客网 时间:2024/06/07 06:06
sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类.
我们在数据库查询中,如果返回的类型是用户自定义的类型(其实我们在数据库查询中大部分返回的都是自定义的类)则需要包装,如果是Java自定义的类型,如:String则不需要.
如果sping与hibernate 相结合了,基本上是用不到,大多数都是在spring单独使用时用到.
可以通过建立内部类实现RowMapper接口,RowMapper中有一个mapRow方法,所以实现RowMapper接口一定要实现mapRow方法,而对自定义类的包装就在mapRow方法中实现.
这里只是一个简单的例子:
public class TestDao {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
public List<TNpc> getAll(){
String sql = "select * from t_npc";
//使用
List list = jt.query(sql, new NpcRowMapper());
return list;
}
/**
* 定义内部类实现RowMapper接口
*/
public class NpcRowMapper implements RowMapper{
//实现mapRow方法
public Object mapRow(ResultSet rs, int num) throws SQLException {
//对类进行封装
TNpc npc = new TNpc();
npc.setId(rs.getLong("id"));
npc.setName(rs.getString("name"));
return npc;
}
}
- 使用spring中的Rowmapper
- Spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- Spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper 类
- 学习记录--spring中的RowMapper
- Spring-RowMapper
- Spring-RowMapper
- Spring-RowMapper
- Spring--RowMapper
- Spring JdbcTemplate 查询方法中的RowMapper实现汇总
- Spring JdbcTemplate 查询方法中的RowMapper实现汇总
- hashCode的性能优化
- Maven实战
- Java华丽转身——浅识J2EE十三标准
- html5论坛 html5现在的发展状况
- Android(Java)删除非空文件夹
- spring 中的 RowMapper
- AdditionQuiz
- Unity3D入门 :给物体增加刚体属性
- 谁说C语言很简单?---from coolshell
- Linux 下安装配置 JDK7
- java生成可执行文件
- ALAsset是什么?
- ruby on rails 4 中利用 remote:true 实现 ajax ,小记
- jQuery.validate插件