Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合
用JdbcTemplate的方法完成, 查询数据库表,把用户表sw_user所有数据以List<User>集合返回
在JdbcTemplateDemo类中增加查询返回所有对象集合的方法queryAllObject()
数据源DriverManagerDataSource不变
jdbcTemplate的方法有所改变,使用query(sql, new MyRowMapper());
其中第二个参数是一个接口RowMapper<User>的实现类,可以使用匿名内部类,我使用的是单独的类MyRowMapper的对象
它要实现重载的方法mapRow(ResultSet rs, int num)只做两件事
一用ResultSet得到所需User对象数据
二用User进行封装得到对象并返回
浏览器得到查询效果图:
JdbcTemplateDemo类:
package com.swift; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.stereotype.Component; @Component(value="jdbcTemplateDemo") public class JdbcTemplateDemo { public List<User> queryAllObject(){ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("root"); dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="select * from sw_user"; List<User> list=jdbcTemplate.query(sql, new MyRowMapper()); return list; } public User queryOneObject() { DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database"); dataSource.setUsername("root"); dataSource.setPassword("root"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="select * from sw_user where username=? and password=?"; User user=jdbcTemplate.queryForObject(sql, new MyRowMapper(), "zhangsan","123456"); return user; } public boolean delete(String username) { DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database"); dataSource.setUsername("root"); dataSource.setPassword("root"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); int count=jdbcTemplate.update("delete from sw_user where username=?", username); if(count!=0) { return true; } return false; } } //没有用匿名内部类,注意MyRowMapper后没有<User> class MyRowMapper implements RowMapper<User> { @Override public User mapRow(ResultSet rs, int num) throws SQLException { String username=rs.getString("username"); String password=rs.getString("password"); User user=new User(username,password); return user; } }
Servlet类调用该JdbcTemplateDemo类
package com.swift; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet("/demo") public class ServletDemo extends HttpServlet { private static final long serialVersionUID = 1L; public ServletDemo() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); //使用JdbcTemplat的queryForObject方法 ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml"); JdbcTemplateDemo jdbcTemplateDemo=(JdbcTemplateDemo) context.getBean("jdbcTemplateDemo"); List<User> list=jdbcTemplateDemo.queryAllObject(); for(User user:list) { response.getWriter().append(user.toString()); response.getWriter().println(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
所用到的xml配置文件在前面随笔中及相关包
Never waste time any more, Never old man be a yong man