<mapper namespace="vallue " ></mapper>java
当使用相对于类路径的资源引用和彻底限定资源定位符(URL)映射器时,namespace=”valeu” value的值能够任意指定,而且mapper映射文件的位置能够任意指定.sql
<mapper namespace="nnn" ></mapper>数据库
<mapper namespace="com.lifeibai.dao.UserDao" ></mapper>缓存
<mapper namespace="vallue " > 这里是mapper的元素</mapper>mybatis
<!-- 自定义条件查询用户列表 -->app
<sql id="sometable">性能
${prefix}Tablespa
</sql>3d
<sql id="someinclude">对象
from
<include refid="${include_target}"/>
</sql>
<select id="select" resultType="map">
select
field1, field2, field3
<include refid="someinclude">
<property name="prefix" value="Some"/>
<property name="include_target" value="sometable"/>
</include>
</select>
<!-- 自定义条件查询用户列表 -->
<select id="findUserByUsername" parameterType="java.lang.String"
resultType="cn.itcast.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
#{}表示一个占位符号,经过#{}能够实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}能够有效防止sql注入。 #{}能够接收简单类型值或pojo属性值。 若是parameterType传输单个简单类型值,#{}括号中能够是value或其它名称。
${}表示拼接sql串,经过${}能够将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换, ${}能够接收简单类型值或pojo属性值,若是parameterType传输单个简单类型值,${}括号中只能是value。
1) po类字段与数据表中的列名不一致
2) 一对一关系
3) 一对多关系
4) 多对多
5) 根据条件封装不一样结果
<resultMap id="vehicleResult" type="Vehicle">
<id property="id" column="id" />
<result property="vin" column="vin"/>
<result property="year" column="year"/>
<result property="make" column="make"/>
<result property="model" column="model"/>
<result property="color" column="color"/>
<discriminator javaType="int" column="vehicle_type">
<case value="1" resultType="carResult">
<result property="doorCount" column="door_count" />
</case>
<case value="2" resultType="truckResult">
<result property="boxSize" column="box_size" />
<result property="extendedCab" column="extended_cab" />
</case>
<case value="3" resultType="vanResult">
<result property="powerSlidingDoor" column="power_sliding_door" />
</case>
<case value="4" resultType="suvResult">
<result property="allWheelDrive" column="all_wheel_drive" />
</case>
</discriminator>
</resultMap>
作条件判断的,若是咱们不使用这个标签,咱们确定会在代码中判断如查询的元素是否为空,传入的元素是否为空,而这时咱们直接使用这个标签,就减小了代码的书写
<!-- 传递pojo综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
concat(‘%’,#{username},’%’)
</if>
</select>
对于这类标签,就是采用多个选项中找一个,就像单项选择题,可是你不会都选择,只会从中选择1个来做为条件。就有点相似于switch。。case。
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
若是使用第一个if语句的sql有and的话,就会发现没有写where标签就会报错。而这类标签一般是搭配条件标签使用的。
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>