您的位置:首页 > 其它

SSH搭建过程详解(一)

2015-09-30 19:29 369 查看
多次搭建SSH框架,都是跟着别人做的,结果自己搭一个,发现很多地方之前都没有注意到。下面记录一下我这次搭建过程中新学到的东西。

首先学习到的就是一种好的操作习惯。以前我都是搭好了再测试,结果报错了,由于原理理解得不够,练习得不够,常常找不到是哪些问题。其实,到现在这个阶段,我们应该达到在1分钟内确定是什么问题。现在我认识到,SSH框架需要一步一步有序地进行搭建,就比如盖房子一样,要从下到上一步一步进行,而且每一步结束后就进行一个测试,保证之前做得都是正确的,再往下继续。

然后就是整个搭建的流程(前提就是SSH需要的jar包已经准备好,换言之,SSH使用版本已经确定。)

我把它分为四个阶段:由于较长分为两篇完成,这篇先写第一阶段和第二阶段,下篇完成第三阶段和第四阶段。

第一阶段:准备阶段。

1新建Web项目

2.修改编码为utf-8:

全局:修改eclipse或myeclipse默认工作空间的编码(preferences--->general--->workspace);修改某类文本文件的编码(preferences--->general--->Content Types下面的text中的每一个子项都可以根据需要修改);如果是Myeclipse,还可以从preferences--->myeclipse--->fiiles and
editors下面的子项可以根据需要修改。

局部:修改某一个工程的编码(右击项目--->properties,Resource下修改编码);修改某一个文本文件的编码(修改某个指定的文件可以右击选择properties,Resource下修改)

3.拷贝jar包







4.确定使用底层架构,比如使用三层,然后根据架构要求,新建各个source folder以及下面的package.



第二阶段:database+hibernate+部分spring+测试

1.这里选择MySQL数据库,新建一个数据库。

2.创建一个测试entity以及entity.hbm.xml.

package cn.itcast.s2sh.struts2.domain;

import java.io.Serializable;

public class Person implements Serializable{
private Long id;
private String name;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?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="cn.itcast.oa0909.domain.Person">
<id name="pid" type="java.lang.Long" length="10">
<generator class="increment"></generator>
</id>
<property name="pname" type="java.lang.String" length="20">
<column name="pname"></column>
</property>
</class>
</hibernate-mapping>
3.创建配置文件hibernate.cfg.xml(hibernate配置文件)和applicationContext.xml和applicationContext-db.xml(Spring配置文件)并写入基本配置。

在config下面创建这三个文件,最好不要手动创建,从现成的项目中进行拷贝或者官方下载的Demo中拷贝。然后在这基础之上进行修改。下面先给出内容,在进行分析。

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/itcastoa0923
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<!-- 引入各个entity.hbm.xml -->
<mapping resource="cn/itcast/s2sh/struts2/domain/Person.hbm.xml" />

</session-factory>
</hibernate-configuration>


配置内容:连接数据库的信息,驱动,数据库名称,用户名,密码。以及引入entity.hbm.xml文件,还有一些hiebernate的基本属性配置。比如dialect,hbm2dll.auto等。

applicationContext-db.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

</beans>


applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<import resource="applicationContext-db.xml"/>
</beans>


先说明applicationContext.xml和applicationContext-db.xml的关系。它们其实都可以写在applicationContext.xml里。但是后续applicationContext的配置文件还有很多,写在一起会混乱,不清晰,这样容易出错。那么就把复杂不同功能的配置分开写,然后引入到applicationContext.xml下,就是一个很好地处理办法。加载时直接加载applicaionContext.xml,它也会找到其他的配置文件。

这一步其实完成的就是Spring和Hibernate的整合。Hibernate的对象要交由Spring来管理。启动Hibernate的操作也由Spring来做。Spring用LocalSessionFactoryBean来加载hibernate.cfg.xml文件实现ORM操作。所以在在applicationContext-db.xml中要配置LocalSessionFactoryBean,这里要注意hibernate.cfg.xml的路径要写正确。

其实我在做项目中发现applicationContext.xml可以完全取代hibernate.cfg.xml文件。在hibernate.cfg.xml中的配置都可以写在applicationContext.xml中。下面我可以给出一个例子(这个不是搭建的一部分)

<!-- 定义数据库数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<!-- 定义会话工厂 ,并注入数据源实例dataSource -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/itcast/s2sh/struts2/domain/Person.hbm.xml</value>
</list>
</property>
</bean>


4,.写SpringTest类,测试SessionFactory是否正确。

package cn.itcast.oa.test;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

// 测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}

}
test不报错,这说明spring和hibernate的整合没有问题。
这里我想说明一下这里怎么写得测试。这里前面的框架都还没有搭好,那么怎么能启动Spring容器,实例化对象呢?手动new出一个容器。

private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

通过ClassPathXmlApplication对象加载applicationContext.xml配置,实例化出配置好的对象,然后通过getBean("...")参数为配置文件中id值读出对象。就可以辅助我们做测试了。

文章太长,阶段三和四下篇继续。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: