##文件命名
>XXMapper.java
对应xml文件命名为XXMapper.xml
##文件
文件应与数据库中表一一对应,有特殊要求的可以一个表对应多个文件,但严禁一个业务一个文件
##参数
为方便aop控数据级权限,dao层参数统一使用Map
``` java
public List<GoodsEvaluateReply> selectByGoodsEvaluateId(Map<String,Object> map);
```
##注释
1. java文件中每个接口函数都要有对应的函数注释,用来标明函数的作用
``` java
public interface OrdersMapper extends Mapper<Orders> {
/**
* @Description 方订单列表个各状态订单数量
* @author liuy-8
* @date 2016年3月21日 上午10:00:53
* @param params
* @return
*/
@LimitesParent(limites = {
@Limit(condition="query.type=1", equals = "create_user_id=userId", bossRoles = { Constants.ROLE_BUYER_MANAGER, Constants.ROLE_GROUP }),
})
public List<OrdersCountsVO> getOrdersCountByStatus(Map<String, Object> map);
}
```
2. xml文件中每个<select>、<insert>、<update>都要用```<!-- -->```来标明其作用
``` xml
<!-- 查询试用订单列表 -->
<select id="getTrialOrderNum" parameterType="com.glodon.dbhs.vo.GetOrdersVO" resultType="int">
SELECT
count(*)
FROM
orders
<where>
<choose>
<when test="query.type == 1">
AND buyer_id = #{query.buyerId}
</when>
<otherwise>
AND supplier_id = #{query.supplierId}
</otherwise>
</choose>
<if test="query.searchValue!=null and query.searchValue!=''">
AND (
code like CONCAT("%", #{query.searchValue}, "%")
OR
contact_person like CONCAT("%", #{query.searchValue}, "%")
)
</if>
<if test="query.startTime != null">
<![CDATA[AND create_at >= #{query.startTime}]]>
</if>
<if test="query.endTime != null">
<![CDATA[AND create_at <= #{query.endTime}]]>
</if>
AND type = #{query.orderTrailType}
</where>
</select>
```
##xml内容规范
####基本规范
1.
<select>标签 必须对应select的SQL
<insert>标签 必须对应insert的SQL
<update>标签 必须对应update的SQL
<delete>标签 必须对应delete的SQL
2.
取值严禁使用${key},会造成sql注入
必须使用#{key}来取值
3. 大于号,小于号要用<![CDATA[ AND create_at <= #{query.endTime} ]]>来转义
####<if>标签
1.判断字符串是否为空,要使用
``` xml
<if test="searchValue != null and searchValue != ''">
...
</if>
```
2.判断数组是否为空,要使用
``` xml
<if test="array != null and array.size > 0">
...
</if>
```
3.判断数字要使用
``` xml
<if test="number != 1">
...
</if>
```
####WHERE语句
where语句使用<where>标签
``` xml
<where>
<if test="query.startTime != null">
<![CDATA[ AND create_at >= #{query.startTime} ]]>
</if>
<if test="query.endTime != null">
<![CDATA[ AND create_at <= #{query.endTime} ]]>
</if>
AND type = #{query.orderTrailType}
</where>
```
####IN语句使用<foreach>标签
``` xml
<select id="getStudentListByClassIDs" resultMap="studentResultMap">
SELECT * FROM orders
<where>
id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</where>
</select>
```