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

二:创建数据库和搭建Hibernate环境配置 
1.首先将Hibernate的Jar包拷贝到“WEB-INF”的lib目录下面 

 

2.使用Oracle数据库创建表Student 

 

3.在src目录下新建包“com.wl.po”,在这个包下面“Student”的类及对应的映射文件“student.hbm.xml” 
Student.java 
Java代码   收藏代码
  1. package com.wl.po;  
  2.   
  3. public class Student {  
  4.   
  5.     private String id;  
  6.       
  7.     private String stuName;  
  8.       
  9.     private String stuNo;  
  10.       
  11.     private String stuSex;  
  12.       
  13.     private int stuAge;  
  14.       
  15.     private String stuGrade;  
  16.   
  17.     public int getStuAge() {  
  18.         return stuAge;  
  19.     }  
  20.   
  21.     public void setStuAge(int stuAge) {  
  22.         this.stuAge = stuAge;  
  23.     }  
  24.   
  25.     public String getStuGrade() {  
  26.         return stuGrade;  
  27.     }  
  28.   
  29.     public void setStuGrade(String stuGrade) {  
  30.         this.stuGrade = stuGrade;  
  31.     }  
  32.   
  33.     public String getStuName() {  
  34.         return stuName;  
  35.     }  
  36.   
  37.     public void setStuName(String stuName) {  
  38.         this.stuName = stuName;  
  39.     }  
  40.   
  41.     public String getStuNo() {  
  42.         return stuNo;  
  43.     }  
  44.   
  45.     public void setStuNo(String stuNo) {  
  46.         this.stuNo = stuNo;  
  47.     }  
  48.   
  49.     public String getStuSex() {  
  50.         return stuSex;  
  51.     }  
  52.   
  53.     public void setStuSex(String stuSex) {  
  54.         this.stuSex = stuSex;  
  55.     }  
  56.   
  57.     public String getId() {  
  58.         return id;  
  59.     }  
  60.   
  61.     public void setId(String id) {  
  62.         this.id = id;  
  63.     }  
  64. }  

student.hbm.xml 
Java代码   收藏代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="com.wl.po">  
  7.     <class name="Student" table="student">  
  8.         <id name="id">  
  9.            <generator class="assigned"></generator>  
  10.         </id>  
  11.         <property name="stuName" column="stuName" type="java.lang.String"></property>  
  12.         <property name="stuNo" column="stuNo" type="java.lang.String"></property>  
  13.         <property name="stuSex" column="stuSex" type="java.lang.String"></property>  
  14.         <property name="stuGrade" column="stuGrade" type="java.lang.String"></property>  
  15.         <property name="stuAge" column="stuAge" type="java.lang.Integer"></property>  
  16.           
  17.     </class>  
  18. </hibernate-mapping>  


4.在src下面新建包“com.wl.dao”,在下面新建接口“studentDao" 
Java代码   收藏代码
  1. package com.wl.dao;  
  2.   
  3. import com.wl.po.Student;  
  4. public interface studentDao {  
  5.   
  6.     public void save(Student stu);  
  7.     public void delete(Student stu);  
  8.     public void update(Student stu);  
  9.       
  10. }  

5.在src下面新建包“com.wl.dao.impl”,在下面新建类“studentDaoImpl" 
Java代码   收藏代码
  1. package com.wl.dao.impl;  
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  3. import com.wl.dao.studentDao;  
  4. import com.wl.po.Student;  
  5.   
  6. public class studentDaoImpl extends HibernateDaoSupport implements studentDao{  
  7.   
  8.     public void save(Student stu){  
  9.         getHibernateTemplate().save(stu);  
  10.     }  
  11.       
  12.     public void delete(Student stu){  
  13.         getHibernateTemplate().delete(stu);  
  14.     }  
  15.       
  16.     public void update(Student stu){  
  17.         getHibernateTemplate().update(stu);  
  18.     }  
  19.   
  20. }  


6.在src下新建包”com.wl.service“,在下面新建接口"studentService" 
Java代码   收藏代码
  1. package com.wl.service;  
  2.   
  3. import com.wl.po.Student;  
  4. public interface studentService {  
  5.   
  6.     public void saveStudent(Student stu);  
  7.     public void updateStudent(Student stu);  
  8.     public void deleteStudent(Student stu);  
  9. }  


7.在src下新建包“com.wl.serviceImpl”,在下面新建类“serviceImpl” 
Java代码   收藏代码
  1. package com.wl.service.impl;  
  2.   
  3. import com.wl.dao.studentDao;  
  4. import com.wl.po.Student;  
  5. import com.wl.service.studentService;  
  6.   
  7. public class studentServiceImpl implements studentService {  
  8.   
  9.     private studentDao stuDao;  
  10.       
  11.     public studentDao getStuDao() {  
  12.         return stuDao;  
  13.     }  
  14.   
  15.     public void setStuDao(studentDao stuDao) {  
  16.         this.stuDao = stuDao;  
  17.     }  
  18.   
  19.     public void deleteStudent(Student stu) {  
  20.         // TODO Auto-generated method stub  
  21.         stuDao.delete(stu);  
  22.     }  
  23.   
  24.     public void saveStudent(Student stu) {  
  25.         // TODO Auto-generated method stub  
  26.         stuDao.save(stu);  
  27.     }  
  28.   
  29.     public void updateStudent(Student stu) {  
  30.         // TODO Auto-generated method stub  
  31.         stuDao.update(stu);  
  32.     }  
  33.   
  34. }  


8.配置Hibernate的映射文件Hibernate.cfg.xml 
Java代码   收藏代码
  1. <?xml version='1.0' encoding='utf-8'?>  
  2.   
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.   
  5.     "-//Hibernate/Hibernate Configuration DTD//EN"  
  6.   
  7.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  8.   
  9. <hibernate-configuration>  
  10.   
  11.     <session-factory>  
  12.   
  13.         <!-- properties -->  
  14.   
  15.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  16.   
  17.         <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>  
  18.   
  19.         <property name="connection.username">WANGLEI</property>  
  20.   
  21.         <property name="connection.password">leiwang</property>  
  22.           
  23.         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>  
  24.   
  25.         <property name="show_sql">true</property>  
  26.   
  27.         <!-- mapping files -->  
  28.   
  29.         <mapping resource="com/wl/po/student.hbm.xml"/>  
  30.   
  31.     </session-factory>  
  32.       
  33. </hibernate-configuration>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值