javaweb实例.docx
- 文档编号:6649232
- 上传时间:2023-01-08
- 格式:DOCX
- 页数:46
- 大小:302.48KB
javaweb实例.docx
《javaweb实例.docx》由会员分享,可在线阅读,更多相关《javaweb实例.docx(46页珍藏版)》请在冰豆网上搜索。
javaweb实例
【项目一】
1.项目名称:
双向多对一的信息添加与查询
2.主要代码:
《1》publicclassFactory{
privateIntegerfactoryId;//生产商的id
privateStringfactoryName;//生产商名称
privateSet
publicIntegergetFactoryId(){
returnfactoryId;
}
publicvoidsetFactoryId(IntegerfactoryId){
this.factoryId=factoryId;
}
publicStringgetFactoryName(){
returnfactoryName;
}
publicvoidsetFactoryName(StringfactoryName){
this.factoryName=factoryName;
}
publicSet
returnproducts;
}
publicvoidsetProducts(Set
this.products=products;
}
《2publicclassHibernateInitialize{
privatestaticfinalThreadLocal
privatestaticSessionFactorysessionFactory=null;//SessionFactory对象
//静态块
static{
try{
//加载Hibernate配置文件
Configurationcfg=newConfiguration().configure();
sessionFactory=cfg.buildSessionFactory();
}catch(Exceptione){
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
publicstaticSessiongetSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
if(session==null||!
session.isOpen()){
if(sessionFactory==null){
rebuildSessionFactory();
}
session=(sessionFactory!
=null)?
sessionFactory.openSession():
null;
threadLocal.set(session);
}
returnsession;
}
publicstaticvoidrebuildSessionFactory(){
try{
//加载Hibernate配置文件
Configurationcfg=newConfiguration().configure();
sessionFactory=cfg.buildSessionFactory();
}catch(Exceptione){
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
publicstaticSessionFactorygetSessionFactory(){
returnsessionFactory;
}
publicstaticvoidcloseSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
threadLocal.set(null);//
if(session!
=null){
session.close();//关闭Session
}
}
}
《3》publicclassProduct{
privateIntegerid;//唯一性标识
privateStringname;//产品名称
privateDoubleprice;//产品价格
privateFactoryfactory;//关联的生产商
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicDoublegetPrice(){
returnprice;
}
publicvoidsetPrice(Doubleprice){
this.price=price;
}
publicFactorygetFactory(){
returnfactory;
}
publicvoidsetFactory(Factoryfactory){
this.factory=factory;
}
}
《4》publicclassAddProductextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
Stringname=request.getParameter("name");//产品名称
Doubleprice=newDouble(request.getParameter("price"));//产品价格
IntegerfactoryID=newInteger(request.getParameter("factory"));//生产商的ID
Sessionsession=null;//声明一个Session对象
try{
//Hibernate的持久化操作
session=HibernateInitialize.getSession();//获取Session
session.beginTransaction();//事务开启
Factoryfactory=newFactory();//实例化对象
factory=(Factory)session.get(Factory.class,factoryID);//获取生产商的实体对象
Productproduct=newProduct();//实例化对象
product.setName(name);//设置产品名称
product.setPrice(price);//设置产品价格
product.setFactory(factory);
session.save(product);//保存产品信息
session.getTransaction().commit();//事务提交
}catch(Exceptione){
e.printStackTrace();
session.getTransaction().rollback();//事务回滚
}finally{
HibernateInitialize.closeSession();//关闭Session
}
//跳转到查找产品信息列表的Servlet
RequestDispatcherrd=this.getServletContext().getRequestDispatcher("/QueryProduct");
rd.forward(request,response);
}
}
《5》publicclassQueryFactoryextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
Sessionsession=null;//声明一个Session对象
Listlist=newArrayList();//List集合
try{
//Hibernate的持久化操作
session=HibernateInitialize.getSession();//获取Session
Stringhql="fromFactory";//定义HQL语句
session.beginTransaction();//事务开启
list=session.createQuery(hql).list();
session.getTransaction().commit();//事务提交
}catch(Exceptione){
e.printStackTrace();
session.getTransaction().rollback();//事务回滚
}finally{
HibernateInitialize.closeSession();//关闭Session
}
request.setAttribute("list",list);//保存查询返回的集合
//跳转到产品添加页面
RequestDispatcherrd=this.getServletContext().getRequestDispatcher("/AddProduct.jsp");
rd.forward(request,response);
}
}
《6》publicclassQueryProductextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
Sessionsession=null;//声明一个Session对象
List
try{
//Hibernate的持久化操作
session=HibernateInitialize.getSession();//获取Session
//联合查询的HQL语句
Stringhql="selectp.id,p.name,p.price,f.factoryNamefromProductpinnerjoinp.factoryf";
session.beginTransaction();//事务开启
list=session.createQuery(hql).list();
session.getTransaction().commit();//事务提交
}catch(Exceptione){
e.printStackTrace();
session.getTransaction().rollback();//事务回滚
}finally{
HibernateInitialize.closeSession();//关闭Session
}
request.setAttribute("list",list);//保存查询返回的集合
//跳转到产品列表页面
RequestDispatcherrd=this.getServletContext().getRequestDispatcher("/ProductList.jsp");
rd.forward(request,response);
}
}
《7》
产品名称 | |
产品价格 | |
生成商 | forEachitems="${list}"var="list"> forEach> |
《8》
${list[1]} | ${list[3]} |
3.结果截图:
【项目二】
1.项目名称:
员工信息的模糊查询
2.主要代码:
《1》publicclassEmployee{
privateIntegerid;//id值
privateStringname;//员工姓名
privateStringsex;//性别
privateStringbusiness;//职务
privateStringaddress;//住址
privateStringremark;//备注
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
this.sex=sex;
}
publicStringgetBusiness(){
returnbusiness;
}
publicvoidsetBusiness(Stringbusiness){
this.business=business;
}
publicStringgetAddress(){
returnaddress;
}
publicvoidsetAddress(Stringaddress){
this.address=address;
}
publicStringgetRemark(){
returnremark;
}
publicvoidsetRemark(Stringremark){
this.remark=remark;
}
}
《2》publicclassHibernateUtil{
privatestaticfinalThreadLocal
privatestaticSessionFactorysessionFactory=null;//SessionFactory对象
//静态块
static{
try{
//加载Hibernate配置文件
Configurationcfg=newConfiguration().configure();
sessionFactory=cfg.buildSessionFactory();
}catch(Exceptione){
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
publicstaticSessiongetSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
if(session==null||!
session.isOpen()){
if(sessionFactory==null){
rebuildSessionFactory();
}
session=(sessionFactory!
=null)?
sessionFactory.openSession():
null;
threadLocal.set(session);
}
returnsession;
}
publicstaticvoidrebuildSessionFactory(){
try{
//加载Hibernate配置文件
Configurationcfg=newConfiguration().configure();
sessionFactory=cfg.buildSessionFactory();
}catch(Exceptione){
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
publicstaticSessionFactorygetSessionFactory(){
returnsessionFactory;
}
publicstaticvoidcloseSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
threadLocal.set(null);
if(session!
=null){
session.close();//关闭Session
}
}
}
《3》publicclassQueryEmployeeextendsHttpServlet{
protectedvoiddoG
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- javaweb 实例