Struts2+Hibernate+Spring框架搭建(二)

二:創建數據庫和搭建Hibernate環境配置
1.首先將Hibernate的Jar包拷貝到“WEB-INF”的lib目錄下面

[img]http://dl.iteye.com/upload/attachment/468327/e28dea60-ffd8-366c-a86f-e511643e706c.png[/img]

2.使用Oracle數據庫創建表Student

[img]http://dl.iteye.com/upload/attachment/468329/77b8c30b-7e5a-3593-a90a-69fdd849f2b8.png[/img]

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章