1、信 息 科 学 与 技 术 学 院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
2、;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
3、 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 Str
4、ing 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.xml3.HibernateTest.javapackage com.itedu.hibernatetest;import org.hibernate.Sessi
5、on;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.先拿到sessionFactoryS
6、essionFactory 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();
7、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 cust
8、Level;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) th
9、is.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 s
10、etCustSource(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;p
11、ublic String toString() return “Customer cid=“ + cid + “, custName=“ + custName+ “, custLevel=“ + custLevel + “, custSource=“ + custSource+ “, custPhone=“ + custPhone + “, custMobile=“ + custMobile+ “;6.customer.hbm.xml7.LinkedPerson.javapackage com.itedu.entity;public class LinkedPerson private Int
12、eger 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() re
13、turn 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_gend
14、er;public String getLkp_phone() return lkp_phone;public void setLkp_phone(String lkp_phone) this.lkp_phone = lkp_phone;8.LinkedPerson.hbm.xml9.Hibernate.cfg.xmlcom.mysql.jdbc.Driverjdbc:mysql:/java0515root123456truetrueorg.hibernate.dialect.MySQLDialectupdate三、 学习心得(问题解决)1) Hibernate 简化了 JDBC 繁琐的编码,
15、 它通过映射来实现 java 对象和数据库的联系。2)关联映射Hibernate 是关系映射工具,必存在 many-to-one,one-to-many,双向一对多,many-to-many 关联。要实现这些操作,首先实体之间要有关联关系,即通过一个对象持有另一个对象的实例。而在数据库的表中,表的主外键也能实现表与表的关联关系。然后我们就要把这些关联关系在映射文件(hbm.xml)中体现出来。many-to-one 是 many 的一端应持有 one 的一端的对象,one-to-many 是 one 的一端应持有 many 端的对象集合,双向一对多就是同时配置了单向的一对多和单向的多对一,多对
16、多关联则是将多对多转换成两个一对多,而且为中间表建立实体类及映射文件,两个端点和中间端分别建立双向一对多关联。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.Str
17、uts2.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“
18、;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 ca
19、tch 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.javapackag
20、e 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.Htt
21、pServletResponse;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“);d
22、ouble 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(
23、);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)