一切请查看 mybatis官网
mybatis回忆总结:
1、select查询。resultMap指定多个id绑定即可。
里面值得注意的点:
1、collection操作
2、assoction操作
3、include sql操作 定义: sql语句 使用
举例说明:
<resultMap type="com.entity.BusiFeeData" id="busiFeeData">
<result property="busiId" column="busiId"/>
<!--详细请参考mybatis官网-->
<collection property="feeItemTaxList" ofType="com.pojo.FeeItemTaxData">
<result property="feeItemCode" column="feeItemCode"/>
<!-- 为什么两个? 这两个用于指定两个类与类之间的关系 以下两种方式 -->
<association property="feeItemType" resultMap="feeItemType"/>
<association property="feeItemData" javaType="con.pojo.FeeItemData">
<result property="feeItemId" column="feeItemId"/>
<result property="feeItemName" column="feeItemName"/>
</association>
<!--collection可多重 property指定映射名 ofType指定类型对象 -->
<collection property="taxListData" ofType="com.pojo.TaxListData">
<result property="taxId" column="taxId"/>
<result property="taxName" column="taxName"/>
</collection>
</collection>
<!--普通字段-->
</resultMap>
<resultMap id="feeItemType" type="com.entity.FeeItem">
<result property="key" column="feeItem_key"/>
<result property="value" column="feeItem_value"/>
</resultMap>
条件指定(普通限定条件除外):
1、if条件指定:
<if test="Po.id != null and Po.id != ''"></if>
2、foreach条件指定:
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item.feeItemCode}
</foreach>
3、choose when otherwise搭配条件
<choose>
<when test="of.name == null or of.name == ''"> 0 </when>
<otherwise>
SUM(CASE WHEN upper(of.name) =upper(#{of.name}) THEN 1 ELSE 0)
</otherwise>
</choose>
AS name
4、trim 灵活的去掉多余的关键字,本质就是字符串的拼接,他可以灵活的用来处理where、set
举例子:where a=#{a} and (b=#{b} or c=#{c}) 括号内的可以不要,可以一个都要。
where a=#{a}
<trim prefix="and (" prefixOverrides="or" suffix=")">
<if test="b != -1">
or b=#{b}
</if>
<if test="c != -1">
or c=#{c}
</if>
</trim>
有了上面的概念,insert、update、delete操作都是类似的。
这里我再来补充说明各个的属性:
特别说明以下说的路径都是/换. 不要后缀.java
1、namespace的属性:对象mapper的相对路径。
2、resultMap的属性:id唯一,type为需要映射的实体相对路径。
3、result的属性:property指定实体映射名,可以是父类的属性,jdbcType指定数据库字段类型
javaType指定实体类型 column数据库字段名称
4、sql的属性:只有一个id
5、select的属性:
id绑定mapper中方法名
parameterType参数类型:取决于mapper文件中方法的参数类型。比如User findUserById(String id);为string。再比如java.util.List、String、int、Integer、实体相对路径等。
resultMap:没什么可说的绑定上面的声明。
resultType指定返回映射对象,上面resultMap两个选其一指定,取决于mapper文件的返回。比如resultType=“com.entity.Meme”,也可以为String,int等。
6、insert的属性:
id唯一。
parameterType参数类型:取决于mapper文件中方法的参数类型。比如User findUserById(String id);为string。再比如java.util.List、String、int、Integer、实体相对路径等。
useGeneratedKeys和keyProperty表示如果id自增则自动生成。比如:
7、update的属性
id唯一。
parameterType参数类型:取决于mapper文件中方法的参数类型。比如User findUserById(String id);为string。再比如java.util.List、String、int、Integer、实体相对路径等。
8、delete的属性
id唯一。
parameterType参数类型:取决于mapper文件中方法的参数类型。比如User findUserById(String id);为string。再比如java.util.List、String、int、Integer、实体相对路径等。
9、if的属性:test指定条件
10、foreach的属性
collection:对应mapper参数名或者其他形式的参数名
item:item
index:下标
close:结束拼接
open:开始拼接
separator:分割符号
常用于select aa in/not in ; insert into … values foreach
11、choose when otherwise
when 指定条件 test指定条件 test不满足则为otherwise。一般用于二选一,比如name的总数如上。
12、trim的属性:prefix 添加前缀 suffix 添加后缀 prefixOverrides 删除前缀 suffixOverrides 删除后缀