一.ibatis单独配置实用:
***ibatis中#与$的区别:前者传入的会匹配数据类型,后者按实际值传入不会转换。如:id=#id# name=#id# 前者整型,后者自动转换为字符串 id=$id$ 永远是传入的值
1.sql-map-config典型配置
<sqlMapConfig>
<properties resource="com/a.properties"/>
<settings maxRequests="256" maxSessions="64" maxTransactions="16"/>
<transactionManager type="JDBC">
<!-- dbcp数据源 -->
<dataSource type="DBCP">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
<!-- OPTIONAL PROPERTIES BELOW -->
<property name="Pool.MaximumActiveConnections" value="10"/> --最大连接
<property name="Pool.MaximumIdleConnections" value="5"/> --最大空闲连接
<property name="Pool.MaximumWait" value="60000"/> --等待时间
<!-- Use of the validation query can be problematic.
If you have difficulty, try without it. -->
<property name="Pool.ValidationQuery" value="select * from acount"/> --检测连接是否可用
<property name="Pool.LogAbandoned" value="false"/>
<property name="Pool.RemoveAbandoned" value="false"/>
<property name="Pool.RemoveAbandonedTimeout" value="50000"/>
</dataSource>
<!-- jndi数据源 -->
<dataSource type="JNDI">
<property name="DataSource" value="java:comp/env/testjndi"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/zte/bean/user.xml"/>
</sqlMapConfig>
2.sql-map典型配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap SYSTEM "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap>
<typeAlias alias="user" type="com.zte.bean.User"/>
<typeAlias alias="userform" type="com.zte.form.UserForm"/>
<cacheModel type="LRU" id="cache">
<flushInterval minutes="1"/>
<flushOnExecute statement="register"/>
<property name="size" value="1000"/>
</cacheModel>
<parameterMap class="java.util.HashMap" id="para">
<parameter property="u_name" javaType="java.lang.String" jdbcType="VARCHAR" mode="IN" nullValue="a"/>
<parameter property="u_id" javaType="java.lang.Integer" jdbcType="INTEGER" mode="OUT" nullValue="1"/>
</parameterMap>
<resultMap class="user" id="re">
<result property="u_name" column="u_name"/>
<result property="u_id" column="u_id"/>
</resultMap>
<statement id="register" parameterClass="userform">
insert into test values(#u_id#,#u_name#,#u_pwd#)
</statement>
<statement id="show" resultMap="re">
select * from test where u_pwd=#u_pwd#
</statement>
<statement id="a" parameterClass="userform" resultMap="re">
select * from test
<dynamic prepend="where">
<isNull prepend="and" property="u_name">u_pwd=#u_pwd#</isNull><!-- 当为null时 -->
<isEmpty prepend="and" property="u_pwd">u_id=#u_id#</isEmpty><!-- 当为""时,包括null -->
<isEqual prepend="and" property="u_name" compareValue="1234">u_pwd=#u_pwd#</isEqual><!-- 当和值比较相等 -->
<isEqual prepend="and" property="u_pwd" compareProperty="u_name">u_id=#u_id#</isEqual><!-- 当和对象比较相等 -->
<isGreaterEqual prepend="and" property="" compareValue="">...</isGreaterEqual><!-- 当大于等于值 -->
<isGreaterEqual prepend="and" property="u_name" compareProperty="u_pwd">u_pwd=#u_pwd#</isGreaterEqual><!-- 当大于等于对象 -->
<isGreaterThan prepend="and" property="" compareValue="">...</isGreaterThan><!-- 当大于值 -->
<isGreaterThan prepend="and" property="u_name" compareProperty="u_pwd">u_pwd=#u_pwd#</isGreaterThan><!-- 当大于对象 -->
</dynamic>
</statement>
<procedure id="" parameterMap="para">
{call pro(?,?)}
</procedure>
</sqlMap>
重要说明:
jdbcType的类型都大写,即java.sql.types里要有这个属性,否则报类型无效
对于存储过程的java代码调用:
HashMap a=new HashMap();
a.put("u_name",uf.getU_name());
a.put("u_pwd",uf.getU_pwd());
getSqlMapClientTemplate().update("pro_add3",a);
System.out.println("aaaaaaaaa"+a.get("p_return"));
二.sping,struts1.x与jdbc的集成
1.web.xml配置监听器以启动sping容器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2.struts配置action的type属性设为:将action交给sping来管理。 org.springframework.web.struts.DelegatingActionProxy
3.sping配置具体action:说明:name属性与action的path属性一致。
<bean name="/user" class="com.zte.test.action.UserAction">
<property name="dao" ref="daoimpl"></property>
</bean>
4.数据源的配置:
dbcp:<bean id="theDatasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@10.130.50.130:1521:zxin130"/>
<property name="username" value="zxdbp_187" />
<property name="password" value="zxdbp_187" />
</bean>
jndi:<bean id="theDatasource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/test" />
</bean>
两个数据源返回的为DataSource,bean属性一定为dataSource并且setter方法已经有了
sping能加载属性文件吗????????
三.sping,struts1.x与ibatis的集成
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
</bean>
<bean id="daoimpl" class="com.zte.test.daoimp.UserDaoImpl">
<property name="dataSource" ref="theDatasource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property> <!-- "dataSource sqlMapClient"固定,并且setter方法已实现 -->
</bean>
当然dataSource数据源也可以配在ibatis里,配了此项删除。
四.sping,struts2.x与持久层集成
只需要将struts2-spring-plugin-2.1.8.1.jar放在lib下就OK了,就是说action实例由spring创建
<bean id="action" class="com.zte.action.UserAction">
<property name="dao" ref="daoimpl"></property>
</bean>
<action name="user_*" class="action" method="{1}"></action>
此外就是单个框架的配置或者ssi的配置了
五.spring的常用和各种框架集成配置:
<?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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd">
<!-- 读取properties数据配置文件 -->
<bean id="pro" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="config/config.properties"></property>
</bean>
<!-- jndi数据源 -->
<bean id="datajndi" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/test"></property>
</bean>
<!-- dbcp数据源 -->
<bean id="datadbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 读取ibatis -->
<bean id="sqlmap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
</bean>
<!-- 读取hibernate -->
<bean id="hiber" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datajndi"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- ibatis实现类 -->
<bean id="daoimplibatis" class="UserDaoImplibatis">
<property name="dataSource" ref="datajndi"></property>
<property name="sqlMapClient" ref="sqlmap"></property>
</bean>
<!-- jdbc实现类 -->
<bean id="daoimpljdbc" class="UserDaoImpljdbc">
<property name="dataSource" ref="datajndi"></property>
</bean>
<!-- hibernate实现类 -->
<bean id="daoimplhibernate" class="UserDaoImplhibernate">
<property name="sessionFactory" ref="hiber"></property>
</bean>
<!-- action代理配置 -->
<bean name="/user" class="com.zte.action.UserAction">
<property name="daoibatis" ref="daoimplibatis"></property>
<property name="daoidbc" ref="daoimpljdbc"></property>
</bean>
<!-- jdbc事务管理器 -->
<bean id="tranjdbc" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datajndi"></property>
</bean>
<!-- hibernate事务管理器 -->
<bean id="tranhi" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hiber"></property>
</bean>
<!-- xml事务配置-->
<tx:advice id="tx" transaction-manager="tran">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="cut" expression="excution(* com.zte.daoimpl.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="cut"/>
</aop:config>
<!-- 事务注解配置打开开关-->
<tx:annotation-driven transaction-manager="tran"/>
然后在service类里方法上用@Transactional 标注就可以了,默认是运行时RuntimeException 事务回滚
**写了@Transactional 就不能throw exception或try..catch了,会导致事务处理失效,一般写在服务层,在控制层统一处理异常
</beans>
注:1.要使事务生效,最好将try..catch放在struts等的web层处理。
如果使用了struts1.x的统一异常处理并且只有一个异常,可在service层try..catch,然后在catch里throw new UnifyException("",""),这样web层就不用处理了.当然也可以处理。但是当要处理多个异常时,就不能在web层处理,直接throw new UnifyException
2.如果要使用ibatis的批量处理,使用getSqlMapClientTemplate().execute(new SqlMapClientCallback(){});
3.使用jdbc的话,就用getJdbcTemplate().execute(new ConnectionCallback(){});
ps:当然也可以在出现错误处或禁止这样做的地方,return 跳转到统一的错误页面,显示错误信息,如果是国际化的错误信息,需要写获取资源的类。
jdbc批量提交:
con.setAutoCommit(false);
for(int i=0;i<200000;i++){
pre.addBatch();
if(i==500){ //一定要写,否则第二次请求报内存泄漏(不要大批量一起提交)
pre.executeBatch();
}
}
pre.executeBatch();
当使用ibatis批量提交时,如果数据太多,建议不要用,效率太低,改用jdbc的。
for(int i=0;i<uf.size();i++){
if(i%50000==0){
arg.startBatch();
}
arg.insert("register",uf.get(i));
if(i%49999==0&&i!=0){
arg.executeBatch();
}
}
arg.executeBatch();
###要使用sshi等框架的集成,必须使用的jar包:commons-beanutils-1.8.0.jar,cglib-nodep-2.1_3.jar(aspectjrt.jar,aspectjweaver.jar注解事务使用),commons-collections-3.2.1.jar,commons-logging-1.0.4.jar