<?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">
<!-- 不使用namespace的话sql搜索定位会比较方便 -->
<mapper namespace="com.blueland.dao.AppDatabaseDao">
	<resultMap id="RM_PfAppDatabaseEntity" type="com.blueland.bo.AppDatabaseEntity">
        <result property="appId" column="app_id"/>
        <result property="typeName" column="type_name"/>
        <result property="url" column="url"/>
        <result property="instanceName" column="instance_name"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="driverName" column="driver_name"/>
	</resultMap>

	<sql id="PfAppDatabaseEntityColumns">
        	app_id,
        	type_name,
        	url,
        	instance_name,
        	username,
        	password,
        	driver_name
	</sql>
	
	<sql id="PfAppDatabaseEntityQueryColumns">
        	app_id,
        	type_name,
        	url,
        	instance_name,
        	username,
        	password,
        	driver_name
	</sql>
	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="insert" useGeneratedKeys="false" keyProperty="id">
        INSERT INTO t_pf_application_db (<include refid="PfAppDatabaseEntityColumns"/>) VALUES (
        	#{appId,jdbcType=VARCHAR},
        	#{typeName,jdbcType=VARCHAR},
        	#{url,jdbcType=VARCHAR},
        	#{instanceName,jdbcType=VARCHAR},
        	#{username,jdbcType=VARCHAR},
        	#{password,jdbcType=VARCHAR},
        	#{driverName,jdbcType=VARCHAR}
        )
	</insert>
    
    <select id="get" resultMap="RM_PfAppDatabaseEntity">
		SELECT <include refid="PfAppDatabaseEntityQueryColumns"/>
	    FROM t_pf_application_db 
        WHERE 
	        app_Id = #{appId,jdbcType=VARCHAR} 
	</select>
	
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="findList" resultMap="RM_PfAppDatabaseEntity">
    	SELECT <include refid="PfAppDatabaseEntityQueryColumns"/>
	    FROM t_pf_application_db 
		<include refid="findList_where"/>
		
		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
    </select>

	<select id="findListNew" resultMap="RM_PfAppDatabaseEntity">
		SELECT <include refid="PfAppDatabaseEntityQueryColumns"/>
		FROM t_pf_application_db
		<include refid="findList_where"/>

		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
	</select>

	<select id="findPage" resultMap="RM_PfAppDatabaseEntity">
		SELECT <include refid="PfAppDatabaseEntityQueryColumns"/>
		FROM t_pf_application_db
		<include refid="findList_where"/>

		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
	</select>


	<sql id="findList_where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      				
	       <if test="@Ognl@isNotEmpty(bo.appId)">
				AND app_id = #{bo.appId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.typeName)">
				AND type_name = #{bo.typeName,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.url)">
				AND url = #{bo.url,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.instanceName)">
				AND instance_name = #{bo.instanceName,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.username)">
				AND username = #{bo.username,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.password)">
				AND password = #{bo.password,jdbcType=VARCHAR}
			</if>
			<if test="@Ognl@isNotEmpty(bo.driverName)">
				AND driver_name = #{bo.driverName,jdbcType=VARCHAR}
			</if>
		</where>
	</sql>
    
    <!-- delete one AppDatabase by appId -->
	 <delete id="deleteAppDatabaseByAppId"  parameterType="java.lang.String">
		DELETE FROM t_pf_application_db WHERE app_id=#{appId,jdbcType=VARCHAR}
	</delete>
	
	<!-- 批量删除 -->
    <delete id="batchDeleteByAppId" parameterType="java.lang.String">
        delete from t_pf_application_db where app_id in
        <foreach item="appId" collection="array" open="(" separator="," close=")">
            #{appId}
        </foreach>
    </delete>
	
</mapper>

