




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料目 录1.1在容器外实现hibernate的包含关系及应用实例(mysql第1部分)21.1.1构建项目在相关的各个pojo类21.1.2映射相关的数据库表91.1.3编程项目中相关的dao类111.1 在容器外实现hibernate的包含关系及应用实例(mysql第1部分)1.1.1 构建项目在相关的各个pojo类1、了解component(组合)映射技术的基本要求在hibernate的世界中,术语fine-grained object model,大致意思就是适当的细粒度对象模型所谓的细粒度对象模型是指在业务模型的对象上加以细分,得到
2、更多的业务模型对象,说白了就是就把一个对象分成几个对象对hibernate而言,主要是针对实体类设计的对象细分,对象的细分主要出于两种目的(1)面向设计的粒度细分:通过对象的细化,实现更加清晰的系统逻辑(2)性能的粒度细分:针对业务逻辑,通过合理的细粒度对象,提高系统的能耗比(性能/资源消耗)如果包含目标类的一个对象,可以应用component(组合)映射技术。2、添加一个emailpo类(1)emailpo类、com.px1987.sshwebcrm.dao.pobjectprivate string mailname; /它代表邮件帐号 private string host; /它代表邮
3、件服务器的主机名package com.px1987.sshwebcrm.dao.pobject;public class emailpoprivate string mailname; /它代表邮件帐号 private string host; /它代表邮件服务器的主机名public string gethost() return host;public void sethost(string host) this.host = host;public string getmailname() return mailname;public void setmailname(string ma
4、ilname) this.mailname = mailname;overridepublic int hashcode() final int prime = 31;int result = 1;result = prime * result + (host = null) ? 0 : host.hashcode();result = prime * result+ (mailname = null) ? 0 : mailname.hashcode();return result;overridepublic boolean equals(object obj) if (this = obj
5、)return true;if (obj = null)return false;if (getclass() != obj.getclass()return false;final emailpo other = (emailpo) obj;if (host = null) if (other.host != null)return false; else if (!host.equals(other.host)return false;if (mailname = null) if (other.mailname != null)return false; else if (!mailna
6、me.equals(other.mailname)return false;return true;3、在项目中再添加一个userinfopo类(1)userinfomailpo类、com.px1987.sshwebcrm.dao.pobjectprivate string id; private string username; private char usersex; private int userage; private emailpo email;package com.px1987.sshwebcrm.dao.pobject;public class userinfomailpo
7、 /更好地体现 oo中封装private string id; overridepublic int hashcode() final int prime = 31;int result = 1;result = prime * result + (email = null) ? 0 : email.hashcode();result = prime * result + (id = null) ? 0 : id.hashcode();result = prime * result + userage;result = prime * result+ (username = null) ? 0
8、 : username.hashcode();result = prime * result + usersex;return result;overridepublic boolean equals(object obj) if (this = obj)return true;if (obj = null)return false;if (getclass() != obj.getclass()return false;userinfopo other = (userinfopo) obj;if (email = null) if (other.email != null)return fa
9、lse; else if (!email.equals(other.email)return false;if (id = null) if (other.id != null)return false; else if (!id.equals(other.id)return false;if (userage != other.userage)return false;if (username = null) if (other.username != null)return false; else if (!username.equals(other.username)return fal
10、se;if (usersex != other.usersex)return false;return true;public string getid() return id;public void setid(string id) this.id = id;public string getusername() return username;public void setusername(string username) this.username = username;public char getusersex() return usersex;public void setuser
11、sex(char usersex) this.usersex = usersex;public int getuserage() return userage;public void setuserage(int userage) this.userage = userage;public emailpo getemail() return email;public void setemail(emailpo email) this.email = email;private string username; private char usersex; private int userage;
12、 private emailpo email;public userinfomailpo()1.1.2 映射相关的数据库表1、设计数据库表_将紧密相关的数据放在同一个表(粗粒度的设计提高数据访问性能)userinfotable数据库表的结构如下:2、设计我们的userinfomailpo.hbm.xml映射文件 3、在hibernate.cfg.xml中增加对userinfomailpo.hbm.xml映射文件的关联1.1.3 编程项目中相关的dao类1、在项目中添加一个新的接口containhibernatedaointerface接口,包名称为com.px1987.sshwebcrm.da
13、erpackage er;import org.hibernate.hibernateexception;public interface containhibernatedaointerface public boolean doinsertuserinfodatatodb()throws hibernateexception,java.io.unsupportedencodingexception; public boolean doselectuserinfodatafromdb(string selecthql) thr
14、ows hibernateexception;2、添加containhibernatedaointerface接口的实现类containhibernatedaoimple、包名称为com.px1987.sshwebcrm.dao.imple编程该实现类package com.px1987.sshwebcrm.dao.imple;import java.util.list;import java.util.listiterator;import mons.logging.log;import mons.logging.logfactory;
15、import org.hibernate.hibernateexception;import org.hibernate.session;import org.hibernate.transaction;import er.containhibernatedaointerface;import com.px1987.sshwebcrm.dao.pobject.emailpo;import com.px1987.sshwebcrm.dao.pobject.userinfopo;import com.px1987.sshwebcrm.dao.
16、util.hibernateutil;public class containhibernatedaoimple implements containhibernatedaointerface private static log log = logfactory.getlog(hibernatedaobean.class);public containhibernatedaoimple() public boolean doinsertuserinfodatatodb() throws hibernateexception,java.io.unsupportedencodingexcepti
17、onsession session=null;transaction tx=null;trysession = hibernateutil.currentsession();tx= session.begintransaction();userinfomailpo oneuser = new userinfomailpo ();/*对某些数据库系统如mssqlserver2000必须进行字符的编码转换,否则会出现中文乱码oneuser.setusername(new string(张三.getbytes(gb2312),iso8859-1); */oneuser.setusername(张三)
18、; oneuser.setusersex(m); oneuser.setuserage(30);emailpo oneemail = new emailpo(); oneemail.sethost();oneemail.setmailname(trainict);oneuser.setemail(oneemail); /通过它来体现组合关系session.save(oneuser); /保存该实体mit();catch(hibernateexception he)if ( tx!=null )tx.rollback();log.error(在doinsertuser
19、infodatatodb方法中出现了hibernateexception错误, he); throw he;/* catch(java.io.unsupportedencodingexception ex) log.error(在doinsertuserinfodatatodb方法中出现了unsupportedencodingexception错误, ex); throw ex; */finallyhibernateutil.closesession();return true; /下面的代码片段則说明如何查询数据。public boolean doselectuserinfodatafromdb(string selecthql) throws hibernateexceptionsession session=null;transaction tx=null;trysession = hibernateutil.currentsessi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度建筑工程沙土供应合同4篇
- 环境保护与治理工程咨询服务合同
- 智能农业技术和设备采购合同
- 2025年度桩基础施工技术咨询服务合同范本
- 中国云办公行业市场全景评估及发展战略规划报告
- 商业计划书和可行性报告 中央空调某市市场可行性分析报告(一)
- 中国一次性注射器行业市场调研分析及投资战略咨询报告
- 中国舞蹈鞋市场运营态势分析及投资前景预测报告
- 污水处理可行性研究报告
- 中国云灾备行业投资分析及发展战略研究咨询报告
- 2025年茂名市高三年级第一次综合测试(一模)物理试卷(含答案)
- 2025年重症医学科(ICU)护理工作计划
- 四川省名校2025届高三第二次模拟考试英语试卷含解析
- 《电子商务法律法规》电子商务专业全套教学课件
- 《产后出血预防与处理指南(2023)》解读课件
- 全套教学课件《工程伦理学》
- 江苏省建筑与装饰工程计价定额(2014)电子表格版
- 清华大学考生自述
- 幼儿园中班绘本:《我喜欢我的小毯子》
- 小学二年级体育课《团身滚动》教案
- 级本科诊断学绪论+问诊课件
评论
0/150
提交评论