1 package dao; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.SQLQuery; 8 import org.hibernate.Transaction; 9 10 import com.sun.org.apache.bcel.internal.generic.GETSTATIC; 11 12 import common.BaseDAO; 13 import common.DataZh; 14 15 import entity.Station; 16 17 18 //station和ddxx是一对多关系,为保证代码整洁,每个实体的增删改查写在自己的dao,复杂查询可以写在需要的dao里 19 public class StationDAO extends BaseDAO<Station> { 20 21 22 ////////////////////////////////////////////////////////////////演示hql查询 23 // 使用Hql查询站名里含"京"字的所有站点 24 public List findByproperty(String propertyName) { 25 26 // 通过占位符传参 27 String querystring = "from Station as model where model.czmc like ?"; 28 29 // 显示传参 30 // String 31 // querystring="from Station as model where model.czmc like '%"+propertyName+"%'"; 32 33 Query queryObject = gs().createQuery(querystring); 34 35 queryObject.setParameter(0, "%" + propertyName + "%"); // 传参 36 37 return queryObject.list(); 38 } 39 40 // 查询最后一个字是"京"字并且等级是"一级的"所有站点 41 public List findByKey(String Key1, String Key2) { 42 43 String querystring = "from Station as model where model.czmc like ? and model.dj=?"; 44 45 // 显示传参 46 // String 47 // querystring="from Station as model where model.czmc like '%"+propertyName+"%'"; 48 49 Query queryObject = gs().createQuery(querystring); 50 51 queryObject.setParameter(0, "%" + Key1); 52 queryObject.setParameter(1, Key2); // 传参 53 54 return queryObject.list(); 55 } 56 57 58 // 通过判断参数动态组合Hql语句,生成基本通用查询 59 public List find(Station entity) { 60 List reuslt = null; 61 62 // 字符串辅助类 63 StringBuffer hql = new StringBuffer("from Station where 1=1"); 64 65 List vp = new ArrayList(); 66 67 if (entity != null) { 68 69 // 小于0的和null都不做比较 70 if (entity.getCzdm() != null && entity.getCzdm() < 0) { 71 hql.append(" and czdm=?"); 72 vp.add(entity.getCzdm()); 73 } 74 75 // 空字符串和null都不做比较 76 if (entity.getCzmc() != null && entity.getCzmc().length() > 0) { 77 hql.append(" and czmc = ?"); 78 vp.add(entity.getCzmc()); 79 } 80 81 if (entity.getDj() != null) { 82 hql.append(" and dj=?"); 83 vp.add(entity.getDj()); 84 } 85 } 86 87 Query q = gs().createQuery(hql.toString()); 88 89 for (int i = 0; i < vp.size(); i++) { 90 q.setParameter(i, vp.get(i)); 91 } 92 93 reuslt = q.list(); 94 return reuslt; 95 } 96 97 98 ////////////////////////////////////////////////////////////////演示复杂查询 99 100 // 使用原生sql,根据等级模糊查询符合条件的所有车站名 101 public List<String> findonebydj(String dj) { 102 // sql语句 103 String sql = "select czmc from station where dj like '%" + dj + "%'"; 104 105 SQLQuery q = gs().createSQLQuery(sql); 106 107 return DataZh.ObjtoStr(q.list()); 108 } 109 110 // 使用原生sql,根据等级模糊查询符合条件的所有的车站名和车站代码 111 public List<String[]> findmanybydj(String dj) { 112 // sql语句 113 String sql = "select czmc,czdm from station where dj like '%" + dj 114 + "%'"; 115 116 SQLQuery q = gs().createSQLQuery(sql); 117 118 return DataZh.ObjArrtoStrArr(q.list()); 119 } 120 121 122 // 使用原生sql,关联两表station,ddxx, 查询广州东发出的所有订单id,状态,等级 123 public List<String[]> findmanysbydj(String dj) { 124 // sql语句 125 String sql = "select a.czmc,id,b.status,dj from station a,ddxx b where a.czdm=b.czid and a.czmc='" 126 + dj + "'"; 127 128 SQLQuery q = gs().createSQLQuery(sql); 129 return DataZh.ObjArrtoStrArr(q.list()); 130 } 131 132 ////////////////////////////////////////////////////////////////演示复杂删除 133 134 // 输入车站代码,将相关的车站和订单全部删除 135 public int excuteFzDelete(Integer id) { 136 137 //分别删除 138 String sql = "delete from station where czdm=" + id; 139 String sql2 = "delete from ddxx where czid=" + id; 140 141 // 增删改需要事务,事务开始 142 Transaction tx = null; 143 tx = gs().beginTransaction(); 144 145 SQLQuery q = gs().createSQLQuery(sql); 146 147 int a = q.executeUpdate(); 148 149 SQLQuery q2 = gs().createSQLQuery(sql2); 150 int b = q2.executeUpdate(); 151 152 tx.commit(); // 提交事务 153 154 if (a > 0 || b > 0) //有效删除则返回1 155 return 1; 156 else 157 return 0; //无效删除则返回0 158 159 } 160 161 ////////////////////////////////////////////////////////////////演示复杂更新 162 163 // 将所有无效的(车站已经不存在的)订单全部车站ID(czid)全部更新为指定的车站ID 164 165 //复杂更新,需用inner join 166 //update ddxx a INNER JOIN 167 //(select id from ddxx where czid not in(select DISTINCT czdm from station)) as b 168 //ON a.id=b.id set czid=222 169 public int excuteFzUpdate(Integer czid) 170 { 171 StringBuffer s=new StringBuffer(); 172 s.append("update ddxx a INNER JOIN"); 173 s.append("(select id from ddxx where czid not in(select DISTINCT czdm from station)) as b"); 174 s.append(" ON a.id=b.id set czid="+czid); 175 176 //增删改加事务 177 Transaction tx; 178 tx=gs().beginTransaction(); 179 SQLQuery q=gs().createSQLQuery(s.toString()); 180 181 //受影响的行数 182 int a=q.executeUpdate(); 183 tx.commit(); 184 return a; 185 } 186 187 188 189 190 ////////////////////////////////////////////////////////////////演示多对多 191 192 /* 193 * 站点(Station)与线路(Line)是多对多关系, 194 * 在数据库里面,解决方式是拆成三张表,做一个中间表,中间表要包含两个主表的主键 195 * 多对多拆成了两个一对多 196 * 197 */ 198 199 200 //查询经过指定站点的所有线路名称 201 public List<String> searchLineByStation(String czmc) 202 { 203 String sql = "select xlmc from line where xlid in (select a.LID from stationjoinline a,station b where a.SID = b.czdm and b.czmc=?)"; 204 SQLQuery q=gs().createSQLQuery(sql); 205 206 //建议使用setParameter加参数 207 q.setParameter(0, czmc); 208 return DataZh.ObjtoStr(q.list()); 209 } 210 211 212 213 //查询指定线路经过的所有站点名称,站点ID 214 public List<String[]> searchStationByLine(String xlmc) 215 { 216 String sql="SELECT * from station c WHERE c.czdm IN"+ 217 "(SELECT b.SID FROM stationjoinline b WHERE b.LID IN"+ 218 "(SELECT a.xlid FROM line a WHERE a.xlmc=?))"; 219 SQLQuery q=gs().createSQLQuery(sql); 220 q.setParameter(0, xlmc); 221 List<String[]> list=q.list(); 222 return DataZh.ObjArrtoStrArr(q.list()); 223 } 224 225 226 227 228 229 ////////////////////////////////////////////////////////////dao总结 230 /* 231 * 总结: 232 * 233 * --------------------------------------查询 234 * 1.hql查询 235 * 236 * StringBuffer hql = new StringBuffer("from Station where 1=1"); 237 * 238 * Query q = gs().createQuery(hql.toString()); 239 * 240 * q.list(); 241 * 242 * 2.sql查询 243 * 244 * String sql = "select czmc,czdm from station where dj like '%" + dj 245 + "%'"; 246 247 SQLQuery q = gs().createSQLQuery(sql); //sql查询使用createSQLQuery 248 249 q.list(); 250 * 251 * DataZh.ObjArrtoStrArr(q.list()) //sql查询返回的是Object,记得转换 252 * 253 * 254 * --------------------------------------增删改 255 * 使用原生sql语句执行 256 * 257 * String sql = "delete from station where czdm=" + id; 258 * 259 * Transaction tx = null; 260 tx = gs().beginTransaction(); 261 262 SQLQuery q = gs().createSQLQuery(sql); 263 264 265 * tx.commit(); // 提交事务 266 * 267 * q.executeUpdate() //增删改的语句使用这个函数 268 * 269 * return result //返回受影响的行数 270 * 271 */ 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 }