您的位置:首页 > 移动开发

mapper.xml内容解析

2015-09-07 23:41 369 查看
<span style="font-family:Arial;">先来看一个mapping完整代码</span>
<?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.cheqiren.caren.mapper.EditorScoreMapper">
<resultMap id="BaseResultMap" type="com.cheqiren.caren.model.EditorScore">
<id column="HISTORY_ID" property="historyId" jdbcType="INTEGER" />
<result column="USER_ID" property="userId" jdbcType="INTEGER" />
<result column="MONTH_SCORE" property="monthScore" jdbcType="INTEGER" />
<result column="COUNT_SCORE" property="countScore" jdbcType="INTEGER" />
<result column="ADD_MANAGER_ID" property="addManagerId"
jdbcType="INTEGER" />
<result column="UPD_MANAGER_ID" property="updManagerId"
jdbcType="INTEGER" />
<result column="ADD_TIME" property="addTime" jdbcType="CHAR" />
<result column="UPD_TIME" property="updTime" jdbcType="CHAR" />
</resultMap>

<sql id="Base_Column_List">
HISTORY_ID, USER_ID, MONTH_SCORE, COUNT_SCORE,
ADD_MANAGER_ID, UPD_MANAGER_ID,
ADD_TIME, UPD_TIME
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="java.lang.Integer">
select
<include refid="Base_Column_List" />
from t_editor_score
where HISTORY_ID = #{historyId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from
t_editor_score
where HISTORY_ID = #{historyId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.cheqiren.caren.model.EditorScore">
insert into t_editor_score
(HISTORY_ID, USER_ID, MONTH_SCORE,
COUNT_SCORE, ADD_MANAGER_ID,
UPD_MANAGER_ID, ADD_TIME, UPD_TIME)
values
(#{historyId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER},
#{monthScore,jdbcType=INTEGER}, #{countScore,jdbcType=INTEGER},
#{addManagerId,jdbcType=INTEGER}, #{updManagerId,jdbcType=INTEGER},
date_format(now(),'%Y%m%d%H%i%S'),
date_format(now(),'%Y%m%d%H%i%S') )
</insert>
<insert id="insertSelective" parameterType="com.cheqiren.caren.model.EditorScore">
insert into t_editor_score
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="historyId != null">
HISTORY_ID,
</if>
<if test="userId != null">
USER_ID,
</if>
<if test="monthScore != null">
MONTH_SCORE,
</if>
<if test="countScore != null">
COUNT_SCORE,
</if>
<if test="addManagerId != null">
ADD_MANAGER_ID,
</if>
<if test="updManagerId != null">
UPD_MANAGER_ID,
</if>
ADD_TIME,
UPD_TIME
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="historyId != null">
#{historyId,jdbcType=INTEGER},
</if>
<if test="userId != null">
#{userId,jdbcType=INTEGER},
</if>
<if test="monthScore != null">
#{monthScore,jdbcType=INTEGER},
</if>
<if test="countScore != null">
#{countScore,jdbcType=INTEGER},
</if>
<if test="addManagerId != null">
#{addManagerId,jdbcType=INTEGER},
</if>
<if test="updManagerId != null">
#{updManagerId,jdbcType=INTEGER},
</if>
date_format(now(),'%Y%m%d%H%i%S'),
date_format(now(),'%Y%m%d%H%i%S')
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.cheqiren.caren.model.EditorScore">
update t_editor_score
<set>
<if test="historyId != null">
HISTORY_ID = #{historyId,jdbcType=INTEGER},
</if>
<if test="userId != null">
USER_ID = #{userId,jdbcType=INTEGER},
</if>
<if test="monthScore != null">
MONTH_SCORE = #{monthScore,jdbcType=INTEGER},
</if>
<if test="countScore != null">
COUNT_SCORE = #{countScore,jdbcType=INTEGER},
</if>
<if test="updManagerId != null">
UPD_MANAGER_ID = #{updManagerId,jdbcType=INTEGER},
</if>
UPD_TIME = date_format(now(),'%Y%m%d%H%i%S')
</set>
where HISTORY_ID = #{historyId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.cheqiren.caren.model.Editor">
update
t_editor_score
set USER_ID = #{userId,jdbcType=INTEGER},
MONTH_SCORE =
#{monthScore,jdbcType=VARCHAR},
COUNT_SCORE =
#{countScore,jdbcType=VARCHAR},
UPD_MANAGER_ID =
#{updManagerId,jdbcType=VARCHAR},
UPD_TIME =
date_format(now(),'%Y%m%d%H%i%S')
where HISTORY_ID =
#{historyId,jdbcType=INTEGER}
</update>
<select id="selectAll" resultType="com.cheqiren.caren.vo.EditorScoreVO"
parameterType="com.cheqiren.caren.common.SQLAdapter">
SELECT
tes.HISTORY_ID AS scoreId,
tes.UPD_TIME AS updTime,
tes.MONTH_SCORE AS monthScore,
tes.ADD_TIME as months
FROM t_editor_score tes
where 1=1
${adapter.whereSQL}
order by ${adapter.orderSQL}
limit ${adapter.fromNum},${adapter.toNum}
</select>
<select id="selectCount" resultType="java.lang.Integer"
parameterType="com.cheqiren.caren.common.SQLAdapter">
select count(1)
from T_EDITOR_SCORE where 1=1
${adapter.whereSQL}
</select>
</mapper>
以上是一个完整的mapping文件内容,mapping里面大部分代码都是代码生成器自动生成的

