spring 中的 RowMapper
spring 中的 RowMapper
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学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...
- Spring中JdbcTemplate中使用RowMapper
转自:https://blog.csdn.net/u012661010/article/details/70049633 1 sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类 ...
- Spring中的JDBCTemplate
src\dayday\JDBCTestTest package dayday;import com.sun.org.apache.xalan.internal.xsltc.compiler.Templ ...
- Spring 中的 JDBC 事务
Spring 对 JDBC 的支持 JdbcTemplate 简介 •为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架. • ...
- spring框架总结(04)----介绍的是Spring中的JDBC模板
1.1 Jdbc模板概述 它是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装.spring框架为我们提供了很多的操作模板类,入下图所示: 我们今天的主角在spring-jd ...
- Spring中使用JDBC
Spring中的数据库异常体系 使用JDBC(不使用Spring)的时候,我们需要强制捕获SQLException,否则无法使用JDBC处理任何事情.SQLException表示尝试访问数据库的时候出 ...
- spring对数据库的操作、spring中事务管理的介绍与操作
jdbcTemplate的入门 创建maven工程 此处省略 导入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/s ...
- 2018.12.25 Spring中JDBCTemplate模版API学习
1 Spring整合JDBC模版 1.1 spring中土拱了一个可以操作数据库的对象.对象封装了jdbc技术 JDBCTemplateJDBC模板对象 1.2 与DBUtils中的QueryRunn ...
- Spring中的JDBC操作
一.Spring模板JdbcTemplate 为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架JdbcTemplate. 作 ...
随机推荐
- [AngularJS] Directive using another directive by 'require'
Directive can use another directive though 'require' keyword. angular.module('docsTabsExample', []) ...
- 图解JS原型链
一:任何一个对象都有一个prototype的属性,在js中可以把它记为:__proto__ 当初ECMAscript的发明者为了简化这门语言,同时又保持继承的属性,于是就设计了这个链表.. 在数据结构 ...
- oc-06-无参方法的调用
// 12-[掌握]无参方法声明实现及调用 #import <Foundation/Foundation.h> //类的声明 @interface Person : NSObject { ...
- ptrace x64 转
#include <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include < ...
- vim替换命令
转载:http://blog.csdn.net/glorin/article/details/6317098 替換(substitute) :[range]s/pattern/string/[c,e, ...
- Cheap CK100 1024 tokens NXP FIX Chip on Eobd2
CK100 is a well-known and cost-effective key programmer for many cars. Some said it is a must for bo ...
- Android(java)学习笔记79:java中InetAddress类概述和使用
要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机. 在TCP/IP协议中,这个标识号就是IP地址. 那么,我们如果获取和操作I ...
- android源码编译过程
1.下载好android源码包. 2.装好vm,ubuntu(如果能在实体机装linux更好). 3.安装所需要的deb包 在终端执行如下命令: sudo apt-get install flex b ...
- MPMoviePlayerController过期导致视频播放时间条颠倒及AVPlayerViewController的用法
MPMoviePlayerController虽然好用 但是过期了 所以可能会导致一些莫名的问题 ,比如说下面时间条的问题 但我们可以使用AVPlayerViewController来实现相应的想过 ...
- mysql中文乱码的完美解决方案
问题描述: mysql插入中文时显示为乱码或"?"号 解决方案: 修改mysql的my.ini配置 [mysql] default_character_set=utf8 [mysq ...