databaseIdProvider
这里的 DB_VENDOR 会通过 DatabaseMetaData#getDatabaseProductName() 返回的字符串进行设置 见JDBC-基础。 由于通常情况下这个字符串都非常长而且相同产品的不同版本会返回不同的值,所以最好通过设置属性别名来使其变短
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql"/>
<property name="Oracle" value="oracleeee" />
<property name="SQL Server" value="sqlserver" />
</databaseIdProvider>
<select id="selectOneUser" resultType="com.tiantian.mybatis.entity.User" databaseId="mysql">
select qqq, name, age from pcmc_user where qqq = #{id}
</select>
<select id="selectOneUser" resultType="com.tiantian.mybatis.entity.User" databaseId="oracleeee">
select id qqq, name, age from user where id = #{id} -- 此时表名可能不一样,字段也不一样,就要起别名
</select>
注意:此时需要引入oracle驱动jar,db.properties也需要写好连接信息
_databaseId动态选择执行的sql
<select id="selectUsrs" databaseId="mysql" resultType="com.heiketu.pojo.Users">
<if test="_databaseId == 'mysql'">
select * from usrs where id = 2
</if>
</select>
_parameter判断要不要增加条件
<select id="findPyjp" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ESB_INTERFACE
<where>
JKZT = '1'
<if test="_parameter != null and _parameter != ''">
and CONCAT(CONCAT(CONCAT('',jkmc),jkbh),pyjp) like '%' || #{jkmc,jdbcType=VARCHAR} || '%'
</if>
</where>
</select>
鉴别器
将查出来的数据,按照某一字段进行分类,第一类封装为a,第二类封装为b,比如:查出来是女生就给名字前加Miss,查出来是男生就给名字前加Mr。
<?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.xxx.entity.mapper.EmpMapper">
<resultMap id="employeeMap" type="com.xxx.entity.Emp">
<id property="id" column="id" />
<result property="empName" column="emp_name" />
<result property="sex" column="sex" />
<association property="empCard" column="id"
select="com.xxx.entity.mapper.EmpCardMapper.getEmpCardByEmpId" />
<collection property="projectList" column="id"
select="com.xxx.entity.mapper.ProjectMapper.findProjectByEmpId" />
<discriminator javaType="int" column="sex">
//这里设置为简单类型的话就不写resultMap,写resultType就可以
<case value="1" resultMap="maleEmpMap" />
<case value="2" resultMap="femaleEmpMap" />
</discriminator>
</resultMap>
<select id="getEmp" parameterType="int" resultMap="empMap">
select id, emp_name as empName, sex from t_employee where id =#{id}
</select>
<resultMap id="maleEmpMap" type="com.xxx.pojo.MaleEmp" extends="empMap">
<collection property="prostateList" select="com.xxx.mapper.MaleEmpMapper.findProstateList" column="id" />
</resultMap>
<resultMap id="femaleEmpMap" type="com.xxx.pojo.FemaleEmp" extends="employeeMap">
<collection property="uterusList" select="com.xxx.mapper.FemaleEmpMapper.findUterusList" column="id" />
</resultMap>
</mapper>
<![CDATA[转义字符]]>
–将转义字符放在 <![CDATA[转义字符]]> 中
–写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,使用XML语法<![CDATA[ ]]>
<select id="allUserInfo" parameterType="java.util.HashMap" resultMap="userInfo1">
<![CDATA[
SELECT newsEdit,newsId, newstitle FROM shoppingGuide WHERE 1=1 AND newsday > #{startTime} AND newsday <= #{endTime}
]]>
<if test="etidName!=''">
AND newsEdit=#{etidName}
</if>
</select>
–但是有个问题那就是 等这些标签都不会被解析,
–所以我们只把有特殊字符的语句放在 <![CDATA[ ]]> 尽量缩小 <![CDATA[ ]]> 的范围
SELECT state FROM emp WHERE state <![CDATA[<>]]>‘effect’
调用存储过程
输入参数out,输入参数in statementType="CALLABLE"
什么时候使用,jdbcType=NUMERIC
<select id="queryTemporaryTable" parameterType="java.util.HashMap" resultType="java.util.LinkedHashMap">
{call proc_resource_plan #{project_id}, #{cut_off_time}, #{djh}, #{startNum}, #{endNum}, #{hasrow}}
</select> // 长亮写法
<select id="queryFullAttendSheet" parameterType="java.util.HashMap" resultType="java.util.LinkedHashMap">
{call [pro_full_attend] @date_start=#{date_start}, @date_end=#{date_end}, @org_name=#{org_name} }
</select>
调用insert_user存储过程
<!-- 添加用户 -->
<insert id="addUser" parameterType="user" statementType="CALLABLE">
{call insert_user(
#{id, mode=OUT, jdbcType=INTEGER},
#{name, mode=IN,jdbcType=NUMERIC},
#{sex, mode=IN,jdbcType=NUMERIC},
#{age, mode=IN,jdbcType=NUMERIC})}
</insert>
调用deleteUser存储过程
<!-- 删除用户 -->
<delete id="deleteUser" parameterType="Integer" statementType="CALLABLE">
{call deleteUser(#{id,mode=IN,jdbcType=NUMERIC})}
</delete>
调用updateUser存储过程
<!-- 更新用户 -->
<update id="updateUser" parameterType="user" statementType="CALLABLE">
{call updateUser( #{id,mode=IN,jdbcType=NUMERIC}, #{name,mode=IN,jdbcType=NUMERIC}, #{sex,mode=IN,jdbcType=NUMERIC}, #{age,mode=IN,jdbcType=NUMERIC} ) }
</update>
调用getUserById存储过程
<!-- 根据id查询用户 -->
<select id="getUserById" parameterType="Integer" resultType="user" statementType="CALLABLE">
{call getUserById( #{id,mode=IN,jdbcType=NUMERIC} ) }
</select>
复杂的参数类型parameterMap
<select id="texuChange" parameterMap="texuChangeMap" statementType="CALLABLE" resultType="String">
{call pl_texu( ?,?,?,?,?,?,?)}
</select>
<parameterMap type="java.util.Map" id="texuChangeMap">
<parameter property="v_tz_flg" jdbcType="INTEGER" mode="IN"/>
<parameter property="v_tz_qujian_start" jdbcType="DATE" mode="IN"/>
<parameter property="v_tz_qujian_end" jdbcType="DATE" mode="IN"/>
<parameter property="v_baoliu_p" jdbcType="INTEGER" mode="IN"/>
<parameter property="v_max_p" jdbcType="INTEGER" mode="IN"/>
<parameter property="v_stop_run" jdbcType="INTEGER" mode="IN"/>
<parameter property="p_result" jdbcType="VARCHAR" mode="OUT"/>
</parameterMap>
bind
<!--
bind标签可以使用OGNL表达式创建一个变量并将其绑定到上下文中。
需求:
concat函数连接字符串,在MySQL中,这个函数支持多个参数,但在Oracle中只支持两个参数。
由于不同数据库之间的语法差异,如果更换数据库,有些SQL语句可能就需要重写。针对这种情况
可以使用bin标签来避免由于更换数据库带来的一些麻烦。
<bind>属性:
name:为绑定到上下文的变量名
value:为OGNL表达式。
使用bind标签拼接字符串不仅可以避免因更换数据库而修改SQL,也能预防SQL注入。
-->
<!--原代码-->
<if test="username!=null and userName!=''">
and user_name like concat('%',#{userName},'%')
</if>
<!--改进后代码-->
<if test="username!=null and userName!=''">
<bind name="userNameLike" value="'%'+userName+'%'"/>
and user_name like #{userNameLike}
</if>