myBatis 动态sql实例

来源:互联网 发布:php文件管理器源码 编辑:程序博客网 时间:2024/05/20 07:37

采用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实现代码

@Autowired
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。
List<InterBankRateHistory> interBankRateHistoryList = sqlMap.queryForList("InterBankRateHistory.queryInterBankRateHistory", params);


}


0 0