有的时候不喜欢用xml配置文件,但是xml配置文件的开发使用简单,自己经常要用到;
因为代码维护的缘故,有的时候看别人的代码,可能存在大量的mappper文件,也不是你想用注解就用注解的;
下面我还是贴一段代码,BaseResultMap返回的是更多的result,而reulstType只返回一种类型;
resulstMap其实对应注解@Results,resultType对应的是@Result参考第二段代码
<resultMap id="BaseResultMap" type="XX.XXMatch">
<id column="i_id" jdbcType="BIGINT" property="iId" />
<result column="XX" jdbcType="VARCHAR" property="XX" />
<result column="XX" jdbcType="VARCHAR" property="XX" />
<result column="XX" jdbcType="VARCHAR" property="XX" />
<result column="XX" jdbcType="VARCHAR" property="XX" />
<result column="XX" jdbcType="TIMESTAMP" property="XX" />
<result column="d_create_time" jdbcType="TIMESTAMP" property="dCreateTime" />
<result column="d_update_time" jdbcType="TIMESTAMP" property="dUpdateTime" />
</resultMap>
<sql id="Base_Column_List">
XX, XX, XX, XX, XX, XX,
d_create_time, d_update_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ipps_face_match
where i_id = #{iId,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from ipps_face_match
where i_id = #{iId,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="XX.XXMatch">
insert into ipps_face_match (XX, XX, XX,
XX, XX, XX,
d_create_time, d_update_time)
values (#{iId,jdbcType=BIGINT}, #{XX,jdbcType=VARCHAR}, #{XX,jdbcType=VARCHAR},
#{XX,jdbcType=VARCHAR}, #{XX,jdbcType=VARCHAR}, #{XX,jdbcType=TIMESTAMP},
#{dCreateTime,jdbcType=TIMESTAMP}, #{dUpdateTime,jdbcType=TIMESTAMP})
</insert>
@Insert("insert into t_alarm (index_code, happened_time, address,save_time)"
+ "values (#{indexCode}, #{happenedTime},#{address},#{saveTime})")
@Options(useGeneratedKeys = true)
int save(DeviceAlarm deviceAlarm);
//因为返回的字段就是*,所以还是写为*。如果查询一部分字段,不能使用*
@Select("select * from t_alarm order by id desc limit 3")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "index_code", property = "indexCode"),
@Result(column = "happened_time", property = "happenedTime"),
@Result(column = "address", property = "address"),
@Result(column = "save_time", property = "saveTime"),
// map-underscore-to-camel-case = true 可以实现一样的效果
// @Result(column = "update_time", property = "updateTime"),
})
ArrayList<DeviceAlarm> findTopAlarms();