一、当保存的是Hibernate 对象的时候:
/** * @功能描述 保存或者修改收文记录 * @param main void */ @SuppressWarnings("unchecked") public void saveOrUpdateRecive( TbDocumentReceiveMain main) { this.saveOrUpdate(main); this.flush(); }
TbDocumentMain 类的一个对象,表示数据库中的一条记录,每次可以给类的对象赋值之后在调用</span><pre name="code" class="html">saveOrUpdate保存或者修改之后的对象
二、删除
(1)sql删除
/** * @描述:删除TB_DOCUMENT_RECEIVE_DEPT记录 * @param receiveDeptId * void * @创建人 :kj */ @SuppressWarnings("unchecked") public void deleteDemp(String receiveDeptId) { StringBuffer sql = new StringBuffer(); sql.append("delete TB_DOCUMENT_RECEIVE_DEPT where RECIVE_MAIN_ID = ? "); SQLQuery query = this.createSQLQuery(sql.toString(), null, null, new Object[]{receiveDeptId}); query.executeUpdate(); }
三、查询
(1)、当查询的记录对应的是一张表里面的数据,并且该表对应一个类的对象
例;
/** * * @功能描述 根据收文ID和类型查询附件 * @param id * @return List<TbDocumentFile> */ @SuppressWarnings("unchecked") public List<TbDocumentFile> queryFileByDocumentid( String id,String type) { List<TbDocumentFile> list = this.createQuery("from TbDocumentFile where documentId = ? and type = ?", new Object[]{id,type}).list(); return list; }解释: TbDocumentFile对应数据库里面的表TB_DOCUMENT_FILE表如下图所示
List<TbDocumentFile> list = this.createQuery("from TbDocumentFile where documentId = ? and type = ?", new Object[]{id,type}).list();
这句将要查询的参数放在object数组里面,需要注意的是这里的查询语句是:"from TbDocumentFile where documentId = ? and type = ?", new Object[]{id,type})我们可以看出:
1、from前面没有select 关键字,并且from后面跟的不是表名,<span style="font-family: Arial, Helvetica, sans-serif;">而是数据表对应的类的类名</span>
2、where 后面跟的不是<span style="background-color: rgb(240, 240, 240);">TB_DOCUMENT_FILE中的字段DOCUMENT_ID ,而是</span></span><span style="font-family: Arial, Helvetica, sans-serif;">DOCUMENT_ID 字段对类属性:</span><span style="font-family: Arial, Helvetica, sans-serif;">documentId </span>查询出来的记录存放在 List<TbDocumentFile> list 这个list数组里面,数组里面的每一条数据元素就是一个TbDocumentFile对象即一条数据表中的一条记录
下面给大家看看查询的到的list数据:
由这张表和上面一张数据库存储的表可以看出他们的一一对应关系。
(2)、当查询的数据不是来自一张表而是多表联查
例:
/** * @描述:根据员工ID获取岗位类别 * @param empid 员工id * @return 00 正所长, 01副所长 ,02 科室主任 ,03 科员 * String * @创建人 :kj */ @SuppressWarnings("unchecked") public String queryPostType(String empid) { String postType = "03" ; String sql ="select e.post_type_code as code ,d.name from cen_reg.t_employee e " + "left join cendic.d_dictionary_item d on d.code = e.post_type_code and d.d_code='DIC_PT_GWLB' " + "where e.id = '"+empid+"'"; List<Map<String,String>> list = this.createSQLQuery(sql, null, null, null) .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP) .list(); if(list != null && list.size() > 0){ String temp = list.get(0).get("CODE"); if(StringUtils.equals(temp, "01")){ //还需要判断正副 所长 sql = " select id from TB_DOCUMENT_MANAGER t where t.emp_id = ? " ; list = this.createSQLQuery(sql, null, null, new Object[]{empid}).list(); if(list != null && list.size() > 0){ //正所长 postType = "00" ; }else{ //副所长 postType = temp ; } } if(StringUtils.equals(temp, "02")){ postType = temp ; } } return postType ; }
由这句sql语句可以知道:查询的数据code 来自表cen_reg.t_employee表,查询的name来自表cendic.d_dictionary_item
String sql ="select e.post_type_code as code ,d.name from cen_reg.t_employee e " + "left join cendic.d_dictionary_item d on d.code = e.post_type_code and d.d_code='DIC_PT_GWLB' " + "where e.id = '"+empid+"'";这种情况调用函数 createSQLQuery来获取返回值
List<Map<String,String>> list = this.createSQLQuery(sql, null, null, null) .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP) .list();
返回的list在Debug下存储结构为: