JdbcTemplate(三)

1.UserInfoDao类中getById简化

未简化:

public UserInfo getById(int id) {
		String sql = "select id,user_name ,password from user_info1 where id =?";	
		
		//创建一个内部类 获取单个
		/*class UserInfoRowMapper implements RowMapper<UserInfo>{
			public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
				
				UserInfo userInfo = new UserInfo();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserName(rs.getString("user_name"));
				userInfo.setPassword(rs.getString("password"));
				return userInfo;
			}
			
		}*/

简化:
 

public UserInfo getById(int id) {
		String sql = "select id,user_name ,password from user_info1 where id =?";	
		//下面是简化版
		 RowMapper rowMapper = (ResultSet rs,int rowNum)->{	
				UserInfo userInfo = new UserInfo();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserName(rs.getString("user_name"));
				userInfo.setPassword(rs.getString("password"));
				return userInfo;
		};
		
		
		return (UserInfo) jdbcTemplate.queryForObject(sql, rowMapper,id);
	}

2.这里获取表中一共有多少个id

UserInfoDao类中写selectAll()

public List<UserInfo> selectAll(){
		String sql = "select id,user_name ,password from user_info1 ";	
		return jdbcTemplate.query(sql, (ResultSet rs, int rowNum)->{
			UserInfo userInfo = new UserInfo();
			userInfo.setId(rs.getInt("id"));
			userInfo.setUserName(rs.getString("user_name"));
			userInfo.setPassword(rs.getString("password"));
			return userInfo;
		});
	}

test中改为

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.userinfo.dao.UserInfoDao;
import com.jd.vo.UserInfo;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("application.xml");
		//直接操作Dao层
		UserInfoDao userDao =  applicationContext.getBean(UserInfoDao.class);
		
		//这里有改动  查询单个id
		//UserInfo userInfo1 =  userDao.getById(2);
		
		//获取总的id数
		int size = userDao.selectAll().size();
		System.out.println(size);
		applicationContext.close();
	}
	//运行出错是因为还少一个aop包
}

运行结果如下:

JdbcTemplate(三)

3.下面还有一种方法可以查询单个还可以防止sql注入:

//创建一个内部类  ResultSetExtractor这个接口与前面有区别  可以避免sql注入
		class UserInfoResultSet implements ResultSetExtractor<UserInfo>{
			@Override
			public UserInfo extractData(ResultSet rs) throws SQLException, DataAccessException {
				UserInfo userInfo = null;
				if(rs.next()) {
				    userInfo = new UserInfo();
					userInfo.setId(rs.getInt("id"));
					userInfo.setUserName(rs.getString("user_name"));
					userInfo.setPassword(rs.getString("password"));
				}
				return userInfo;
			}
		}
		return jdbcTemplate.query(sql, new UserInfoResultSet(),id);	
	}

test中改为:

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.userinfo.dao.UserInfoDao;
import com.jd.vo.UserInfo;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("application.xml");
		//直接操作Dao层
		UserInfoDao userDao =  applicationContext.getBean(UserInfoDao.class);
		
		//这里有改动  查询单个id
		//UserInfo userInfo1 =  userDao.getById(2);
		
		//获取总的id数
		/*int size = userDao.selectAll().size();
		System.out.println(size);*/
		
		UserInfo userInfo1 =  userDao.getById(2);
		System.out.println(userInfo1);
		
		applicationContext.close();
	}
	//运行出错是因为还少一个aop包
}

运行结果是:

JdbcTemplate(三)

3.采用list集合

UserInfoDao中:

//下面代码还可以简化
		/*class UserInfoResultSet implements ResultSetExtractor<List<UserInfo>>{
			
			@Override
			public List<UserInfo> extractData(ResultSet rs) throws SQLException, DataAccessException {
				List<UserInfo> list = new ArrayList<>();
				while(rs.next()) {
					UserInfo userInfo= new UserInfo();
					userInfo.setId(rs.getInt("id"));
					userInfo.setUserName(rs.getString("user_name"));
					userInfo.setPassword(rs.getString("password"));
					list.add(userInfo);
				}
				return list;
			}
		}
		return jdbcTemplate.query(sql, new UserInfoResultSet());*/
		return jdbcTemplate.query(sql, (ResultSet rs)->{
		  List<UserInfo> list = new ArrayList<>();
		  while(rs.next()) {
			    UserInfo userInfo = new UserInfo();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserName(rs.getString("user_name"));
				userInfo.setPassword(rs.getString("password"));
				list.add(userInfo);
		  }
			return list;
		});
	}

test类

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.userinfo.dao.UserInfoDao;
import com.jd.vo.UserInfo;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("application.xml");
		//直接操作Dao层
		UserInfoDao userDao =  applicationContext.getBean(UserInfoDao.class);
		
		//这里有改动  查询单个id
		//UserInfo userInfo1 =  userDao.getById(2);
		
		//获取总的id数
		int size = userDao.selectAll().size();
		System.out.println(size);
		
		/*UserInfo userInfo1 =  userDao.getById(2);
		System.out.println(userInfo1);*/
		
		applicationContext.close();
	}
	//运行出错是因为还少一个aop包
}

 

运行结果:

JdbcTemplate(三)

 

 

 

 

 

 

 

JdbcTemplate(三)JdbcTemplate(三) 千寻001 发布了15 篇原创文章 · 获赞 7 · 访问量 2244 私信 关注
上一篇:五、JDBC连接池&JDBCTemplate使用


下一篇:第四篇:SpringBoot 数据持久化之JdbcTemplate