新增改

可以通过dao @Param() 来告诉mapper传进的参数名是什么 (适用多参传值,且不是实体类情况)
也可以 直接在mapper.xml里面定义好parameterType是什么,(单参情况)

一对多

Mysql一对多
可以看到在mysql里面是没有一对多效果的,一的一端数据会跟随多的一端重复。
但是我们在后端处理的时候,肯定是想要一对多关系数据的“一”不重复,这样比较好处理,对前端也比较 友好
image.png

  1. <resultMap id="resultTemplateInfo" type="com.sample.bean.TemplateCollect">
  2. <result property="TcollecName" column="Tcollec_name"></result>
  3. <result property="TcollecCount" column="Tcollec_count"></result>
  4. <result property="TcollecVolume" column="Tcollec_volume"></result>
  5. <collection property="procList" ofType="com.sample.bean.TemplateProc">
  6. <id property="TprocId" column="Tproc_id" ></id>
  7. <result property="TprocName" column="Tproc_name" ></result>
  8. <result property="TprocCount" column="Tproc_count" ></result>
  9. <result property="TprocVolume" column="Tproc_volume" ></result>
  10. </collection>
  11. </resultMap>
  12. <select id="selectTemplateCollec" resultMap="resultTemplateInfo" parameterType="String">
  13. SELECT
  14. Tcollec_name,
  15. Tcollec_count,
  16. Tcollec_volume,
  17. Tproc_name,
  18. Tproc_count,
  19. Tproc_volume,
  20. Tproc_unit
  21. FROM
  22. template_collect,
  23. template_proc,
  24. template
  25. WHERE
  26. template_name = "模板1"
  27. AND template_collect.template_id = template.template_id
  28. AND template_proc.Tcollec_id = template_collect.Tcollec_id
  29. </select>

配置一个resultMap,指明这个数组指向哪个类,数组名是什么,然后写清楚collection下分别有哪些参数,参数名和对应的数据库字段名。
当然resultMap对应的这个类肯定是有数组的
ps:多个一对多的情况,还是这套配置,只不过在dao层返回类型写List<实体类>就行,它会自动匹配上。

模糊查询

        SELECT
            device_hole_loc_id,
            hole_loc_state
        FROM
            hole_loc_info
        WHERE
            hole_loc_name like concat (#{holeLocName},'%');
1.//通过concat方法拼接
concat('%',#{holeLocName},'%')
2.//
like binary #{}

插入后返回主键给实体类

mapper里面只需要写parameterType就行,然后取值也是从传入的这个对象上get

useGeneratedKeys="true" keyProperty="operateId"

函数和字段查询

如果mybatis需要返回一个mysql里面的函数和其他的一些字段,函数我们要用as来另命名,方便实体类参数接收,并且count(0)最好用0

SELECT count(0) as counts,study_end FROM file_info GROUP BY study_end

for循环 动态sql

collection:告诉mapper你要用什么样的数组类型(如果定义好@Param则用自定义的命名)
index:个数索引
item:数组对象的参数名

<foreach collection="list" index="index" item="sampleStorage" separator=",">

</foreach>

update动态

UPDATE xx_table
        <set>
            <if test = "vmStatus!=null and vmStatus!=''">
                VM_status = #{vmStatus},
            </if>
            <if test = "daBoxId!=null and daBoxId!=''">
                DA_box_id = #{daBoxId},
            </if>
        </set>

update动态时逗号问题

<trim suffixOverrides=","> 去掉最后一个逗号

    <insert id="addPersonalInfo" useGeneratedKeys="true" keyProperty="objectInfo_id">
        insert into personal_info(
        <trim suffixOverrides=",">
        <if test="name!=null and name!=''">
             name,
        </if>
        <if test="remarks!=null and remarks!=''">
            remarks,
        </if>
        </trim >)
        values (
        <trim suffixOverrides=",">
        <if test="name!=null and name!=''">
             #{name},
        </if>
        <if test="remarks!=null and remarks!=''">
            #{remarks},
        </if>
        </trim>)
    </insert>

排他

        <choose>
            <when test="hospitalId !=null and hospitalId!=''">
                hospital_id
            </when>
            <when test="departmentId !=null and departmentId!=''">
                department_id
            </when>
            <when test="personalId !=null and personalId!=''">
                personal_id
            </when>
        </choose>