首页>代码>轻量级apache dbutil搭建的一个demo,如果嫌hibernate等ORM框架太重的话可以考虑dbutil>/000_DBUtil/java/user/dao/UserDAO.java
                
                package user.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;
import user.domain.User;
import user.exception.SQLException;
import user.web.utils.JDBCUtils;
public class UserDAO {
	static QueryRunner quer = new QueryRunner(JDBCUtils.getDataSource());
	public User findByLogonNameAndLogonPwd(User user) {
		String sql = "select * farom s_user where  logonName =? and logonPwd=?";
		User logonUser;
		try {
			logonUser = quer.query(sql, new BeanHandler<User>(User.class),
					user.getLogonName(), user.getLogonPwd());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SQLException("SQL有错");
		}
		return logonUser;
	}
	public void save(User u) {
		String sql = "insert into s_user values(null,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] args = { u.getUserName(), u.getLogonName(), u.getLogonPwd(),
				u.getSex(), u.getBirthday(), u.getEducation(),
				u.getTelephone(), u.getInterest(), u.getPath(),
				u.getFilename(), u.getRemark() };
		try {
			quer.update(sql, args);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	public static List<User> findList(User u) {
		String sql = "select * from s_user where 1=1  ";
		List argList = new ArrayList();
		if (StringUtils.isNotBlank(u.getUserName())) {
			sql += "and userName like ?";
			argList.add("%" + u.getUserName() + "%");
		}
		if (StringUtils.isNotBlank(u.getSex())) {
			sql += " and sex=? ";
			argList.add(u.getSex());
		}
		if (StringUtils.isNotBlank(u.getEducation())) {
			sql += " and education=? ";
			argList.add(u.getEducation());
		}
		if (StringUtils.isNotBlank(u.getIsUpload())) {
			if (u.getIsUpload().equals("1")) {
				sql += " and filename is not null";
			} else if (u.getIsUpload().equals("2")) {
				sql += " and filename is null";
			}
		}
		List<User> users = null;
		try {
			users = quer.query(sql, new BeanListHandler<User>(User.class),
					argList.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return users;
	}
	public void delete(User u) {
		String sql = "delete from s_user where userID=?";
		try {
			quer.update(sql, u.getUserID());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public User findById(int userID) {
		String sql = "select * from s_user where userID=?";
		User u = null;
		try {
			u = quer.query(sql, new BeanHandler<User>(User.class), userID);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return u;
	}
	public void update(User u, boolean isEditUpload) {
		String sql = "";
		if (isEditUpload) {
			sql = "update s_user set userName=?,logonName=?,logonPwd=?,sex=?,birthday=?,education=?,telephone=?,interest=?,path=?,filename=?,remark=? where userID=?";
			Object[] args = { u.getUserName(), u.getLogonName(),
					u.getLogonPwd(), u.getSex(), u.getBirthday(),
					u.getEducation(), u.getTelephone(), u.getInterest(),
					u.getPath(), u.getFilename(), u.getRemark(), u.getUserID() };
			updateMethod(sql, args);
		} else {
			sql = "update s_user set userName=?,logonName=?,logonPwd=?,sex=?,birthday=?,education=?,telephone=?,interest=?,remark=? where userID=?";
			Object[] args = { u.getUserName(), u.getLogonName(),
					u.getLogonPwd(), u.getSex(), u.getBirthday(),
					u.getEducation(), u.getTelephone(), u.getInterest(),
					u.getRemark(), u.getUserID() };
			updateMethod(sql, args);
		}
	}
	public void updateMethod(String sql, Object[] obj) {
		try {
			quer.update(sql, obj);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
 最近下载更多
最近下载更多
                
                xuyongff     LV24
                2020年3月3日
            
            
        
                luo123456789     LV3
                2019年9月3日
            
            
        
                独行侠梦     LV4
                2017年11月22日
            
            
        
                sevenvsky     LV27
                2017年1月12日
            
            
        
                wangkun999     LV10
                2016年7月23日
            
            
        
                prime2015     LV14
                2016年3月8日
            
            
        
                le2010     LV1
                2015年1月29日
            
            
        
                ding1209     LV13
                2014年7月14日
            
            
        
                limtno     LV23
                2014年7月11日
            
            
        
                annoby     LV23
                2014年6月16日
            
            
         最近浏览更多
最近浏览更多
                
                cqbscxh     LV11
                2022年9月19日
            
            
        
                xuyongff     LV24
                2019年11月4日
            
            
        
                luo123456789     LV3
                2019年9月3日
            
            
        
                2544183455    
                2019年8月2日
            
            
                    暂无贡献等级
            
        
                z924931408     LV25
                2019年3月11日
            
            
        
                654321zq     LV2
                2018年10月24日
            
            
        
                lee123321     LV22
                2018年8月22日
            
            
        
                wkc     LV21
                2018年5月14日
            
            
        
                liuxufen     LV8
                2018年4月20日
            
            
        
                cyr921104     LV9
                2018年3月19日
            
            
        
 
                 
                 
                