`
xiangzhengyan
  • 浏览: 124823 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring+hibernate-annotations

阅读更多
注意 实体bean 注释是 @javax.persistence.Entity 而不是@org.hibernate.annotations.Entity
spring配置 xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3.   
  4. <beans>  
  5.   
  6.     <bean id="dataSource"  
  7.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  8.         <property name="driverClassName">  
  9.             <value>com.mysql.jdbc.Driver</value>  
  10.         </property>  
  11.         <property name="url">  
  12.             <value>jdbc:mysql://localhost/test</value>  
  13.         </property>  
  14.         <property name="username">  
  15.             <value>root</value>  
  16.         </property>  
  17.         <property name="password">  
  18.             <value>123456</value>  
  19.         </property>  
  20.     </bean>  
  21.   
  22.     <bean id="sessionFactory"  
  23.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  24.         <property name="configurationClass">  
  25.             <value>org.hibernate.cfg.AnnotationConfiguration</value>  
  26.         </property>  
  27.         <property name="dataSource">  
  28.             <ref bean="dataSource" />  
  29.         </property>  
  30.         <property name="configLocation">  
  31.             <value>/hibernate.cfg.xml</value>  
  32.         </property>  
  33.   
  34.     </bean>  
  35.   
  36.     <bean id="userDAO" class="test.dao.UserDAOImpl">  
  37.         <property name="sessionFactory">  
  38.             <ref bean="sessionFactory" />  
  39.         </property>  
  40.     </bean>  
  41.   
  42.   
  43. </beans>  

hibernate.cfg.xml 代码

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC   
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.         <property name="show_sql">true</property>  
  9.         <mapping class="test.model.User" />  
  10.     </session-factory>  
  11. </hibernate-configuration>  

 

java 代码 DAO
  1. package test.dao;   
  2.   
  3. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  4.   
  5. import test.model.User;   
  6.   
  7. public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {   
  8.   
  9.     public void save(User user) {   
  10.         getHibernateTemplate().save(user);   
  11.     }   
  12.   
  13. }  

java 代码
  1. package test.model;   
  2.   
  3. import javax.persistence.Entity;   
  4. import javax.persistence.GeneratedValue;   
  5. import javax.persistence.GenerationType;   
  6. import javax.persistence.Id;   
  7.   
  8.   
  9. @Entity  
  10. public class User {   
  11.     @Id  
  12.     @GeneratedValue(strategy = GenerationType.AUTO)   
  13.     private int id;   
  14.   
  15.     private String name;   
  16.   
  17.     public int getId() {   
  18.         return id;   
  19.     }   
  20.   
  21.     public void setId(int id) {   
  22.         this.id = id;   
  23.     }   
  24.   
  25.     public String getName() {   
  26.         return name;   
  27.     }   
  28.   
  29.     public void setName(String name) {   
  30.         this.name = name;   
  31.     }   
  32.   
  33. }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics