Mybatis-代理开发

Mapper 接口开发需要遵循以下规范:
1、 Mapper.xml文件中的namespace与mapper接口的全限定名相同
2、 Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、 Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4、 Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

 

MyBatis映射文件配置

<select>:查询
<insert>:插入
<update>:修改
<delete>:删除
<where>:where条件
<if>:if判断
<foreach>:循环
<sql>:sql片段抽取

 

foreach标签的属性含义如下:
<foreach>标签用于遍历集合,它的属性:
• collection:代表要遍历的集合元素,注意编写时不要写#{}
• open:代表语句的开始部分
• close:代表结束部分
• item:代表遍历集合的每个元素,生成的变量名
• sperator:代表分隔符

 

Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的

1
2
3
4
5
<sql id="selectUser">select * from user</sql>
<!--查询操作-->
<select id="findAll" resultType="user">
    <include refid="selectUser"></include>
</select>

  

where

1
2
3
4
5
6
7
8
<where>
    <if test="id!=0">
        and id=#{id}
    </if>
    <if test="username!=null">
        and username=#{username}
    </if>
</where>               

  

posted @   FinnYY  阅读(61)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示