採用mybatis動態sql語句最大的好處是不需要去拼sql語句,自身可以根據if條件,自行判斷,從而實現動態調整。
1、首先配置mybatis——sqlSessionFactory和sqlMap
sqlMap中將要查詢的語句文件寫成該格式目錄中:"classpath*:config/sqlmap/*-Mapping.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
。。。。。。。。。。。。。
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
。。。。。。。。。。。
</bean>
<bean id="sqlMap"
class="com.bocom.jump.bp.service.sqlmap.impl.SqlMapFactoryBean">
<qualifier value="main" />
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/sqlmap/Configuration.xml" />
<property name="mappingLocations" value="classpath*:config/sqlmap/*-Mapping.xml" />
</bean>
</beans>
2、創建sql查詢語句文本eg:interbank-rate-history-Mapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- *************************************************************** * @Module:
term-Mapping.xml * @Author: kevin * @Purpose: 此配置文件裝載條款ibatis配置信息 *************************************************************** -->
<mapper namespace="InterBankRateHistory">
<resultMap id="interBankInfo" type="com.bocom.rtis.model.InterBankRateHistory">
<result property="seq" column="sequence" />
<result property="quoteId" column="quote_id " />
<result property="productType" column="product_type" />
<result property="expireTime" column="expire_time" />
<result property="price" column="price" />
<result property="publishDate" column="publish_date"/>
<result property="valueDate" column="value_date"/>
<result property="quoteSource" column="ref_resource_id"/>
</resultMap>
<select id="queryInterBankRateHistory" parameterType="map"
resultMap="interBankInfo">
select * from interbank_rate_history
<where>
<if test="quoteId != null">
and quote_id =#{quoteId}
</if>
<if test="productType != null">
and product_type=#{productType}
</if>
<if test="expireTime != null">
and expire_time=#{expireTime}
</if>
<if test="valueDate != null">
and value_date=#{valueDate}
</if>
<if test="publishDate != null">
and publish_date=#{publishDate}
</if>
<if test="quoteSource != null">
and ref_resource_id=#{quoteSource}
</if>
</where>
</select>
</mapper>
namespace:是組名 select id="queryInterBankRateHistory" 是要查詢單條語句的id, parameterType="map" 傳入的參數爲map類型
resultMap="interBankInfo" 查詢的結果類型爲自定義的interBankInfo 這裏實際上是type="com.bocom.rtis.model.InterBankRateHistory"的對象
通常如查詢結果爲整數 直接寫成resultType="int"
3、java實現代碼
private SqlMap sqlMap;
public void test ()
{
// 設置查詢參數
Map<String, Object> params = new HashMap<String, Object>();
params.put("quoteId", “XXX”);
params.put("productType",“XXX”);
params.put("expireTime", “XXX”);
params.put("valueDate", “XXX”);
params.put("publishDate",“XXX”;
params.put("quoteSource",“XXX”);
// 採用動態sql語句獲取數據庫中的數據,sqlMap.queryForList()方法中第一參數是文本中的組名.id即namespace.id,params爲傳入的查詢參數,這裏返回的查詢結果爲一個List。
}