sql语句查询
作者:互联网
xxxImpl.java 继承 IDao<xxx>
以qqUser为例
public class qqDaoImpl implements IDao<qqUser> { @Override public int insert(qqUser qqUser) throws SQLException { String sql="insert into qqUser values(?,?,?)"; return jdbcUtils.update(sql,qqUser.getId(),qqUser.getPassword(),qqUser.getName()) ; } //在方法体内部根据形参EmpBean中的某些属性是否为空来动态拼接SQL语句,处理不同的查询需求 @Override public ArrayList<qqUser> queryData(qqUser qqUser) throws SQLException { String sql="select * from qqUser where 1=1 "; ArrayList<Object> whereValues=new ArrayList<>(); if(qqUser!=null){ if(qqUser.getId()!=null){ sql+="and id=?"; whereValues.add(qqUser.getId()); } } ArrayList<qqUser> qqUserArrayList=jdbcUtils.queryDate(sql, new rowmapper<day2.bean.qqUser>() { @Override public qqUser rowMap(ResultSet rs) throws SQLException { qqUser qqUser1 = new qqUser(); qqUser1.setId(rs.getString("id")); qqUser1.setName(rs.getString("name")); qqUser1.setPassword(rs.getString("password")); return qqUser1; } },whereValues.toArray()); return qqUserArrayList; } }
测试类
qqDaoImpl qqDao = new qqDaoImpl(); ArrayList<qqUser> qqUserArrayList=null; //插入用户 // qqUser qqUser2 = new qqUser("12780","1232","hello7"); // qqDao.insert(qqUser2); qqUser qqUser = new qqUser(); qqUser.setId("12784"); qqUserArrayList=qqDao.queryData(qqUser); for(qqUser s:qqUserArrayList){ System.out.println(s); }
标签:语句,qqUser,ArrayList,查询,qqUserArrayList,sql,new,qqUser1 来源: https://www.cnblogs.com/lyl890/p/16592728.html