---
title: "Spring + Hibernate Sample"
author: "Jameslyy"
date: "2007-09-29T17:20:00Z"
tags: ["Bean", "Hibernate", "MySQL", "Spring", "XML"]
slug: "spring-hibernate-sample"
draft: false
lastmod: "2007-09-29T17:20:00Z"
id: 128263
url: "https://jamesblog.iteye.com/blog/128263"
---

**类库：在附件中。**

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 代码

01. public class AppContext {
02. /\**
03. * @param args
04. \*/
05. public static void main(String\[] args) {
06. // TODO Auto-generated method stub
07. AppContext ac = new AppContext();
08. ac.initContext();
09. }
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. &lt;!DOCTYPE hibernate-configuration PUBLIC
2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
4. &lt;hibernate-configuration&gt;
5. &lt;session-factory&gt;
6. &lt;mapping class="com.james.business.modle.domain.Event" /&gt;
7. &lt;/session-factory&gt;
8. &lt;/hibernate-configuration&gt;

application-context.xml

xml 代码

01. &lt;xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xsi:schemaLocation="http://www.springframework.org/schema/beans
05. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;
06. &lt;bean id="myDataSource"
07. class="org.apache.commons.dbcp.BasicDataSource"
08. destroy-method="close"&gt;
09. &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
10. &lt;!-- ?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true --&gt;
11. &lt;property name="url"
12. value="jdbc:mysql://10.1.1.2:3306/test" /&gt;
13. &lt;property name="username" value="test" /&gt;
14. &lt;property name="password" value="test" /&gt;
15. &lt;/bean&gt;
16. &lt;bean id="mySessionFactory"
17. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
18. &lt;property name="dataSource" ref="myDataSource" /&gt;
19. &lt;!-- mappingResources --&gt;
20. &lt;!-- com/james/business/modle/domain/Event.hbm.xml --&gt;
21. &lt;!-- mappingLocations classpath:org/jbpm/\*\*/\*.hbm.xml --&gt;
22. &lt;property name="configLocations"&gt;
23. &lt;list&gt;
24. &lt;value&gt;classpath\*:/hibernate-entities.xml&lt;/value&gt;
25. &lt;/list&gt;
26. &lt;/property&gt;
27. &lt;property name="configurationClass"
28. value="org.hibernate.cfg.AnnotationConfiguration" /&gt;
29. &lt;property name="hibernateProperties"&gt;
30. &lt;value&gt;
31. hibernate.dialect=org.hibernate.dialect.MySQLDialect
32. &lt;/value&gt;
33. &lt;/property&gt;
34. &lt;/bean&gt;
35. &lt;bean id="myEventDao"
36. class="com.james.business.modle.dao.impl.EventDaoImpl"&gt;
37. &lt;property name="sessionFactory" ref="mySessionFactory" /&gt;
38. &lt;/bean&gt;
39. &lt;/beans&gt;