ITeye博客

Spring + Hibernate Sample

Jameslyy · 2007-09-29 17:20 · Database ORM · Spring · XML · MySQL · Hibernate · Bean · Markdown

类库:在附件中。

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

java 代码

  1. public interface EventDao {
  2. Event testEvent();
  3. }
  4. public class EventDaoImpl extends HibernateDaoSupport implements EventDao {
  5. public Event testEvent() {
  6. Event e = (Event) getHibernateTemplate().get(Event.class, 1l);
  7. return e;
  8. }
  9. }

java 代码

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

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. <bean id="myDataSource"
  7. class="org.apache.commons.dbcp.BasicDataSource"
  8. destroy-method="close">
  9. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  10. <!-- ?useUnicode=true&characterEncoding=utf-8&autoReconnect=true -->
  11. <property name="url"
  12. value="jdbc:mysql://10.1.1.2:3306/test" />
  13. <property name="username" value="test" />
  14. <property name="password" value="test" />
  15. </bean>
  16. <bean id="mySessionFactory"
  17. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  18. <property name="dataSource" ref="myDataSource" />
  19. <!-- mappingResources -->
  20. <!-- com/james/business/modle/domain/Event.hbm.xml -->
  21. <!-- mappingLocations classpath:org/jbpm/**/*.hbm.xml -->
  22. <property name="configLocations">
  23. <list>
  24. <value>classpath*:/hibernate-entities.xml</value>
  25. </list>
  26. </property>
  27. <property name="configurationClass"
  28. value="org.hibernate.cfg.AnnotationConfiguration" />
  29. <property name="hibernateProperties">
  30. <value>
  31. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  32. </value>
  33. </property>
  34. </bean>
  35. <bean id="myEventDao"
  36. class="com.james.business.modle.dao.impl.EventDaoImpl">
  37. <property name="sessionFactory" ref="mySessionFactory" />
  38. </bean>
  39. </beans>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。