感謝昨天有人提醒用maven來管理所有依賴,今天稍微看了一下,終於把所有依賴交予了maven。也算對maven的掌握更進了一步。
今天的任務是——反向生成DAO並優化開發目錄。完成的效果如下(左邊為包視圖、右邊為文件視圖):
1.反向生成DAO並測試(上次我們已經在ssimdb中建立了ssim_user表)
1.1 hibernate反向工程,切換到 Database Explorer ,選中ssim_user,右鍵選擇Hibernate Reverse Engineering,彈出如下視圖
1.2 點擊finish,完成反向工程後項目發生了如下變化
2.測試DAO並優化開發目錄
2.1創建測試父類 SpringTestCase.java,如下
package net.selitech.ssim.utils;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTestCase extends TestCase {
private ApplicationContext context;
@Override
protected void setUp(){
context = new ClassPathXmlApplicationContext(getPathName());
}
@Override
protected void tearDown(){
}
protected String getPathName() {
return "application-context.xml";
}
protected Object getBean(String name) {
return context.getBean(name);
}
protected ApplicationContext getContext() {
return context;
}
}
2.2 測試SimmUserDAO
package net.selitech.net.dao;
import java.util.List;
import net.selitech.ssim.dao.SsimUser;
import net.selitech.ssim.dao.SsimUserDAO;
import net.selitech.ssim.utils.SpringTestCase;
/**
* @author: juxuejian
* @filename: SsimUserDAOTest.java
* @date: Aug 9, 2009 , 9:14:54 PM
* @description :
*/
public class SsimUserDAOTest extends SpringTestCase{
SsimUserDAO ssimUserDAO;
public void setUp(){
super.setUp();
ssimUserDAO=(SsimUserDAO)getBean("SsimUserDAO");
}
public void testSsimUserDAO(){
List<SsimUser> list=ssimUserDAO.findAll();
for(SsimUser s:list){
System.out.println(s.getUserEmail()+"\t"+s.getUserRealname());
}
}
}
2.3 如果你正確的輸出了郵件地址以及姓名,恭喜你,如果沒有,不要灰心,再來一次,看哪裡有出入,仔細比對。
接下來我們講對開發目錄進行優化
3.優化目錄以及配置文件
3.1 將數據庫鏈接信息提取為配置文件 ssimdb-config.properties,如下
# database connection to data warehouse
#
ssimdb.mysql.url=jdbc\:mysql\://localhost\:3306/ssimdb
ssimdb.mysql.username=root
#密碼設置為自己的密碼。tomcyj是我的密碼哈
ssimdb.mysql.password=tomcyj
# debug information
#
hibernate.show_sql=false
3.2 修改application-conten.xml,如下
<?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">
<import resource="beans/data-source.xml" />
<import resource="beans/ssim-dao.xml" />
<bean id="configurator" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>ssimdb-config.properties</value>
</list>
</property>
</bean>
</beans>
3.3 正如你看到的,我們將數據連接信息以及DAO配置分別放在了 data-source.xml和ssim-dao.xml中,那我們就來創建他們。
data-source.xml
<?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="ssimdb.TxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="ssimdb.session.factory" />
</bean>
<bean id="ssimdb.mssql" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="${ssimdb.mysql.url}" />
<property name="username" value="${ssimdb.mysql.username}" />
<property name="password" value="${ssimdb.mysql.password}" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="2" />
<property name="maxActive" value="10" />
</bean>
<bean id="ssimdb.session.factory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="ssimdb.mssql" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>dao/SsimUser.hbm.xml</value>
</list>
</property>
</bean>
</beans>
ssim-dao.xml
<?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="SsimUserDAO" class="net.selitech.ssim.dao.SsimUserDAO">
<property name="sessionFactory">
<ref bean="ssimdb.session.factory" />
</property>
</bean>
</beans>
到了這裡,運行一下SsimUserDAOTest,看看結果,如果順利通過,該恭喜你完成了我們今天的任務了。