<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:annotation-config />
<context:component-scan base-package="com.tiamaes.**">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/oracleGJDS"></property> </bean> -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${dataSource.className}" />
<property name="jdbcUrl" value="${dataSource.url}" />
<property name="user" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}" />
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="acquireRetryAttempts" value="${cpool.acquireRetryAttempts}" />
<property name="acquireRetryDelay" value="${cpool.acquireRetryDelay}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="rollbackOnCommitFailure" value="true"/>
</bean>
<!-- 事务装配 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="query*" propagation="NOT_SUPPORTED"/>
<tx:method name="get*" propagation="NOT_SUPPORTED"/>
<tx:method name="find*" propagation="NOT_SUPPORTED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.tiamaes.**.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- <aop:aspectj-autoproxy /> -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.tiamaes.gjds.app.bean" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tiamaes.gjds.app.persistence" />
</bean>
</beans>
configLocation:MyBatis配置文件
typeAliasesPackage:配置Bean目录
basePackage:配置Dao和配置文件的目录,配置文件的目录也可以单独配置
NOTE
可以在sqlSessionFactory
中配置mapperLocations
来指定配置文件的路径,如果不指定默认跟Dao在一个目录下
<?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>
<settings>
<!-- 使全局的映射器启用或禁用缓存。 -->
<setting name="cacheEnabled" value="true" />
<!-- 全局启用或禁用延迟加载。 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许JDBC支持生成的键。需要适合的驱动。如果设置为true则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如Derby)。 -->
<setting name="useGeneratedKeys" value="false" />
<!-- 配置默认的执行器。SIMPLE执行器没有什么特别之处。REUSE执行器重用预处理语句。BATCH执行器重用语句和批量更新 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间。 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings>
</configuration>
package com.tiamaes.gjds.app.persistence;
import java.util.List;
import java.util.Map;
import com.tiamaes.gjds.app.bean.GjdsSwLineInfo;
import com.tiamaes.gjds.app.bean.GjdsSwStationEntrance;
import com.tiamaes.gjds.app.bean.GjdsSwStationInfo;
/** * <p>类描述:轨道交通的接口 </p> * <p>创建时间:2014年12月15日 上午11:20:37 </p> */
public interface GjdsSwStationMapper {
/** * <p>方法描述:模糊匹配地铁的线路的站点信息</p> * <p>创建时间: 2014年12月15日 下午2:14:20 </p> * <p>修改记录:</p> * @param stationName * @return * List<GjdsSwStationInfo> */
public List<GjdsSwStationInfo> queryBystationNameLike(String stationName);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tiamaes.gjds.app.persistence.GjdsSwStationMapper">
<resultMap type="GjdsSwStationInfo" id="gjdsSwStationInfoMap">
<result property="stationId" column="STATION_ID" />
<result property="stationName" column="STATION_NAME" />
<result property="lng" column="LNG" />
<result property="lat" column="LAT" />
<result property="regionCode" column="REGION_CODE" />
<result property="spatial" column="SPATIAL" />
<result property="labelNo" column="LABEL_NO" />
<result property="startPassTime" column="START_PASS_TIME" />
<result property="endPassTime" column="END_PASS_TIME" />
<result property="isUpDown" column="IS_UP_DOWN" />
<result property="lineNo" column="LINE_NO" />
</resultMap>
<!-- 模糊匹配站点信息 -->
<select id="queryBystationNameLike" parameterType="String" resultMap="gjdsSwStationInfoMap">
SELECT STATION_ID,STATION_NAME,LNG,LAT FROM subway.GJDS_SW_STATION_INFO
WHERE STATION_NAME LIKE #{stationName,jdbcType=NVARCHAR}
OR STATION_PY LIKE UPPER(#{stationName,jdbcType=NVARCHAR})
</select>
</mapper>
namespace为接口的包名加类名