hibernate之增删改查demo

  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 }
时间: 2024-08-27 05:36:33

hibernate之增删改查demo的相关文章

hibernate 入门增删改查demo

原文:hibernate 入门增删改查demo 源代码下载地址:http://www.zuidaima.com/share/1550463648238592.htm

day14:hibernate简单增删改查demo

Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(); session.getTransaction().commit(); session.close(); sf.close(); Config

SSH登录与增删改查demo详解+源代码

点击下载,测试绝对可用SSH整合框架登录加增删改查demo 下载地址:http://download.csdn.net/detail/qq_33599520/9784679   一.框架概述 spring+Struts2+hibernate(SSH)整合框架是一种非常典型与实用的Web项目MVC架构,其中: Spring主要通过控制反转(IOC)与依赖注入(DI)控制对象的创建与维护,与实现面向切片编程(AOP),核心配置文件为:applicationContext.xml. Struts2是M

受老师邀请给学院国创队伍培训php,以一个实战新闻cms增删改查demo为例,写的文档讲义供大家参考

PHP实战基础——以一个新闻cms的增删改查为例 一.        环境配置 二.        数据库创建 三.        增删改查demo 连接数据库 <?php $link=mysql_connect("localhost","root","root"); mysql_select_db("demo",$link); mysql_query("set names utf8"); ?>

hibernate+springMVC增删改查

原文:hibernate+springMVC增删改查 源代码下载地址:http://www.zuidaima.com/share/1550463770610688.htm 1,开发环境:MyEclipse 2013 字符集"UTF-8" 2,Web服务器:Tomcat 6-7都可以 3,SQL Server 2008 需要自己添加数据库db_new,也可以自己改配置文件. 4,使用技术:hibernate,springmvc     官方验证: 程序是没问题的,如楼主描述的功能,他没有

ztree--插件实现增删改查demo(完整版)

ztree--插件实现增删改查demo(完整版) var setting = { async: { enable: true,       //开启异步加载处理 dataFilter: filter  //用于对 Ajax 返回数据进行预处理的函数 }, view: { addHoverDom: addHoverDom, removeHoverDom: removeHoverDom, selectedMulti: false, }, check: { enable: false }, data:

Hibernate 基本增删改查操作

本文将要介绍Hibernate基本的增删改查的操作的实现,首先我们创建一个对象实例.一般情况下会创建User,本例也不例外需要创建这样的对象. [转载使用,请注明出处:http://blog.csdn.net/mahoking] User对象 public class User { private Integer id; private String userName; private String password; /*以下省略getter与setter*/ } 配置User.hbm.xml

Hibernate的增删改查

一.搭建Hibernate开发环境,这里就不说了,直接说环境搭好后的事情. 二.项目的目录结构 三.base_image.hbm.xml配置文件内容 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://w

Hibernate 批量增删改查操作

上文介绍Hibernate基本的增删改查,本例将介绍更实用的批量增删改查的操作的实现.本文中增删改查的操作,仅供参考.如果读者需要应用到实际的应用场景需要的话,需要在此基础上扩展与丰富. [转载使用,请注明出处:http://blog.csdn.net/mahoking] 在学习本例时,需要扩展一下Hibernate中Session的知识.Hibernate中的Session是一级缓存,可以理解为进程级的缓存.在进程运行期间一直存在.Session可以理解为一个可以操作数据库的对象 具体如何操作