已阅读5页,还剩55页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
信息科学与技术学院Web系统开发课程设计实训报告书 题 目: Web系统开发课程设计 专 业: 信息管理与信息系统 班 级: 姓 名: 学 号: 指导老师: 设计时间:2017年5月15日2017年5月19日第一天一、 学习内容1)软件安装(myEclipse10.0、mysql 5.1+navicat、Tomcat7.0)2)Hibernate 的基本配置和核心文件、关系映射文件回顾3)单表操作和多对多的操作4)Hibernate级联查询 (自连接 左外连接 右外连接)二、 学习代码1.User.javapackage com.itedu.entity;public class User private Integer id;private String username;private String password;private String alias;/描述public User(Integer id, String username, String password, String alias) super();this.id = id;this.username = username;this.password = password;this.alias = alias;public User() super();public Integer getId() return id;public void setId(Integer id) this.id = id;public String getUsername() return username;public void setUsername(String username) this.username = username;public String getPassword() return password;public void setPassword(String password) this.password = password;public String getAlias() return alias;public void setAlias(String alias) this.alias = alias;public String toString() return User id= + id + , username= + username + , password=+ password + , alias= + alias + ;2.User.hbm.xml 3.HibernateTest.javapackage com.itedu.hibernatetest;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.itedu.entity.User;public class HibernateTest public void test()/1.加载ConfigurationConfiguration cf = new Configuration();cf.configure();/2.先拿到sessionFactorySessionFactory sf = cf.buildSessionFactory();/3.seesion CRUD操作Session session = sf.openSession();Transaction tx = session.beginTransaction();/4.开启事务User user = new User();user.setId(1);user.setUsername(张三);user.setPassword(123);user.setAlias(你好!);session.save(user);/5.提交事务mit();session.close();sf.close();4.Hibernate.cfg.xmlcom.mysql.jdbc.Driverjdbc:mysql:/java0515root123456truetrueorg.hibernate.dialect.MySQLDialectupdate5.Customer.javapackage com.itedu.entity;import java.util.HashSet;import java.util.Set;public class Customer private Integer cid;private String custName;private String custLevel;private String custSource;private String custPhone;private String custMobile;private Set linkedset = new HashSet();public Set getLinkedset() return linkedset;public void setLinkedset(Set linkedset) this.linkedset = linkedset;public Integer getCid() return cid;public void setCid(Integer cid) this.cid = cid;public String getCustName() return custName;public void setCustName(String custName) this.custName = custName;public String getCustLevel() return custLevel;public void setCustLevel(String custLevel) this.custLevel = custLevel;public String getCustSource() return custSource;public void setCustSource(String custSource) this.custSource = custSource;public String getCustPhone() return custPhone;public void setCustPhone(String custPhone) this.custPhone = custPhone;public String getCustMobile() return custMobile;public void setCustMobile(String custMobile) this.custMobile = custMobile;public String toString() return Customer cid= + cid + , custName= + custName+ , custLevel= + custLevel + , custSource= + custSource+ , custPhone= + custPhone + , custMobile= + custMobile+ ;6.customer.hbm.xml 7.LinkedPerson.javapackage com.itedu.entity;public class LinkedPerson private Integer lkp_id;/联系人编号(主键)private String lkp_name;/联系人姓名private String lkp_gender;/联系人性别private String lkp_phone;/联系人办公电话/声明多的这个表的实体类对象private Customer customer;public Customer getCustomer() return customer;public void setCustomer(Customer customer) this.customer = customer;public Integer getLkp_id() return lkp_id;public void setLkp_id(Integer lkp_id) this.lkp_id = lkp_id;public String getLkp_name() return lkp_name;public void setLkp_name(String lkp_name) this.lkp_name = lkp_name;public String getLkp_gender() return lkp_gender;public void setLkp_gender(String lkp_gender) this.lkp_gender = lkp_gender; public String getLkp_phone() return lkp_phone; public void setLkp_phone(String lkp_phone) this.lkp_phone = lkp_phone; 8.LinkedPerson.hbm.xml 9.Hibernate.cfg.xmlcom.mysql.jdbc.Driverjdbc:mysql:/java0515root123456truetrueorg.hibernate.dialect.MySQLDialectupdate三、 学习心得(问题解决)1) Hibernate简化了JDBC 繁琐的编码, 它通过映射来实现java对象和数据库的联系。2)关联映射Hibernate是关系映射工具,必存在many-to-one,one-to-many,双向一对多,many-to-many关联。要实现这些操作,首先实体之间要有关联关系,即通过一个对象持有另一个对象的实例。而在数据库的表中,表的主外键也能实现表与表的关联关系。然后我们就要把这些关联关系在映射文件(hbm.xml)中体现出来。many-to-one是many的一端应持有one的一端的对象,one-to-many是one的一端应持有many端的对象集合,双向一对多就是同时配置了单向的一对多和单向的多对一,多对多关联则是将多对多转换成两个一对多,而且为中间表建立实体类及映射文件,两个端点和中间端分别建立双向一对多关联。 3) 学习过程中遇到问题要及时与老师同学沟通,换个角度看问题会简单很多。第二天(一)学习内容1.struts2.0框架1) Struts2.0是什么(Struts2框架在Struts1和Webwork基础之上发展全新的框架)2) Struts2.0从何而来(分层处理思想、AOP面向切面编程、IOC控制反转) 3)Struts2.0的作用servlet实现HelloWorld (Struts2+ hibernate +spring +springMVC) 2.Struts2.0的入门案例 3.使用Servlet实现员工管理系统(使用超链接 实现员工的增加)二、学习代码1.DBUtils.javapackage utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBUtils private static final String URL=jdbc:mysql:/localhost:3306/java0515;private static final String USER = root;private static final String PASSWORD =123456;/封装一个连接数据库的方法public static Connection getConnection()Connection conn =null;try /1.加载驱动Class.forName(com.mysql.jdbc.Driver);/2.通过DriverManager类来得到连接conn = DriverManager.getConnection(URL, USER, PASSWORD); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();return conn;/封装一个静态方法关闭public static void close(Connection conn)if(conn !=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public static void main(String args) System.out.println(getConnection();2.AddEmpServlet.javapackage web;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import utils.DBUtils;public class AddEmpServlet extends HttpServlet Overrideprotected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException / 从页面拿数据request.setCharacterEncoding(utf-8);String name = request.getParameter(name);double salary = Double.parseDouble(request.getParameter(salary);int age = Integer.parseInt(request.getParameter(age);response.setContentType(text/html;charset=utf-8);/ 得到流PrintWriter pw = response.getWriter();Connection conn = null;PreparedStatement prep = null;try conn = DBUtils.getConnection(); prep =conn.prepareStatement(insert into t_emp(empname,empage,empsalary) values(?,?,?);prep.setString(1, name);prep.setInt(2, age);prep.setDouble(3, salary);prep.executeUpdate();/ 执行跟新/ 重定向list页面response.sendRedirect(list); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();pw.println(系统繁忙,请稍后在试!); finally if (prep != null) try prep.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if (conn != null) try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();pw.close();3.ListEmpServlet.javapackage web;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import utils.DBUtils;public class ListEmpServlet extends HttpServlet Overrideprotected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html;charset=utf-8);PrintWriter pw=response.getWriter();Connection conn=null;PreparedStatement prep =null;ResultSet rst =null;try conn=DBUtils.getConnection(); prep=conn.prepareStatement(select * from t_emp); rst = prep.executeQuery();pw.write(); pw.write(ID姓名年龄薪水操作); while(rst.next() int id=rst.getInt(t_id); String name=rst.getString(empname); double salary=rst.getDouble(empsalary); int age=rst.getInt(empage);pw.write(+id+name+salary+age+); pw.write(); pw.write(增加员工); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallyif(rst!=null)try rst.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if(prep!=null)try prep.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if(conn !=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();pw.close();4.addEmp.html addEmp.html !- 增加员工 姓名: 薪水: 年龄: 5. index.jsp base href= My JSP index.jsp starting page !- This is my JSP page. 6. HelloServlet.javapackage web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HelloServlet private static final long serialVersionUID = 1L;public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException/需求:向页面输入一句话 Hello Tocmat/1.设置编码格式 /response.setContentType(utf-8);response.setCharacterEncoding(utf-8);/2.得到流-输出流 PrintWriter pw=response.getWriter();/3.封装一个数据 String str=Hello Tomcat.;/4.写出数据pw.write(str);/5.关闭流pw.close();7. TableServlet.javapackage web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TableServlet extends HttpServletpu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公交安全防线讲解
- 轮流讲党课活动安排
- 甘肃省临夏市临夏中学2026届高一化学第一学期期中综合测试模拟试题含解析
- 2026届宁夏银川六中化学高一第一学期期末复习检测试题含解析
- 2025版康复工程师辅助器具应用技能考核及答案解析
- 2025年厨具制造企业安全生产管理人员安全知识考题及答案解析
- 2025假肢知识试题及答案
- 护士文职面试题目及答案
- 山东省青岛市胶州市2026届化学高二第一学期期中质量跟踪监视试题含解析
- 高血压护士面试题及答案
- 光伏电站安全培训课件
- 2025年消防日消防月主题知识培训
- 2022年长春财经学院公共课《思想道德基础与法律修养》科目期末试卷B
- 生物育种方法
- 建筑企业法人a证考试及答案
- 2025年秋统编版小学语文四年级上册第六单元综合测试卷及参考答案
- 《国有企业管理人员处分条例》测试题(名校卷)附答案详解
- 2022版实验室CNAS认可体系全套质量手册含程序文件、质量记录表
- 民航招飞英语试题及答案
- 继电保护测试仪说明书
- 西方史学史教案
评论
0/150
提交评论