提示:有不清楚的可以试着看一下我最后的连接,是跟这些内容相关的
Mapper文件,特殊符号:
转义符号 | 原符号 | 中文意思 |
---|---|---|
&It; | < | 小于号 |
> | > | 大于号 |
& | & | 和 |
' | ' | 单引号 |
" | " | 双引号 |
在Mapper xml文件写一些符号,有时候识别不了,就会出错,就需要用到上面的转义字符了。
或者为了方便用<![CDATA[]]>也是可以的,在下面的例子代码也有用到。
例如:
1 col_name <![CDATA[ >= ]]> #{colVal} 【例1】 2 3 <![CDATA[ and w.WORK_DAY <= #{workdayEnd} ]]> 【例2】
Mapperxml文件的参数说明:
resultMap:resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如好几个表结合起来的数据)映射到【某个类型】的集当中。
例如:我已经写好了需要的pojo类【workRecordBo】,里面有我需要用的字段。
写类字段需要注意:
mybatis默认是属性名和数据库字段名一一对应的,即
数据库表列:user_name
实体类属性:user_name
但是java中一般使用驼峰命名
数据库表列:user_name
实体类属性:userName
我在xml中需要这样写:property="类里面的字段名" ,column="数据库里面对应的字段"
1 <resultMap id="workRecordBo" type="com.ribao.entity.bo.WorkRecordBo" > 2 <result property="workDay" column="WORK_DAY"/> 3 </resultMap>
id:与继承BaseMapper的接口中的方法名一样
例如:我的接口是这样的 public interface WorkRecordMapper extends BaseMapper<WorkRecord>{}
我的方法名: public void getWorkRecord(@Param("workdayStart")String workdayStart,@Param("workdayEnd")String workdayEnd,@Param("teamid")String teamid);
@Param()是与xml文件中的#{}绑定在一起的,名字一定要相同,数据类型也要相同。
例如:@Param("workdayStart")对应xml文件的 #{workdayStart}
resultType:resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等,这里目前只说【对象类型】。
对象类型:写全类名
例如:java文件中
package com.ribao.entity.bo; public class WorkRecordBo {......}
我的类名就是:resultType="com.ribao.entity.bo.WorkRecordBo"
Mapper的xml文件写select语句+where条件筛选:
1 <select id="selectWorkRecord" resultType="com.ribao.entity.bo.WorkRecordBo" resultMap="workRecordBo"> 2 SELECT e.EMPLOYEE_ID,t.TEAM_NAME,p.PROJECT_NAME,b.BIG_NAME,s.SMALL_NAME,l.LAN_NAME,WORK_DA,w.REMARK 3 from WORKRECORD w 4 join PROJECT p on p.PROJECT_ID=w.PROJECT_ID 5 join BIGCATEGORY b on b.BIG_ID=w.BIG_ID 6 join SMALLCATEGORY s on s.SMALL_ID=w.SMALL_ID 7 join EMPLOYEE e on e.EMPLOYEE_ID=w.EMPLOYEE_ID 8 join TEAM t on t.TEAM_ID=w.TEAM_ID 9 join LANGUAGE l on l.LAN_NO=w.LAN_NO 10 11 <where> 12 <if test="workdayStart != null and workdayStart !='' " >//workdayStart为非空的时候执行 13 w.WORK_DAY >= #{workdayStart} 14 </if> 15 16 <if test="workdayEnd != null and workdayEnd !='' " > 17 <![CDATA[ and w.WORK_DAY <= #{workdayEnd} ]]> 18 </if> 19 20 <if test="teamidList != null and teamidList !='' " > 21 and w.TEAM_ID in 22 <foreach collection="teamidList" index="index" item="teamid" open="(" separator="," close=")"> 23 #{teamid} 24 </foreach> 25 </if> 26 27 </where>
可参考的博客:
【Java】数据库mapper与实体字段对应,SpringBoot开启驼峰映射 网址:https://blog.csdn.net/hr952909686/article/details/91344655
Mybatis:resultMap的使用总结网址:https://www.cnblogs.com/kenhome/p/7764398.html
学习 mybatis的官方网址:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html