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

Spring + Hibernate Sample

阅读更多

类库:在附件中。

EventDao是业务接口,EventDaoImpl是其实现类。

java 代码
  1. public interface EventDao {   
  2.     Event testEvent();   
  3. }   
  4.   
  5. public class EventDaoImpl extends HibernateDaoSupport implements EventDao {   
  6.   
  7.     public Event testEvent() {   
  8.         Event e = (Event) getHibernateTemplate().get(Event.class, 1l);   
  9.         return e;   
  10.     }   
  11. }  
java 代码
  1. public class AppContext {   
  2.   
  3.     /**  
  4.      * @param args  
  5.      */  
  6.     public static void main(String[] args) {   
  7.         // TODO Auto-generated method stub   
  8.   
  9.         AppContext ac = new AppContext();   
  10.         ac.initContext();   
  11.     }   
  12.   
  13.     public void initContext() {   
  14.         // Resource resource = new FileSystemResource("beans.xml");   
  15.         // BeanFactory factory = new XmlBeanFactory(resource);   
  16.   
  17.         // ClassPathResource resource = new ClassPathResource("beans.xml");   
  18.         // BeanFactory factory = new XmlBeanFactory(resource);   
  19.         ApplicationContext context = new ClassPathXmlApplicationContext(   
  20.                 new String[] { "application-context.xml" });   
  21.         // of course, an ApplicationContext is just a BeanFactory   
  22.         // BeanFactory factory = (BeanFactory) context;   
  23.   
  24.         EventDao eventDao = (EventDao) context.getBean("myEventDao");   
  25.   
  26.         Event e = eventDao.testEvent();   
  27.   
  28.         System.out.println(e.getTitle() + ", " + e.getDate().toLocaleString());   
  29.     }   
  30. }  

hibernate-entities.xml

xml 代码
  1. <!DOCTYPE hibernate-configuration PUBLIC       
  2.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"       
  3.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">      
  4. <hibernate-configuration>      
  5.     <session-factory>  
  6.         <mapping class="com.james.business.modle.domain.Event" />  
  7.     </session-factory>  
  8. </hibernate-configuration>  

application-context.xml

xml 代码
  1. <!----><xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  6.   
  7.     <bean id="myDataSource"      
  8.         class="org.apache.commons.dbcp.BasicDataSource"      
  9.         destroy-method="close">      
  10.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />   
  11.         <!-- ?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true --><!---->
  12.         <property name="url"      
  13.             value="jdbc:mysql://10.1.1.2:3306/test" />      
  14.         <property name="username" value="test" />      
  15.         <property name="password" value="test" />      
  16.     </bean>      
  17.       
  18.     <bean id="mySessionFactory"      
  19.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">      
  20.         <property name="dataSource" ref="myDataSource" />      
  21.       
  22.     <!---->    <!-- mappingResources -->  
  23.         <!-- com/james/business/modle/domain/Event.hbm.xml -->  
  24.         <!-- mappingLocations classpath:org/jbpm/**/*.hbm.xml -->
  25.     <!---->  
  26. <!---->
  27.         <property name="configLocations">      
  28.             <list>      
  29.                 <value>classpath*:/hibernate-entities.xml</value>      
  30.             </list>      
  31.         </property>     
  32.         <property name="configurationClass" 
  33.                   value="org.hibernate.cfg.AnnotationConfiguration" />
  34.         <property name="hibernateProperties">      
  35.             <value>      
  36.                 hibernate.dialect=org.hibernate.dialect.MySQLDialect       
  37.             </value>      
  38.         </property>      
  39.     </bean>      
  40.       
  41.     <bean id="myEventDao"      
  42.         class="com.james.business.modle.dao.impl.EventDaoImpl">      
  43.         <property name="sessionFactory" ref="mySessionFactory" />      
  44.     </bean>      
  45.       
  46. </beans>  
  • 大小: 86.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics