一个关于spring+hibernate的例子

开发工具以及环境:windows xp,tomcat 5.5.17,Eclipse 3.2,myEclipse 5.0M2,mysql 5.17 
 
1 、新建一个web project;
2、 添加spring capabinities,钩选所需的包.接下来添加hibernate包.注意添加的顺序
很重要 ;
3 、以下是代码:
admin.java
 
package com.yxy.entity;
/**
 * Admin generated by MyEclipse - Hibernate Tools
 */
public class Admin implements java.io.Serializable {
 private static final long serialVersionUID = 1L;
 private long id;
 private String uname;
 private String upsw;
 private String ulevel;
 // Constructors
 /** default constructor */
 public Admin() {
 }
 /** full constructor */
 public Admin(String uname, String upsw, String ulevel) {
  this.uname = uname;
  this.upsw = upsw;
  this.ulevel = ulevel;
 }
 // Property accessors
 public long getId() {
  return this.id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getUname() {
  return this.uname;
 }
 public void setUname(String uname) {
  this.uname = uname;
 }
 public String getUpsw() {
  return this.upsw;
 }
 public void setUpsw(String upsw) {
  this.upsw = upsw;
 }
 public String getUlevel() {
  return this.ulevel;
 }
 public void setUlevel(String ulevel) {
  this.ulevel = ulevel;
 }
}
Admin.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">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.yxy.entity.Admin" table="admin">
        <id name="id" type="long">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="uname" type="string">
            <column name="uname" length="20" />
        </property>
        <property name="upsw" type="string">
            <column name="upsw" length="20" />
        </property>
        <property name="ulevel" type="string">
            <column name="ulevel" length="1" />
        </property>
    </class>
</hibernate-mapping>
 
AdminImpl.java
 
package com.yxy.impl;
import com.yxy.entity.Admin;
public interface AdminImpl {
 void insertUser(Admin admin);
 boolean isLogin(String uname,String upwd);
}
 
AdminDao
 
package com.yxy.springdao;
import java.util.Iterator;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yxy.entity.Admin;
import com.yxy.impl.AdminImpl;
public class AdminDao extends HibernateDaoSupport implements AdminImpl {
 public void insertUser(Admin admin) {
  // TODO Auto-generated method stub
  super.getHibernateTemplate().save(admin);
 }
 
 public boolean isLogin(String uname,String upwd){
  List list=getHibernateTemplate().find("from Admin as a where a.uname='"+uname+"' and a.upsw='"+upwd+"'");
  Iterator it=list.iterator();
  if(!it.hasNext()){
   System.out.println("登陆失败");
   return false;
  }
  else{
   System.out.println("登陆成功");
     return true;
  }
 }
}
 
单元测试
UnitTest.java
 
package com.yxy.unit;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.yxy.entity.Admin;
import com.yxy.impl.AdminImpl;
import junit.framework.TestCase;
public class UnitTest extends TestCase{
 public void testcase()throws Exception{
  Resource resource=new ClassPathResource("applicationContext.xml");
  BeanFactory factory=new XmlBeanFactory(resource);
  AdminImpl al=(AdminImpl)factory.getBean("adminDao");
  Admin admin=new Admin();
  admin.setUname("admin");
  admin.setUpsw("admin");
  admin.setUlevel("0");
  al.insertUser(admin);
  al.isLogin("admin", "admin");
 }
}
 
配置文件
ApplicationContext.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
   <value>jdbc:mysql://localhost:3306/infoweb</value>
  </property>
  <property name="username">
   <value>root</value>
  </property>
 </bean>
 
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">
        true
    </prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
   </props>
  </property>
  <property name="mappingResources">
   <value>com/yxy/entity/Admin.hbm.xml</value>
  </property>
 </bean>
 
 <bean id="adminDao" class="com.yxy.springdao.AdminDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 
</beans>
 
最后注意添加log4j.property

(来源:急速飞鹰的BLOG)
 

你可能感兴趣的:(一个关于spring+hibernate的例子)