前面的SQL语句模板,它自己的增删改查语句,带有if判断的增加和删除语句。

《注意我使用的mysql数据库,既然是生成器生成的语句,应该其他数据库也能用》

这里面有几点要注意

1:

<mapper namespace="com.cheqiren.caren.mapper.EditorScoreMapper">
namespace是将你的mapper和mapping链接起来,指明,从哪个mapper能调用这个mapping。

<resultMap id="BaseResultMap" type="com.cheqiren.caren.model.EditorScore">
<id column="HISTORY_ID" property="historyId" jdbcType="INTEGER" />
<result column="USER_ID" property="userId" jdbcType="INTEGER" />
<span style="font-family:Arial;"></resultMap></span>
resultMap结果映射,规定查询结果对应哪个类,接下来里面的
<id column="HISTORY_ID" property="historyId" jdbcType="INTEGER" />
<result column="USER_ID" property="userId" jdbcType="INTEGER" />
就是具体的设计:其中<id ><result>是同样的作用,只是参照表的主键,设置一个<id>,如果表在数据库里面有主键这个就是必须的,如果没有都可以写成<result > 然后是cloumn ="USER_ID" propert="userId" 表明表里面的那个列,即字段。对应类的那个属性,jdbcType表数据类型就不多说了,只是提醒大家是表数据的类型,不是java的数据类型

2:

自己定义的方法和语句都必须有ID,不然会报错,并且mapper层是根据id来调用方法,举例:这个mapping有一个id为“selectAll“的语句块(方法),在mapper层的调用就是

public interface EditorMapper {
EditorScoreVO <span style="font-family:Arial;font-size:14px;line-height:26px">selectAll</span>(SQLAdapter <span style="font-family:Arial;">adpter</span>);
<span style="font-family:Arial;">}</span>
3:

其他的parameterType(查询参数) 如果没有就可以不写,不带条件的查询或是分类之类的操作,其实对于要输入的查询参数,有两种定义方法,一种就是在parameterType里面一起定义

parameterType="com.cheqiren.caren.common.SQLAdapter"
这样的好处是在插入的时候,不用一个一个列去指定参数类型,他根据你定义的类的属性的类型就可以直接对应知道列的类型,当你的查询参数只有一种类型(比如int
4000
的时候)就可以用parameterType="java.lang.Integer"来定义,但是当你的查询参数有两个,比如根据ID来查询是int name来查是String的时候,parameterType=""就不好写了。里面不能写两个。不写又会报错。

这时候就可以用第二种方法来定义了

#{id,jdbcType=int},
#{name,jdbcType=varchar},
直接在查询参数的后面加上类型,这样就不用设置patameterType属性了

4:

最后是resultMap和resultType

resultMap前面解释过了,除了自动生成的你也可以自己定义,用其他Id就行了,结果对应的类也可以直接改。

resultType就是将查询的结果直接对应你的某个类。需要注意的是:查询的结果显示的字段,定义的那个类属性必须有,反过来不要求。字段名和属性名必须一致,大小有要求,且类型也要对应,例如char对应String。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: