一个完整的Spring+Hibernate+FreeMarker系统搭建过程(一)

2014-11-24 00:38:39 · 作者: · 浏览: 0

用了一个星期时间终于将Spring+Hibernate+FreeMarker系统搭建完成了,现在将搭建的过程发布出来方便以后有和我一样的初学者

第一步

先新建一个WEB工程;导入相当的JAR包(我这里用的是Spring3.0,Hibernate-3.3,FreeMarker-2.3),这个就不用我多说了吧大笑,相信大家都知道。


第二步

配置数据库连接及连接池相关的信息。任何程序都离不开数据。奋斗这个相信大家都明白。

我这里的数据库配置文件为

/WEB-INF/config/jdbc.properties

配置内容如下

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://192.168.141.23:3306/exam characterEncoding=UTF-8

jdbc.username=root

jdbc.password=password

cpool.checkoutTimeout=5000

cpool.minPoolSize=1

cpool.maxPoolSize=50

cpool.maxIdleTime=7200

cpool.maxIdleTimeExcessConnections=1800

cpool.acquireIncrement=10

第三步

增加Spring配置文件并配置Hibernate相关的信息。

我这里的Spring配置文件为

/WEB-INF/config/application-context.xml

此文件的配置信息如下:

xmlns:jee="http://www.springframework.org/schema/jee" 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-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"

default-lazy-init="true">


/WEB-INF/config/jdbc.properties


classpath*:/com/onlineexam/exam/core/entity/hbm/*.hbm.xml

hibernate.dialect=org.hibernate.dialect.OracleDialect

hibernate.show_sql=false

hibernate.format_sql=false

hibernate.query.substitutions=true 1, false 0

hibernate.jdbc.batch_size=20

hibernate.hbm2ddl.auto=update

hibernate.show_sql=true

hibernate.cache.use_query_cache=false


上面是我对这个文件的配置信息做的一个具体的说明(只是我个人的理解,如果哪位高手感觉有什么不对的地方欢迎发贴讨论微笑。)


这个信息配置完成后,我们需要让系统知道这个配置文件信息。因此需要将这个配置文件的信息加到WEB.XML里面;下面的我们说第四步,增加系统对Spring+Hibernate的支持信息


第四步

配置WEB.XML,

下面的是我的配置

onlineexam

contextConfigLocation

/WEB-INF/config/application-context.xml

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

osivFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

encoding

*.do

encoding

*.htm

encoding

*. jsp

encoding

*.jspx

encoding

*.html

encoding

*.jhtml

osivFilter

/*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

20

到了这里Spring+Hibernate相关配置已经完成,下面我们进行第五步将FreeMarker加到Spring中。

第五步

配置FreeMarker信息,我们需要先做一些前期的准备。

首先增加数据国际化配置文件;我这里用