Complete with JdbcTemplate method, query database tables, and return all data of user table sw_user to List < User > set.
In the JdbcTemplateDemo class, add queryAllObject() to the method that queries return all sets of objects.
Data source DriverManager Data Source remains unchanged
The method of jdbcTemplate has changed, using query(sql, new MyRowMapper());
The second parameter is an implementation class of the interface RowMapper < User >, which can use anonymous inner classes. I use a separate MyRowMapper class object.
It does only two things to implement the overloaded method mapRow(ResultSet rs, int num)
Use ResultSet to get the required User object data
The object is encapsulated with User and returned
The browser gets the query effect map:
JdbcTemplateDemo class:
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; } } //No anonymous inner classes are used. Note MyRowMapper No later<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; } }
The Servlet class calls the JdbcTemplateDemo class
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()); //Use JdbcTemplat Of queryForObject Method 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); } }
The xml configuration files used in the previous essays and related packages