SSH+Tomcat+MySQL项目搭建

来源:互联网 发布:哪个软件可以看素媛 编辑:程序博客网 时间:2024/06/05 07:16

一.新建web项目

新建工作空间指定项目编码(或工作空间编码)为utf-8,再建 web project,配置buildpath,引入tomcat 的包,选择对应配置的tomcat版本即可。

添加jstl jar包和mysql驱动包;


二.框架整合

1.添加struts2jar包和配置文件:

    添加jar

commons-fileupload-1.3.1.jarcommons-io-2.2.jarcommons-lang-2.4.jar ,commons-lang3-3.2.jarfreemarker-2.3.19.jarognl-3.0.6.jarstruts2-core-2.x.jar,struts2-spring-plugin-2.x.jarxwork-core-2.x.jar web-inf/lib目录下

    添加struts.xmlsrc目录下。可在“struts-2.x\apps\struts2-blank\WEB-INF\classes”下复制。

struts.xml中添加几个常用属性:

<!-- 禁用动态方法访问 -->

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<!-- 配置成开发模式 -->

<constant name="struts.devMode" value="true" />

<!-- 配置拓展名为action -->

<constant name="struts.action.extention" value="action" />

<!-- 把主题配置成simple -->

<constant name="struts.ui.theme" value="simple" />

     配置web.xml:添加struts2 过滤器:

<filter>

         <filter-name>struts2</filter-name>    

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

     </filter>

     <filter-mapping>

         <filter-name>struts2</filter-name>

         <url-pattern>*.action</url-pattern>

    </filter-mapping>


2. 添加hibernate的jar包和配置文件

添加hibernate jar

  hibernate3.jar,lib/required/*.jarlib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jarlib\bytecode\cglib\cglib-2.2.jarweb-inf/lib目录下。

  至于hibernate.cfg.xml文件,因项目使用spring来整合管理实体和数据库的连接等hibernate原本的工作,所以这个配置文件不再需要。

3.添加spring的jar包和配置文件

添加spring3.0.2中的jar


添加spring配置文件applicationContext.xml src目录下;

web.xml中注册spring监听器,启动spring容器:

<listener>

         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

     </listener>

     <context-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>classpath:applicationContext.xml</param-value>

     </context-param>

4.整合spring和hibernate

applicationContext.xml中配置如下原本在hibernate.cfg.xml中需要配置的信息,在spring中配置后hibernate.cfg.xml 可删除。

配置c3p0数据库连接源:

<!-- 导入外部的properties配置文件 -->

<context:property-placeholder location="classpath:db.properties" />

<!-- 配置c3p0数据源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

<property name="jdbcUrl" value="${jdbcUrl}"></property>

<property name="driverClass" value="${driverClass}"></property>

<property name="user" value="${user}"></property>

<property name="password" value="${password}"></property>

<!--初始化时获取三个连接,取值应在minPoolSizemaxPoolSize之间。Default: 3 -->

<property name="initialPoolSize" value="${initialPoolSize}"></property>

<!--连接池中保留的最小连接数。Default: 3 -->

<property name="minPoolSize" value="3"></property>

<!--连接池中保留的最大连接数。Default: 15 -->

<property name="maxPoolSize" value="${maxPoolSize}"></property>

<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->

<property name="acquireIncrement" value="3"></property>

<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->

<property name="maxIdleTime" value="1800"></property>

</bean>

db.properties

jdbcUrl=jdbc:mysql://localhost:3306/itcastTax?useUnicode=true&characterEncoding=utf8

driverClass=com.mysql.jdbc.Driver

user=root

password=root

initialPoolSize=10

maxPoolSize=30

配置sessionFactory,并将dataSource指向c3p0创建的dataSource

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="javax.persistence.validation.mode">none</prop>

</props>

</property>

<property name="mappingLocations">

<list>

<value>classpath:*/entity/*.hbm.xml</value>  <!--映射文件路径-->

</list>

</property>

</bean>

5.配置spring事务管理

<!—事务管理-->

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!—事务通知-->

<tx:advice id="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:method name="find*" read-only="true" />

<tx:method name="get*" read-only="true" />

<tx:method name="load*" read-only="true" />

<tx:method name="list*" read-only="true" />

<tx:method name="search*" read-only="true" />

<tx:method name="*" rollback-for="Throwable" />

</tx:attributes>

</tx:advice>

 

<!—配置需要进行事务控制的类 -->

<aop:config>

<aop:pointcut id="serviceOperation" expression="bean(*Service)" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />

</aop:config>

【注意:上面的pointcut  expression 表示拦截以Service结尾的bean,或者可写成execution(* cn.itcast..service.impl.*.*(..))】













0 0
原创粉丝点击