这个是我的一个设计,一般通用方法定义好了!当然特殊的需要在子接口中单独定义扩展了!
第一份代码:接口
第二份代码:xml实现
其中第二份代码就是我上一篇文章中的工具自动生成的!
package com.bling.saysays.mapper;
import java.util.List;
import com.bling.saysays.web.PageParam;
/**
* 扫描接口及通用接口定义
* @author BLingSoft
*
* @param <T> 与数据库对应实体,子接口实现
*/
public interface SqlMapper<T> {
/**
* 增加
* @param t
*/
public void add(T t);
/**
* 更新有值得属性
* @param t
*/
public void update(T t);
/**
* Id删除
* @param id
*/
public void delete(Integer id);
/**
* Id获取对象
* @param id
* @return
*/
public T get(Integer id);
/**
* 相等查找
* @param t
* @return
*/
public T findEqual(T t);
/**
* Like查找
* @param t
* @return
*/
public T findLike(T t);
/**
* Like总条数
* @param t
* @return
*/
public int getCountLike(T t);
/**
* 相等总条数
* @param t
* @return
*/
public int getCountEqual(T t);
/**
* 相等总记录集
* @param t
* @return
*/
public List<T> listEqual(T t);
/**
* Like总记录集
* @param t
* @return
*/
public List<T> listLike(T t);
/**
* 相等分页记录集
* @param pageParam
* @return
*/
public List<T> pageListEqual(PageParam<T> pageParam);
/**
* Like记录集
* @param pageParam
* @return
*/
public List<T> pageListLike(PageParam<T> pageParam);
}
<!--实体映射-->
<resultMap id="commonLeaveResultMap" type="CommonLeave">
<!--主键编号-->
<id property="id" column="ID" />
<!--编号-->
<result property="leaveId" column="LEAVE_ID" />
<!--用户编号-->
<result property="userId" column="USER_ID" />
<!--用户名-->
<result property="name" column="NAME" />
<!--标题-->
<result property="title" column="TITLE" />
<!--内容-->
<result property="content" column="CONTENT" />
<!--联系方式-->
<result property="contact" column="CONTACT" />
<!--留言IP地址-->
<result property="leaveIp" column="LEAVE_IP" />
<!--站长回复2/3/4状态发给用户-->
<result property="replay" column="REPLAY" />
<!--创建时间-->
<result property="createTime" column="CREATE_TIME" />
<!--排序-->
<result property="sort" column="SORT" />
<!--状态0未读1已读2已采纳3已联系4恶意留言-->
<result property="status" column="STATUS" />
</resultMap>
<!--分页类型映射-->
<parameterMap type="PageParam" id="pageParamMap">
<parameter property="t" resultMap="commonLeaveResultMap"/>
<parameter property="start" javaType="int"/>
<parameter property="num" javaType="int"/>
</parameterMap>
<!--添加-->
<insert id="add" parameterType="CommonLeave">
insert into saysays_common_leave
(LEAVE_ID,USER_ID,NAME,TITLE,CONTENT,CONTACT,LEAVE_IP,REPLAY,CREATE_TIME,SORT,STATUS)
values
(#{leaveId},#{userId},#{name},#{title},#{content},#{contact},#{leaveIp},#{replay},#{createTime},#{sort},#{status})
</insert>
<!--更新:只更新有值字段-->
<update id="update" parameterType="CommonLeave">
update saysays_common_leave set ID=${id}
<if test="leaveId!=null">
,LEAVE_ID=#{leaveId}
</if>
<if test="userId!=null">
,USER_ID=#{userId}
</if>
<if test="name!=null">
,NAME=#{name}
</if>
<if test="title!=null">
,TITLE=#{title}
</if>
<if test="content!=null">
,CONTENT=#{content}
</if>
<if test="contact!=null">
,CONTACT=#{contact}
</if>
<if test="leaveIp!=null">
,LEAVE_IP=#{leaveIp}
</if>
<if test="replay!=null">
,REPLAY=#{replay}
</if>
<if test="createTime!=null">
,CREATE_TIME=#{createTime}
</if>
<if test="sort!=null">
,SORT=#{sort}
</if>
<if test="status!=null">
,STATUS=#{status}
</if>
where ID=#{id}
</update>
<!--删除:根据主键编号删除-->
<delete id="delete" parameterType="int">
delete from saysays_common_leave
where ID=#{id}
</delete>
<!--查找:根据主键编号查找-->
<select id="get" parameterType="int" resultMap="commonLeaveResultMap">
select * from saysays_common_leave
where ID=#{id}
</select>
<!--==查找,匹配有值字段-->
<select id="findEqual" parameterType="CommonLeave" resultMap="commonLeaveResultMap">
select * from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID=#{id}
</if>
<if test="leaveId!=null">
and LEAVE_ID=#{leaveId}
</if>
<if test="userId!=null">
and USER_ID=#{userId}
</if>
<if test="name!=null">
and NAME=#{name}
</if>
<if test="title!=null">
and TITLE=#{title}
</if>
<if test="content!=null">
and CONTENT=#{content}
</if>
<if test="contact!=null">
and CONTACT=#{contact}
</if>
<if test="leaveIp!=null">
and LEAVE_IP=#{leaveIp}
</if>
<if test="replay!=null">
and REPLAY=#{replay}
</if>
<if test="createTime!=null">
and CREATE_TIME=#{createTime}
</if>
<if test="sort!=null">
and SORT=#{sort}
</if>
<if test="status!=null">
and STATUS=#{status}
</if>
<if test="orderSql!=null">
#{orderSql}
</if>
</select>
<!--like查找,匹配有值字段-->
<select id="findLike" parameterType="CommonLeave" resultMap="commonLeaveResultMap">
select * from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID like %#{id}%
</if>
<if test="leaveId!=null">
and LEAVE_ID like %#{leaveId}%
</if>
<if test="userId!=null">
and USER_ID like %#{userId}%
</if>
<if test="name!=null">
and NAME like %#{name}%
</if>
<if test="title!=null">
and TITLE like %#{title}%
</if>
<if test="content!=null">
and CONTENT like %#{content}%
</if>
<if test="contact!=null">
and CONTACT like %#{contact}%
</if>
<if test="leaveIp!=null">
and LEAVE_IP like %#{leaveIp}%
</if>
<if test="replay!=null">
and REPLAY like %#{replay}%
</if>
<if test="createTime!=null">
and CREATE_TIME like %#{createTime}%
</if>
<if test="sort!=null">
and SORT like %#{sort}%
</if>
<if test="status!=null">
and STATUS like %#{status}%
</if>
<if test="orderSql!=null">
#{orderSql}
</if>
</select>
<!--查询条数,匹配有值字段-->
<select id="getCountLike" parameterType="CommonLeave" resultType="int">
select count(*) from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID like %#{id}%
</if>
<if test="leaveId!=null">
and LEAVE_ID like %#{leaveId}%
</if>
<if test="userId!=null">
and USER_ID like %#{userId}%
</if>
<if test="name!=null">
and NAME like %#{name}%
</if>
<if test="title!=null">
and TITLE like %#{title}%
</if>
<if test="content!=null">
and CONTENT like %#{content}%
</if>
<if test="contact!=null">
and CONTACT like %#{contact}%
</if>
<if test="leaveIp!=null">
and LEAVE_IP like %#{leaveIp}%
</if>
<if test="replay!=null">
and REPLAY like %#{replay}%
</if>
<if test="createTime!=null">
and CREATE_TIME like %#{createTime}%
</if>
<if test="sort!=null">
and SORT like %#{sort}%
</if>
<if test="status!=null">
and STATUS like %#{status}%
</if>
</select>
<!--查询条数,匹配有值字段-->
<select id="getCountEqual" parameterType="CommonLeave" resultType="int">
select count(*) from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID=#{id}
</if>
<if test="leaveId!=null">
and LEAVE_ID=#{leaveId}
</if>
<if test="userId!=null">
and USER_ID=#{userId}
</if>
<if test="name!=null">
and NAME=#{name}
</if>
<if test="title!=null">
and TITLE=#{title}
</if>
<if test="content!=null">
and CONTENT=#{content}
</if>
<if test="contact!=null">
and CONTACT=#{contact}
</if>
<if test="leaveIp!=null">
and LEAVE_IP=#{leaveIp}
</if>
<if test="replay!=null">
and REPLAY=#{replay}
</if>
<if test="createTime!=null">
and CREATE_TIME=#{createTime}
</if>
<if test="sort!=null">
and SORT=#{sort}
</if>
<if test="status!=null">
and STATUS=#{status}
</if>
</select>
<!--==查找,匹配有值字段-->
<select id="listEqual" parameterType="CommonLeave" resultMap="commonLeaveResultMap">
select * from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID=#{id}
</if>
<if test="leaveId!=null">
and LEAVE_ID=#{leaveId}
</if>
<if test="userId!=null">
and USER_ID=#{userId}
</if>
<if test="name!=null">
and NAME=#{name}
</if>
<if test="title!=null">
and TITLE=#{title}
</if>
<if test="content!=null">
and CONTENT=#{content}
</if>
<if test="contact!=null">
and CONTACT=#{contact}
</if>
<if test="leaveIp!=null">
and LEAVE_IP=#{leaveIp}
</if>
<if test="replay!=null">
and REPLAY=#{replay}
</if>
<if test="createTime!=null">
and CREATE_TIME=#{createTime}
</if>
<if test="sort!=null">
and SORT=#{sort}
</if>
<if test="status!=null">
and STATUS=#{status}
</if>
<if test="orderSql!=null">
#{orderSql}
</if>
</select>
<!--like查找,匹配有值字段-->
<select id="listLike" parameterType="CommonLeave" resultMap="commonLeaveResultMap">
select * from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID like %#{id}%
</if>
<if test="leaveId!=null">
and LEAVE_ID like %#{leaveId}%
</if>
<if test="userId!=null">
and USER_ID like %#{userId}%
</if>
<if test="name!=null">
and NAME like %#{name}%
</if>
<if test="title!=null">
and TITLE like %#{title}%
</if>
<if test="content!=null">
and CONTENT like %#{content}%
</if>
<if test="contact!=null">
and CONTACT like %#{contact}%
</if>
<if test="leaveIp!=null">
and LEAVE_IP like %#{leaveIp}%
</if>
<if test="replay!=null">
and REPLAY like %#{replay}%
</if>
<if test="createTime!=null">
and CREATE_TIME like %#{createTime}%
</if>
<if test="sort!=null">
and SORT like %#{sort}%
</if>
<if test="status!=null">
and STATUS like %#{status}%
</if>
<if test="orderSql!=null">
#{orderSql}
</if>
</select>
<!--==分页查找,匹配有值字段-->
<select id="listEqual" parameterType="CommonLeave" resultMap="commonLeaveResultMap">
select * from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID=#{id}
</if>
<if test="leaveId!=null">
and LEAVE_ID=#{leaveId}
</if>
<if test="userId!=null">
and USER_ID=#{userId}
</if>
<if test="name!=null">
and NAME=#{name}
</if>
<if test="title!=null">
and TITLE=#{title}
</if>
<if test="content!=null">
and CONTENT=#{content}
</if>
<if test="contact!=null">
and CONTACT=#{contact}
</if>
<if test="leaveIp!=null">
and LEAVE_IP=#{leaveIp}
</if>
<if test="replay!=null">
and REPLAY=#{replay}
</if>
<if test="createTime!=null">
and CREATE_TIME=#{createTime}
</if>
<if test="sort!=null">
and SORT=#{sort}
</if>
<if test="status!=null">
and STATUS=#{status}
</if>
<if test="orderSql!=null">
#{orderSql}
</if>
limit #{start},#{num}
</select>
<!--like分页查找,匹配有值字段-->
<select id="listLike" parameterType="CommonLeave" resultMap="commonLeaveResultMap">
select * from saysays_common_leavewhere 1=1
<if test="id!=null">
and ID like %#{id}%
</if>
<if test="leaveId!=null">
and LEAVE_ID like %#{leaveId}%
</if>
<if test="userId!=null">
and USER_ID like %#{userId}%
</if>
<if test="name!=null">
and NAME like %#{name}%
</if>
<if test="title!=null">
and TITLE like %#{title}%
</if>
<if test="content!=null">
and CONTENT like %#{content}%
</if>
<if test="contact!=null">
and CONTACT like %#{contact}%
</if>
<if test="leaveIp!=null">
and LEAVE_IP like %#{leaveIp}%
</if>
<if test="replay!=null">
and REPLAY like %#{replay}%
</if>
<if test="createTime!=null">
and CREATE_TIME like %#{createTime}%
</if>
<if test="sort!=null">
and SORT like %#{sort}%
</if>
<if test="status!=null">
and STATUS like %#{status}%
</if>
<if test="orderSql!=null">
#{orderSql}
</if>
limit #{start},#{num}
</select>