RowMapper接口

1.RowMapper接口是什么?

RowMapper接口是Spring框架提供的可以将数据库查询数据进行封装的接口,
包名地址:org.springframework.jdbc.core.RowMapper
通过实现接口,实现接口mapRow方法(),通过对数据的封装就是通过mapRow方法实现

public class NewsMapper implements RowMapper<FanNews> {

	@Override
		//RowMapper组件也可以自己编写,当ResultRet查询结果字段与实体类属性名不一致,需要采用此方法
	public FanNews mapRow(ResultSet res, int arg1) throws SQLException {
		//以编程方式将记录的字段值给实体属性进行封装
		// TODO Auto-generated method stub
		FanNews fn=new FanNews();
		fn.setId(res.getInt("id"));
		fn.setId(res.getInt("id"));
		return fn;
	}

}

2.什么是BeanPropertyRowMapper

这是Spring框架提供的BeanPropertyRowMapper这是对RowMapper的实现类,它可以把ResultSet和实体类的字段进行实现自动映射,可以给同名字段进行封装。自动将一行数据映射到指定类的实例, 首先将这个类实例化,然后通过名称匹配的方式,映射到属性中去。

	@Test
	public void test1() {
		//实例化spring容器
		String config="com/tracy/xml/applicationContext.xml";
		ApplicationContext acc=new ClassPathXmlApplicationContext(config);
		JdbcTemplate template=acc.getBean("jdbcTemplate",JdbcTemplate.class);
		System.out.println(template);
		
		  String sql="select *from fan_news";
		  System.out.println(FanNews.class);
		  List<FanNews>list=template.query(sql,new BeanPropertyRowMapper<FanNews>(FanNews.class));
		  System.out.println(list);
		  for(FanNews news:list) {
		  System.out.println(news.getFan_name()+news.getFan_item()); 

>

		  }
		 
	}

你可能感兴趣的:(Spring,spring,java)