这是我进公司的第5天,一切还处在学习和适应的阶段。但在这里的学习跟学校的方式大不相同,学校里一本书要学上半年,考试就是做题,而在公司经理给你一堆的文档、视频和实例,你必须在几天能学会使用以前都听都没听过的东西,没考试只有实战。下面我要写的就是这几天我接触的东西。,记下来以作总结,也为了不要忘记,说不定还可以顺便帮同行点忙。
Spring大家想必都听说过,可知道JSF和Ibatis的人就没那么都了,至少我在来之前没听过(我之前玩的比较多的是SSH)。JSF和Ibatis具体是什么,想要接触的人应该事先都会了解,这里就不再赘述。主要说一下这几天将JSF、Spring、Ibatis三者结合应用的收获和心得(以OperMasks Studio+SqlServer2000为例)。
框架间结合最关键的是配置文件间的结合。其中Spring的配置文件“applicationContext.xml”是主线,一切都围绕着它转。它有几部分组成。第一部分就是本来应该写在Ibatis配置文件“sqlmapconfig.xml”中的数据库连接信息,以datasource的形式写在applicationContext.xml中,然后用datasource和sqlmapconfig.xml来构建sqlMapClient(sqlmapconfig.xml中只需要配置一下Ibatis中的对应实体sql语句的配置文件“SqlMap.xml”的路径);第二部分是配置事务的传播特性配置;第三部分是DAO的配置;第四部分是Service的配置。具体代码如下:
sqlmapconfig.xml的代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings enhancementEnabled="true" useStatementNamespaces="true"/>
<!-- admin sqlMap-->
<sqlMap resource="com/hime/hpmp/admin/model/map/hm_attachments_SqlMap.xml"/>
<!-- List more here...
-->
</sqlMapConfig>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
default-lazy-init="true">
<!-- Attention: this bean is essential!
Making LiteBeans available in Spring container. -->
<bean class="org.operamasks.faces.spring.ManagedBeanConfigurer" />
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/init.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="1000"></property>
<property name="defaultAutoCommit" value="false"></property>
<property name="removeAbandoned" value="true"></property>
<property name="removeAbandonedTimeout" value="60"></property>
<property name="logAbandoned" value="true"></property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="WEB-INF/sqlmapconfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- ************************************************************************-->
<!-- //ibatis事务代理配置///-->
<!-- ************************************************************************-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
<tx:annotation-driven transaction-manager="transactionManager" />
-->
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!--
<tx:method name="*" read-only="true"/>
-->
</tx:attributes>
</tx:advice>
<!-- 配置哪些类哪些方法使用事务 -->
<aop:config>
<aop:pointcut id="allServiceMethod" expression="execution(* com.hime.hpmp.*.services.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod"/>
</aop:config>
<!-- DAO -->
<bean id="hmattachmentsDAO" class="com.hime.hpmp.admin.dao.HmAttachmentsDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<!-- Service -->
<bean id="hmattachmentsService" class="com.hime.hpmp.admin.services.HmAttachmentsService">
<property name="hmattachmentsDAO" ref="hmattachmentsDAO"/>
</bean>
</beans>
init.properties的内容:
url=jdbc/:sqlserver/://127.0.0.1/:1433;databaseName/=hpmp
password=123456
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
username=sa
确保这些配置文件没问题后,让每中框架做自己的事,把每一层搞清楚,一切就OK了。
ps:1、在整个过程中遇到了一个让人郁闷的不是问题的问题,就是OperaMasks Studio这个IDE想要往工程里导入jar直接通过构建工程的路径的方式导入有时可能导入不成功,应该要把jar包直接复制的工程的lib文件夹下,然后“项目---->清理”,清理下工程,才能保证万无一失。
2、在Spring的事务配置中会指定一些字符,以这些字符开头的方法才会进行事务控制,不然需要提交的方法(如修改、删除等)对数据库进行操作没有效果,在Service中写数据库操作方法时,方法名一定要跟applicationContext.xml中配置的事务控制保持一致,不然会遇到跟我同样的问题就是,所有的东西都没问题,也不报错,可就是运行不出结果,删不掉,改不了,这时候你就要想到事务控制了。
以上是我最近的收获,与大家分享下,水平有限,有纰漏之处敬请批评指正,谢!