The "KeyValueDescriptionMapper" mapper:
<?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="KeyValueDescriptionMapper" >
<!--
author: Richard Golebiowski
Generic mapper used to return key/value information
-->
<resultMap id="ExtKeyValueMap" type="com.utilSpace.util.ExtKeyValuePair" >
<result column="ID" property="intKey" jdbcType="INTEGER" />
<result column="Value" property="value" jdbcType="VARCHAR" />
<result column="DescValue" property="descValue" jdbcType="VARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
ID, Value, DescValue
</sql>
<select id="selectByExample" resultMap="ExtKeyValueMap" parameterType="com.interbase.model.BaseExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from ${table}
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="ExtKeyValueMap" parameterType="com.interbase.model.BaseExample" >
select
<include refid="Base_Column_List" />
from ${table}
where ID = #{ID,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="com.interbase.model.BaseExample" >
delete from ${table}
where ID = #{ID,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.interbase.model.BaseExample" >
delete from ${table}
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.utilSpace.util.ExtKeyValuePair" >
insert into ${table} (ID, Value, DescValue
)
values (#{ID,jdbcType=INTEGER}, #{value,jdbcType=VARCHAR}, #{descValue,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.utilSpace.util.ExtKeyValuePair" >
insert into ${table}
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="ID != null" >
ID,
</if>
<if test="value != null" >
Value,
</if>
<if test="descValue != null" >
DescValue,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="ID != null" >
#{ID,jdbcType=INTEGER},
</if>
<if test="value != null" >
#{value,jdbcType=VARCHAR},
</if>
<if test="descValue != null" >
#{descValue,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.utilSpace.util.ExtKeyValuePair" >
update ${table}
<set >
<if test="value != null" >
Value = #{value,jdbcType=VARCHAR},
</if>
<if test="descValue != null" >
DescValue = #{descValue,jdbcType=VARCHAR},
</if>
</set>
where ID = #{ID,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.utilSpace.util.ExtKeyValuePair" >
update ${table}
set Value = #{value,jdbcType=VARCHAR},
DescValue = #{descValue,jdbcType=VARCHAR}
where ID = #{ID,jdbcType=INTEGER}
</update>
<!--
Select records from the database table specified by the string parameter
-->
<select id="selectAllKeyValue" resultMap="ExtKeyValueMap" parameterType="java.lang.String">
select ID, Value from ${value}
</select>
<!--
Select records from the database table specified by the string parameter
-->
<select id="loadDescSelectList" resultMap="ExtKeyValueMap" parameterType="java.lang.String">
select ID, Value, DescValue from ${value} order by DescValue
</select>
<!--
Select records from the database table specified by the string parameter and the "Value" field returned as "DescValue"
-->
<select id="loadValueDescSelectList" resultMap="ExtKeyValueMap" parameterType="java.lang.String">
select ID, Value as DescValue from ${value} order by DescValue
</select>
<!--
Select records from the database with the field specified by the "field" property returned as "Value"
-->
<select id="loadFieldAsValue" resultMap="ExtKeyValueMap" parameterType="com.interbase.model.BaseExample">
select ID, ${field} as Value from ${table}
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<!--
Select records from the database with the field specified by the "field" property returned as "DescValue"
-->
<select id="loadFieldAsDescValue" resultMap="ExtKeyValueMap" parameterType="com.interbase.model.BaseExample">
select ID, ${field} as DescValue from ${table}
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
</mapper>
Usage Examples:
Use "selectAllKeyValue" to get a list from the "luMediaType" table:
GenericDataService lstServiceTest = new GenericDataService("KeyValueDescriptionMapper");
List <ExtKeyValuePair> tstList = (List<ExtKeyValuePair>) lstServiceTest.selectAllKeyValue("luMediaType");
Using BaseExample to get a list from the "Funder" table with the "FunderShortName" database field as the value and sorted by "FunderShortName":
GenericDataService genService = new GenericDataService("KeyValueDescriptionMapper");
BaseExample baseExample = new BaseExample();
baseExample.setTable("Funder");
baseExample.setField("FunderShortName");
baseExample.setOrderByClause("FunderShortName");
List keyList = (List) genService.getFieldAsValue(baseExample);
The "getFieldAsValue" method for the example above:
public List getFieldAsValue(BaseExample baseExample){
SqlSession session = this.sqlSessionFactoryINform.openSession();
try {
@SuppressWarnings("unchecked")
List list = session.selectList(this.dataMapper + ".loadFieldAsValue", baseExample);
return list;
}catch(Exception e){
System.out.println(e.getMessage());
return null;
} finally {
session.close();
}
}
No comments:
Post a Comment
Thank you for commenting!