而目前較流行的 ORM 解決方案大概就是 Hibernate 了,所以來看看如何用Spring來整合吧
首先以下的測試環境為 Eclipse 一般的 Java Application,並搭配 DBCP 連接池
專案建立好後 在 src 目錄下先來建立一下測試用的映射文件以及 DAO ((model 省略...
注意一點的是 映射文件和 model 盡量放在同一目錄下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.pojo.Good" table="goods" >
<id name="id" column="id">
<generator class="native" />
</id>
<property name="viewpic" column="viewpic" />
<property name="name" column="name" />
<property name="price" column="price" />
<property name="storenum" column="storenum" />
<property name="maaker" column="maker" />
<property name="info" column="info" />
<property name="uptime" column="uptime" />
</class>
</hibernate-mapping>
package org.dao; import java.util.List; import org.pojo.Good; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /*** 在 Spring 框架中,將自定義的 DAO 繼承 org.springframework.orm.hibernate3.support.HibernateDaoSupport ***/ public class GoodDao extends HibernateDaoSupport //繼承 HibernateDaoSupport { public void insert(Good good){ getHibernateTemplate().save(good); } public Good findById(int id){ Good good = null; good = (Good)getHibernateTemplate().get(Good.class, id); return good; } public List findAll(){ List list = getHibernateTemplate().find("FROM Good"); return list; } }
接下來在 src 目錄下配置一下 ApplicationContext.xml,程式大致如下
以下有一段跟配置DBCP連接持的做法一樣
可參考這篇文章 http://programer-learn.blogspot.com/2011/03/spring-dbcp.html
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shopcar"/>
<property name="username" value="root"/>
<property name="password" value="ooxxooxx"/>
</bean>
//命名為 sessionFactory 的 bean, 代表hibernate的sessionFactory
//需將 dataSource 注入給它
//需將 dataSource 注入給它
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
//以下是 Hibernate 配置文件中常見的 很眼熟吧
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
//這裡註冊了 Hibernate 的映射文件
<property name="mappingResources">
<list>
<value>org/pojo/Good.hbm.xml</value>
</list>
</property>
</bean>
//將 sessionFactory 注入到操作資料庫的dao
<bean id="GoodDao" class="org.dao.GoodDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
最後寫點測試程式測試一下吧!!
package org.test; import java.util.List; import java.util.Map; import org.dao.GoodDao; import org.pojo.Good; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class testDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); GoodDao dao = (GoodDao)context.getBean("GoodDao"); Good g = new Good(); g.setInfo("耐磨耐打"); g.setName("abc籃球"); g.setMaaker("NIKE"); g.setPrice(900); g.setStorenum(100); g.setUptime(new java.util.Date()); g.setViewpic("goods/good20.jpg"); dao.insert(g); List list = (List)dao.findAll(); for(int i=0;i<list.size();i++){ Good good = (Good)list.get(i); System.out.println(good.getName()); } } }
沒有留言:
張貼留言