spring 中 mybatis的配置
1、独立的mybatis属性配置(MyBatisSqlMapConfig.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 查看mybatis.dtd,发现整合 spring 以后 只有settings 和 typeAliases 保留其他属性都会被spring覆盖 --> <!-- settings 属性 --> <settings> <setting name="cacheEnabled" value="true" /> <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <!-- typeAliases 属性 --> <typeAliases> <package name="com.lw.domain"/> </typeAliases> </configuration>
2、数据源和事物配置(applicationContext-database.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 测试库数据源 --> <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown" > <property name="dataSource"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.249.86:1521:eis11g" /> </bean> </property> <property name="user" value="bpw"/> <property name="password" value="bpw"/> <property name="jdbcTestStmt" value=" select 0 from dual "/> </bean> <!-- JotmFactoryBean是本地实例,具体代码在文章的最后面 --> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction" ref="jotm" /> </bean> <!-- 注解:事物 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
3、mybatis整合到spring中,(applicationContext-mybatis.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- scan for mappers of oracle and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lw.persistence.mapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:context/MyBatisSqlMapConfig.xml"/> <!-- 类型别名(entity等) package --> <!-- 类型别名package配置可以在这里,或者在classpath:context/MyBatisSqlMapConfig.xml配置文件里面 --> <!-- <property name="typeAliasesPackage" value="com.lw.domain"/> --> </bean> </beans>
4、spring 的一些配置 (applicationContext-beans.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:property-placeholder location="classpath:*/*.properties" ignore-unresolvable="true" /> <!-- enable component scanning (beware that this does not enable mapper scanning!) --> <context:component-scan base-package="com.lw" /> <!-- enable autowire --> <context:annotation-config /> </beans>
5、struts2.xml 的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="user" extends="struts-default" namespace="/user"> <action name="user" class="com.lw.action.UserAction"> <result name="success">/index.jsp</result> <result name="error">/login.jsp</result> </action> </package> </struts>
6、web.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- spring 配置文件加载 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:context/applicationContext-*.xml </param-value> </context-param> <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>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- spring 监听,启动程序时加载spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- session过期时间m --> <session-config> <session-timeout>240</session-timeout> </session-config> <welcome-file-list> <welcome-file>login.html</welcome-file> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
注:(org.springframework.transaction.jta.JotmFactoryBean.java)
org.springframework.beans.factory.FactoryBean.java
org.springframework.beans.factory.DisposableBean.java
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.jta;
import javax.naming.NamingException;
import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
/**
* FactoryBean that retrieves the JTA UserTransaction/TransactionManager for
* ObjectWeb's <a href="http://jotm.objectweb.org">JOTM</a>. Will retrieve an
* already active JOTM instance if found (e.g. if running in JOnAS), else create
* a new local JOTM instance. The same object implements both the UserTransaction
* and the TransactionManager interface, as returned by this FactoryBean.
*
* <p>A local JOTM instance is well-suited for working in conjunction with
* ObjectWeb's <a href="http://xapool.experlog.com">XAPool</a>, e.g. with bean
* definitions like the following:
*
* <pre>
* <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
*
* <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
* <property name="userTransaction" ref="jotm"/>
* </bean>
*
* <bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
* <property name="transactionManager" ref="jotm"/>
* <property name="driverName" value="..."/>
* <property name="url" value="..."/>
* </bean>
*
* <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
* <property name="dataSource" ref="innerDataSource"/>
* <property name="user" value="..."/>
* <property name="password" value="..."/>
* <property name="maxSize" value="..."/>
* </bean></pre>
*
* Uses JOTM's static access method to obtain the JOTM Current object, which
* implements both the UserTransaction and the TransactionManager interface.
*
* @author Juergen Hoeller
* @since 21.01.2004
* @see JtaTransactionManager#setUserTransaction
* @see JtaTransactionManager#setTransactionManager
* @see org.objectweb.jotm.Current
*/
public class JotmFactoryBean implements FactoryBean, DisposableBean {
private Current jotmCurrent;
private Jotm jotm;
public JotmFactoryBean() throws NamingException {
// Check for already active JOTM instance.
this.jotmCurrent = Current.getCurrent();
// If none found, create new local JOTM instance.
if (this.jotmCurrent == null) {
// Only for use within the current Spring context:
// local, not bound to registry.
this.jotm = new Jotm(true, false);
this.jotmCurrent = Current.getCurrent();
}
}
/**
* Set the default transaction timeout for the JOTM instance.
* <p>Should only be called for a local JOTM instance,
* not when accessing an existing (shared) JOTM instance.
*/
public void setDefaultTimeout(int defaultTimeout) {
this.jotmCurrent.setDefaultTimeout(defaultTimeout);
}
/**
* Return the JOTM instance created by this factory bean, if any.
* Will be <code>null</code> if an already active JOTM instance is used.
* <p>Application code should never need to access this.
*/
public Jotm getJotm() {
return jotm;
}
public Object getObject() {
return this.jotmCurrent;
}
public Class getObjectType() {
return this.jotmCurrent.getClass();
}
public boolean isSingleton() {
return true;
}
/**
* Stop the local JOTM instance, if created by this FactoryBean.
*/
public void destroy() {
if (this.jotm != null) {
this.jotm.stop();
}
}
}
java程序资源在附件, 表结构可以到http://davidhhs.iteye.com/admin/blogs/2037902
jar包没有导入太多了。