alibaba连接池druid的使用案例

配置文件的源代码


	
	
	
		
		
		
		
	
	
	
		
	
	
	
		
	
	
	
		
	
	

Dao代码

public interface UserDao {
	/**
	 * 查找所有用户信息
	 * @return
	 */
	List findAll();
	/**
	 * 通过用户ID查找用户信息
	 */
	Userinfo getUserinfoByID(Integer userID);
	/**
	 * 添加用户信息
	 * @param user
	 * @return
	 */
	Integer add(Userinfo user);
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	Integer update(Userinfo user);
	/**
	 * 通过ID删除用户
	 * @param userinfoID
	 * @return
	 */
	Integer delete(Integer userinfoID);
}

DaoImpl类代码

public class UserDaoImpl implements UserDao {
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public List findAll() {
		String sql="select * from userinfo";
		List list = jdbcTemplate.query(sql, new MyRowMapper());
		return list;
	}
	
	public Userinfo getUserinfoByID(Integer userID) {
	
		String sql = "select * from userinfo where userID = ?";
		Userinfo user = jdbcTemplate.queryForObject(sql, new MyRowMapper(), userID);
		return user;
	}
	
	public Integer add(Userinfo user) {
		// TODO Auto-generated method stub
		String sql = "insert into userinfo(userName,userPwd) values(?,?)";
		int i = jdbcTemplate.update(sql, user.getUserName(),user.getUserPwd());
		return i;
	}
	public Integer update(Userinfo user) {

		String sql = "update userinfo set userName = ? ,userPwd = ? where userID = ?";
		int i = jdbcTemplate.update(sql, user.getUserName(),user.getUserPwd(),user.getUserID());
		return i;
	}
	
	public Integer delete(Integer userID) {
		
		String sql = "delete from userinfo where userID = ?";
		int i = jdbcTemplate.update(sql, userID);
		return i;
	}

}


class MyRowMapper implements RowMapper{

	public Userinfo mapRow(ResultSet rs, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		Userinfo user = new Userinfo();
		user.setUserID(rs.getInt("userID"));
		user.setUserName(rs.getString("userName"));
		user.setUserPwd(rs.getString("userPwd"));
		return user;
	}
	
}

Service代码

public interface UserService {
	/**
	 * 查找所有用户信息
	 * @return
	 */
	List findAll();
	/**
	 * 通过用户ID查找用户信息
	 */
	Userinfo getUserinfoByID(Integer userID);
	/**
	 * 添加用户信息
	 * @param user
	 * @return
	 */
	Integer add(Userinfo user);
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	Integer update(Userinfo user);
	/**
	 * 通过ID删除用户
	 * @param userinfoID
	 * @return
	 */
	Integer delete(Integer userinfoID);
}

ServiceImpl代码

public class UserServiceImpl implements UserService {
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public List findAll() {
		// TODO Auto-generated method stub
		return userDao.findAll();
	}

	/* (non-Javadoc)
	 * @see com.zhang.service.UserService#getUserinfoByID(java.lang.Integer)
	 */
	public Userinfo getUserinfoByID(Integer userID) {
		// TODO Auto-generated method stub
		return userDao.getUserinfoByID(userID);
	}

	/* (non-Javadoc)
	 * @see com.zhang.service.UserService#add(com.zhang.entity.Userinfo)
	 */
	public Integer add(Userinfo user) {
		// TODO Auto-generated method stub
		return userDao.add(user);
	}

	/* (non-Javadoc)
	 * @see com.zhang.service.UserService#update(com.zhang.entity.Userinfo)
	 */
	public Integer update(Userinfo user) {
		// TODO Auto-generated method stub
		return userDao.update(user);
	}

	/* (non-Javadoc)
	 * @see com.zhang.service.UserService#delete(java.lang.Integer)
	 */
	public Integer delete(Integer userinfoID) {
		// TODO Auto-generated method stub
		return userDao.delete(userinfoID);
	}

}

测试类代码

public class TestUser {
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = ac.getBean("userService", UserService.class);
		Userinfo user = new Userinfo();
		user.setUserName("zhangsan");
		user.setUserPwd("123");
		user.setUserID(2);
//		Integer add = userService.add(user);
//		System.out.println(add);
//		Integer d = userService.delete(1);
//		System.out.println(d);
//		Integer ii = userService.update(user);
//		System.out.println(ii);
		List list = userService.findAll();
		for(Userinfo u : list){
			System.out.println(u);
		}
	}

}

只是学习使用连接池,连接池在访问数据库量大的情况下,才能体现出其优点。

另一个重点时spring的IOC(控制反转)和DI(依赖注入)的练习使用,IOC的本质是将原先对象的生命周期的控制权从调用者手中转移到spring容器手中,它所实现的方式就是依靠DI(依赖注入)。

原先在

你可能感兴趣的:(alibaba连接池druid的使用案例)