Spring+SimpleJdbcTemplate+SimpleJdbcDaoSupport+SimpleJdbcInsert+SimpleJdbcCall

支持泛型、varargs特性。spring的JDBC集成提供了相应的SimpleJdbcTemplate与SimpleJdbcDaoSupport。
DI容器可以通过构建器将DataSource、JdbcTemplate、NamedParameterJdbcTemplate等对象传给它,从而准备好
SimpleJdbcTemplate实例。
为了使用泛型带来的优势,Spring提供了ParameterizedRowMapper回调接口。
配置示例如下:
 <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
   <constructor-arg ref="jdbcTemplate"></constructor-arg>
 </bean>
回调示例如下:
ListableBeanFactory context = new ClassPathXmlApplicationContext(
    "beans.xml");
//GenericBeanFactoryAccessor dbfa = new GenericBeanFactoryAccessor(context);
SimpleJdbcTemplate jdbc = (SimpleJdbcTemplate) context.getBean("simpleJdbcTemplate");
List<Person> perList = jdbc.query("select * from porder limit 10", new ParameterizedRowMapper<Person>(){
 public Person mapRow(ResultSet rs,int rowNum) throws SQLException{
  Person pers = new Person();
  pers.setName(rs.getString("name"));
  return pers;
 }
});
System.out.println(perList);

ParameterizedBeanPropertyRowMapper和ParameterizedSingleColumnRowMapper是Spring内置的ParameterizedRowMapper
的实现类。ParameterizedBeanPropertyRowMapper类的示例如下:他会映射成单个的Person对象。
SimpleJdbcTemplate jdbc = (SimpleJdbcTemplate) context.getBean("simpleJdbcTemplate");
List<Person> plist = jdbc.query("select name from porder",ParameterizedBeanPropertyRowMapper.newInstance(Person.class));
System.out.println(plist);
ParameterizedSingleColumnRowMapper适合于集合中仅仅存在单列的情况,他会将单列的值取出来。
示例如下:
SimpleJdbcTemplate jdbc = (SimpleJdbcTemplate) context.getBean("simpleJdbcTemplate");
List<String> plist = jdbc.query("select name from porder",ParameterizedSingleColumnRowMapper.newInstance(String.class));
System.out.println(plist);

SimpleJdbcDaoSupport支持类:
SimpleJdbcDaoSupport支持类用于简化SimpleJdbcTemplate的使用。下面展示了继承与SimpleJdbcDaoSupport的
SimpleJdbcDaoSupportImpl类。DI容器需要为它注入DataSource或JdbcTemplate对象。
<bean id="simpleJdbcDaoSupportImpl" class="com.spring.test.SimpleJdbcDaoSupportImpl">
 <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
SimpleJdbcDaoSupportImpl实现类示例如下:
public class SimpleJdbcDaoSupportImpl extends SimpleJdbcDaoSupport {
   public void test(){
 System.out.println(this.getSimpleJdbcTemplate().queryForInt(
   "select count(*) from porder where name=?",
   "李度"));
   }
}
测试类如下:
ListableBeanFactory context = new ClassPathXmlApplicationContext(
    "beans.xml");
//GenericBeanFactoryAccessor dbfa = new GenericBeanFactoryAccessor(context);
SimpleJdbcDaoSupportImpl jdbc = (SimpleJdbcDaoSupportImpl) context.getBean("simpleJdbcDaoSupportImpl");
jdbc.test();

SimpleJdbcInsert用于操作表,而SimpleJdbcCall用于操作存储过程或函数。不用于前面介绍的各种模板。
默认时需要从底层JDBC驱动获得RDBMS相关元数据,尤其是SimpleJdbcCall。
不用写SQL语句,通过withTableName()方法指定记录待插入的表,通过SqlParameterSource或Map对象能够指定记录。
随后,通过触发execute()方法能够将记录存储到表中。
示例代码如下:
ListableBeanFactory context = new ClassPathXmlApplicationContext(
    "beans.xml");
DataSource dataSource = (DataSource)context.getBean("dataSource");
SimpleJdbcInsert tempInsert = new SimpleJdbcInsert(dataSource);
tempInsert.withTableName("temp_no");
Map<String, Object> map = new HashMap<String,Object>();
map.put("no", "no");
map.put("color", "color");
tempInsert.execute(map);
使用usingGeneratedKeyColumns()方法自动生成主键。如
tempInsert.withTableName("temp_no").usingGeneratedKeyColumns("id");
此时SQL语句中将不会考虑id字段。
可以使用executeAndReturnKey(params)或executeReturnKeyHolder();返回主键的值。
使用usingColumns("username","password")考虑显示指定的列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值