随笔-8  评论-20  文章-4  trackbacks-0

        感谢昨天有人提醒用maven来管理所有依赖,今天稍微看了一下,终于把所有依赖交予了maven。也算对maven的掌握更进了一步。

        SSH笔记目录

       
 SSH笔记一  用maven构建项目(源码下载ssim1.rar)
        SSH笔记二  整合hibernate和spring(源码下载ssim2-maven.rar
        SSH笔记三  反向生成DAO  优化开发目录(源码下载ssim3-dao.rar
        SSH笔记四  整合struts2(源码下载ssim4-struts2.rar
        SSH笔记五  整合Tiles2(源码下载ssim5-tiles2.rar


        今天的任务是——向生成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,看看结果,如果顺利通过,该恭喜你完成了我们今天的任务了。明天再接再历

        下一次  SSH笔记四  整合struts2
posted on 2009-08-09 22:13 ju 阅读(3527) 评论(0)  编辑  收藏 所属分类: SSH系列

只有注册用户登录后才能发表评论。


网站导航: