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的更多相关文章

  1. [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction

    1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...

  2. Spring中JdbcTemplate中使用RowMapper

    转自:https://blog.csdn.net/u012661010/article/details/70049633 1 sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类 ...

  3. Spring中的JDBCTemplate

    src\dayday\JDBCTestTest package dayday;import com.sun.org.apache.xalan.internal.xsltc.compiler.Templ ...

  4. Spring 中的 JDBC 事务

    Spring 对 JDBC 的支持 JdbcTemplate 简介 •为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架. • ...

  5. spring框架总结(04)----介绍的是Spring中的JDBC模板

    1.1  Jdbc模板概述 它是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装.spring框架为我们提供了很多的操作模板类,入下图所示: 我们今天的主角在spring-jd ...

  6. Spring中使用JDBC

    Spring中的数据库异常体系 使用JDBC(不使用Spring)的时候,我们需要强制捕获SQLException,否则无法使用JDBC处理任何事情.SQLException表示尝试访问数据库的时候出 ...

  7. spring对数据库的操作、spring中事务管理的介绍与操作

    jdbcTemplate的入门 创建maven工程 此处省略 导入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/s ...

  8. 2018.12.25 Spring中JDBCTemplate模版API学习

    1 Spring整合JDBC模版 1.1 spring中土拱了一个可以操作数据库的对象.对象封装了jdbc技术 JDBCTemplateJDBC模板对象 1.2 与DBUtils中的QueryRunn ...

  9. Spring中的JDBC操作

    一.Spring模板JdbcTemplate 为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架JdbcTemplate. 作 ...

随机推荐

  1. [Ramda] Compose and Curry

    Curry: The idea of Curry is to spreate the data from the function. Using Curry to define the functio ...

  2. oc-09-#pragma mark指令的使用,用于查找代码

    // 3-[了解]#pragma mark指令的使用,用于查找代码. #import <Foundation/Foundation.h> //声明一个狗类 #pragma mark 声明狗 ...

  3. Linux守护进程(init.d和xinetd)

    http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html

  4. Getting started with Apache Camel--转载

    原文地址:http://www.javacodegeeks.com/2012/12/getting-started-with-apache-camel.html About Camel: Apache ...

  5. Helpers\TableBuilder

    Helpers\TableBuilder Table builder helper is a class that would help you to create tables in MySQL ( ...

  6. 小白日记12:kali渗透测试之服务扫描(二)-SMB扫描

    SMB扫描 Server Message Block 协议.与其他标准的TCP/IP协议不同,SMB协议是一种复杂的协议,因为随着Windows计算机的开发,越来越多的功能被加入到协议中去了,很难区分 ...

  7. Node.js module.exports和exports的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...

  8. jQuery 插件写法2

    转载:http://www.xuanfengge.com/jquery-plug-in-written-summary-and-summary-of-writing-object-oriented-m ...

  9. 利用传感器(sensor)实现微信摇一摇动画

    所需要的权限: <uses-permission android:name="android.permission.VIBRATE"></uses-permiss ...

  10. Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果

    Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 目前Edge的功能尚 ...