最简单的spring+hibernate

最近一直忙着考试,不过,还是看了看,spring呵呵,很好。就吧我的代码粘出来,与大家分享。

//applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//PPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
  <property name="url" value="jdbc:jtds:sqlserver://localhost:1433/test"></property>
  <property name="username" value="sa"></property>
  <property name="password" value=""></property>

 </bean>
 
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
 <property name="dataSource">
  <ref bean="dataSource"/>
 </property>
 
 <property name="mappingResources">
  <list>
  <value>cn/anyf/skycliff/Customer.hbm.xml</value>
  </list>
 </property>
 
 
 <property name="hibernateProperties">
  <props>
   <prop key="hibernate.dialect">
   org.hibernate.dialect.SQLServerDialect
   </prop>
   </props>
   </property>
 </bean>
 
 
 <bean id="customerDAO" class="cn.anyf.skycliff.CustomerDAO">
 <property name="sessionfactory">
  <ref bean="sessionFactory"/>
 </property>
 </bean>
</beans>

 

//ICustomerDAO.java 定义接口

package cn.anyf.skycliff;

public interface ICustomerDAO {
public void save(Customer customer);
}

 

//CustomerDAO.java

package cn.anyf.skycliff;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class CustomerDAO implements ICustomerDAO {
 private HibernateTemplate hibernateTemplate;
 private SessionFactory sessionfactory;
 public CustomerDAO(){}
 public CustomerDAO(SessionFactory sessionFactory){
  hibernateTemplate=new HibernateTemplate(sessionFactory);
 }
 
 
 public void save(Customer customer) {
 hibernateTemplate.save(customer);


 }
 public void setSessionfactory(SessionFactory sessionFactory) {
  hibernateTemplate=new HibernateTemplate(sessionFactory);
 }

}

 

 

//Customer.java

package cn.anyf.skycliff;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class Customer implements Serializable {

    /** identifier field */
    private String id;

    /** nullable persistent field */
    private String password;

    /** full constructor */
    public Customer(String id, String password) {
        this.id = id;
        this.password = password;
    }

    /** default constructor */
    public Customer() {
    }

    /** minimal constructor */
    public Customer(String id) {
        this.id = id;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("id", getId())
            .toString();
    }

}

//Customer.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>
<!--
    Created by the Middlegen Hibernate plugin 2.2

    http://boss.bekk.no/boss/middlegen/
    http://www.hibernate.org/
-->

<class
    name="cn.anyf.skycliff.Customer"
    table="customer"
    lazy="false"
>

    <id
        name="id"
        type="java.lang.String"
        column="id"
    >
   
        <generator class="assigned" />
    </id>

    <property
        name="password"
        type="java.lang.String"
        column="password"
        length="50"
    />

    <!-- Associations -->
 

</class>
</hibernate-mapping>

 

//test .java测试

package cn.anyf.skycliff;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
ApplicationContext context=new ClassPathXmlApplicationContext("conf/applicationContext.xml");
 ICustomerDAO dao=(ICustomerDAO)context.getBean("customerDAO");
 Customer customer=new Customer();
 customer.setId("大马");
 customer.setPassword("1234214124");
 dao.save(customer);
 System.out.println("id:" +customer.getId());
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值