二:创建数据库和搭建Hibernate环境配置
1.首先将Hibernate的Jar包拷贝到“WEB-INF”的lib目录下面
2.使用Oracle数据库创建表Student
3.在src目录下新建包“com.wl.po”,在这个包下面“Student”的类及对应的映射文件“student.hbm.xml”
Student.java
student.hbm.xml
4.在src下面新建包“com.wl.dao”,在下面新建接口“studentDao"
5.在src下面新建包“com.wl.dao.impl”,在下面新建类“studentDaoImpl"
6.在src下新建包”com.wl.service“,在下面新建接口"studentService"
7.在src下新建包“com.wl.serviceImpl”,在下面新建类“serviceImpl”
8.配置Hibernate的映射文件Hibernate.cfg.xml
1.首先将Hibernate的Jar包拷贝到“WEB-INF”的lib目录下面
2.使用Oracle数据库创建表Student
3.在src目录下新建包“com.wl.po”,在这个包下面“Student”的类及对应的映射文件“student.hbm.xml”
Student.java
- package com.wl.po;
- public class Student {
- private String id;
- private String stuName;
- private String stuNo;
- private String stuSex;
- private int stuAge;
- private String stuGrade;
- public int getStuAge() {
- return stuAge;
- }
- public void setStuAge(int stuAge) {
- this.stuAge = stuAge;
- }
- public String getStuGrade() {
- return stuGrade;
- }
- public void setStuGrade(String stuGrade) {
- this.stuGrade = stuGrade;
- }
- public String getStuName() {
- return stuName;
- }
- public void setStuName(String stuName) {
- this.stuName = stuName;
- }
- public String getStuNo() {
- return stuNo;
- }
- public void setStuNo(String stuNo) {
- this.stuNo = stuNo;
- }
- public String getStuSex() {
- return stuSex;
- }
- public void setStuSex(String stuSex) {
- this.stuSex = stuSex;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- }
student.hbm.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.wl.po">
- <class name="Student" table="student">
- <id name="id">
- <generator class="assigned"></generator>
- </id>
- <property name="stuName" column="stuName" type="java.lang.String"></property>
- <property name="stuNo" column="stuNo" type="java.lang.String"></property>
- <property name="stuSex" column="stuSex" type="java.lang.String"></property>
- <property name="stuGrade" column="stuGrade" type="java.lang.String"></property>
- <property name="stuAge" column="stuAge" type="java.lang.Integer"></property>
- </class>
- </hibernate-mapping>
4.在src下面新建包“com.wl.dao”,在下面新建接口“studentDao"
- package com.wl.dao;
- import com.wl.po.Student;
- public interface studentDao {
- public void save(Student stu);
- public void delete(Student stu);
- public void update(Student stu);
- }
5.在src下面新建包“com.wl.dao.impl”,在下面新建类“studentDaoImpl"
- package com.wl.dao.impl;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.wl.dao.studentDao;
- import com.wl.po.Student;
- public class studentDaoImpl extends HibernateDaoSupport implements studentDao{
- public void save(Student stu){
- getHibernateTemplate().save(stu);
- }
- public void delete(Student stu){
- getHibernateTemplate().delete(stu);
- }
- public void update(Student stu){
- getHibernateTemplate().update(stu);
- }
- }
6.在src下新建包”com.wl.service“,在下面新建接口"studentService"
- package com.wl.service;
- import com.wl.po.Student;
- public interface studentService {
- public void saveStudent(Student stu);
- public void updateStudent(Student stu);
- public void deleteStudent(Student stu);
- }
7.在src下新建包“com.wl.serviceImpl”,在下面新建类“serviceImpl”
- package com.wl.service.impl;
- import com.wl.dao.studentDao;
- import com.wl.po.Student;
- import com.wl.service.studentService;
- public class studentServiceImpl implements studentService {
- private studentDao stuDao;
- public studentDao getStuDao() {
- return stuDao;
- }
- public void setStuDao(studentDao stuDao) {
- this.stuDao = stuDao;
- }
- public void deleteStudent(Student stu) {
- // TODO Auto-generated method stub
- stuDao.delete(stu);
- }
- public void saveStudent(Student stu) {
- // TODO Auto-generated method stub
- stuDao.save(stu);
- }
- public void updateStudent(Student stu) {
- // TODO Auto-generated method stub
- stuDao.update(stu);
- }
- }
8.配置Hibernate的映射文件Hibernate.cfg.xml
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- properties -->
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
- <property name="connection.username">WANGLEI</property>
- <property name="connection.password">leiwang</property>
- <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
- <property name="show_sql">true</property>
- <!-- mapping files -->
- <mapping resource="com/wl/po/student.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>