一、首先先建立一个Web Project
二、然后在程序根目录建立文件夹“DataBase”和“Doc”,分别存放数据库文件和保存SQL语句,建完如下所示:
三、建立数据库“dbHibernate”,保存到文件夹“DataBase”下
四、书写SQL语句,创建“student”表
-- 数据库名称 dbHibernate
-- 版本 SQLServer2008
--学生表
create table student(
ID varchar(10) primary key,
Name varchar(20) not null,
)
--插入数据
insert into student values('101','张三');
insert into student values('102','李四');
insert into student values('103','王五');
五、打开“DB Browser”后,右击选择“New...”,Driver template选择“Microsoft SQL Server”,Driver name为“Hibernate”,Connection URL为“jdbc:sqlserver://localhost:1433; DatabaseName=dbHibernate”,然后就是数据库的User name以及Password;还要添加“sqljdbc4.jar”驱动包,点击“Test Driver”,可以看到测试成功
六、然后点击“Next”,选择“Display the selected schemas”,点击“Add”添加数据库“dbHibernate”,最后“Finish”。
七、右击项目“Hibernate”->“Myeclipse”->“Add Hibernate Capabilities...”,然后默认点击“Next”
八、选择“Browse”,添加自己建立的包名,然后“Next”后,选择“DB Driver”下的“Hibernate”,下面的内容将自动调出。然后“Next”,选择“Java package”
九、建完后,在包“com.langguojie.Hibernate”下添加了“HibernateSessionFactory.java”以及“hibernate.cfg.xml”两个文件
十、在“DB Browser”下,选择“Hibernate”->“Open connection”->Connected to Hibernate->dbo->TABLE->student,右击选择“Hibernate Reverse Engineering”
十一、选择 Java package:“com.langguojie.Hibernate.orm”,然后选择如下图所示内容,点击“Finish”即可。
十二、在包“com.langguojie.Hibernate.orm”下添加了“Student.java”和“Student.hbm.xml”两个文件
十三、在index.jsp中添加如下代码:
<%@page import="com.langguojie.Hibernate.orm.Student"%>
<%@page import="org.hibernate.Query"%>
<%@page import="com.langguojie.Hibernate.HibernateSessionFactory"%>
<%@page import="org.hibernate.Session"%>
<center>
学生管理<br>
<br>
<a href="StudentAdd.html">添加</a><br>
<br>
<table border="1" cellspacing="0" cellpadding="4">
<%
Session ss = HibernateSessionFactory.getSession(); //建立Session对象ss
ss.beginTransaction(); //打开Transaction Query query=ss.createQuery("from Student"); //调用函数Query查询 List<Student>studentList = new ArrayList<Student>(); //将查询的结果放到List表中
studentList = query.list();
for(int i = 0 ;i < studentList.size();i++){
%> <tr>
<td><%=studentList.get(i).getId()%></td> <!-- 输出ID -->
<td><%=studentList.get(i).getName()%></td> <!-- 输出姓名 -->
<td><a href='StudentEdit.jsp?ID=<%=studentList.get(i).getId()%>'>修改</a>
</td>
<td><a href='servlet/StudentDelete.do?ID=<%=studentList.get(i).getId()%>'>删除</a>
</td>
</tr> <%
}
ss.close(); //关闭Transaction
%>
</table>
</center>
十四、建立添加页面“StudentAdd.html”,代码如下:
<form id="form1" name="form1" method="post"action="servlet/StudentAdd.do">
<center>添加<br><br>
学号:<input type="text" id="ID" name="ID"><br><br>
姓名:<input type="text" id="Name" name="Name" ><br><br>
<input type="submit" value="确定"></center>
</form>
十五、建立servlet“StudentAdd.java”,使用函数“doPost()”代码如下:
request.setCharacterEncoding("UTF-8");
String strID = "";
String strName = ""; strID = request.getParameter("ID"); //获取ID
strName = request.getParameter("Name"); //获取Name Session ss = HibernateSessionFactory.getSession();
ss.beginTransaction();
Student s = new Student();
s.setId(strID);
s.setName(strName);
ss.save(s); //保存数据
ss.getTransaction().commit(); //提交数据
ss.close(); response.sendRedirect("../index.jsp");
十六、建立编辑页面“StudentEdit.jsp”,代码如下:
<%@page import="com.langguojie.Hibernate.orm.Student"%>
<%@page import="com.langguojie.Hibernate.HibernateSessionFactory"%>
<%@page import="org.hibernate.Session"%>
<%
request.setCharacterEncoding("UTF-8");
String strID=""; strID = request.getParameter("ID"); Session ss = HibernateSessionFactory.getSession();
ss.beginTransaction();
Student student = (Student)ss.get(Student.class, strID); ss.getTransaction().commit();
ss.close();
%>
<center> <%if(student != null){ %> <br><br>编辑学生信息<br>
<form name="f1" id="f1" action="servlet/StudentEdit.do" method="post">
<table border="0">
<tr>
<td>学号:</td>
<td><input type="text" readonly="readonly" name="ID" id="ID" value="<%=student.getId() %>"> 学号不允许编辑</td>
</tr> <tr>
<td>姓名:</td>
<td><input type="text" name="Name" id="Name" value="<%=student.getName() %>"></td>
</tr> <tr>
<td colspan="2" align="center"><input type="submit" value=" 确定 " ></td>
</tr> </table>
</form>
<%} else { %>
<br><br>编辑学生信息<br><br>
学号为<%=strID %>的学生数据在数据库中不存在!<br><br>
<a href="index.jsp">返回</a>
<%} %>
</center>
十七、建立servlet“StudentEdit.java”,使用函数“doPost()”,代码如下:
request.setCharacterEncoding("utf-8");
String strID = "";
String strName = ""; strID = request.getParameter("ID");
strName = request.getParameter("Name"); Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, strID);
student.setName(strName); session.update(student); session.getTransaction().commit();
session.close(); response.sendRedirect("../index.jsp");
十八、建立servle“StudentDelete.java”,使用函数“doGet()”,代码如下:
request.setCharacterEncoding("UTF-8");
String strID=""; strID = request.getParameter("ID"); Session ss = HibernateSessionFactory.getSession();
ss.beginTransaction(); Student student = (Student)ss.load(Student.class, strID);
ss.delete(student); ss.getTransaction().commit();
ss.close(); response.sendRedirect("../index.jsp");
十九、运行结果截图: