Mybatisplus XML配置文件详解 日期:2025-03-05 人气:44 #### 定义SQL语句标签 ##### select标签 标签属性 - id 唯一的名称,对应dao中mapper的接口名称。 - paramterType 定义传入的参数类型。 - resultType 返回数据类型对应实体类。 - resultMap 外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,对其有一个很好的理解的话,许多复杂映射的情形都能迎刃而解。使用 resultMap 或 resultType,但不能同时使用。 - flushCache 将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false。 - useCache 将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true。 - timeout 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。 - fetchSize 这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset(依赖驱动)。 - statementType STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 - resultSetType FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为 unset (依赖驱动)。 - databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。 - resultOrdered 这个设置仅针对嵌套结果 select 语句适用:如果为true,就是假设包含了嵌套结果集或是分组了,这样的话当返回一个主结果行的时候,就不会发生有对前面结果集的引用的情况。这就使得在获取嵌套的结果集的时候不至导致内存不够用。默认值:false。 - resultSets 这个设置仅对多结果集的情况适用,它将列出语句执行后返回的结果集并每个结果集给一个名称,名称是逗号分隔的。 ##### update标签、insert标签和delete标签 标签属性 - id 唯一的名称,对应dao中mapper的接口名称。 - parameterType 将要传入语句的参数的完全限定类名或别名。这个属性是可选的,因为 MyBatis 可以通过 TypeHandler 推断出具体传入语句的参数,默认值为 unset。 - flushCache 将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:true(对应插入、更新和删除语句)。 - timeout 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。 - statementType STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 - useGeneratedKeys(仅对 insert 和 update 有用)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server这样的关系数据库管理系统的自动递增字段, oracle使用序列是不支持的,通过selectKey可以返回主键),默认值:false。 - keyProperty (仅对 insert 和 update 有用)唯一标记一个属性,MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey子元素设置它的键值,默认:unset。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 - keyColumn(仅对 insert 和 update 有用)通过生成的键值设置表中的列名,这个设置仅在某些数据库(像PostgreSQL)是必须的,当主键列不是表中的第一列的时候需要设置。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 - databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。 #### 配置关联关系标签 ##### collection标签和association标签 collection与association的属性一样,都是用于resultMap返回关联映射使用,collection(一对多映射)关联的是集合,而association(多对一/一对一映射)是关联单个对象。 标签属性 - property resultMap返回实体类中字段和result标签中的property一样。 - column 数据库的列名或者列标签别名,是关联查询往下一个语句传送值。注意: 在处理组合键时,您可以使用column=“{prop1=col1,prop2=col2}”这样的语法,设置多个列名传入到嵌套查询语句。这就会把prop1和prop2设置到目标嵌套选择语句的参数对象中。 - javaType 一般为ArrayList或是java.util.List。 - ofType java的实体类,对应数据库表的列名称,即关联查询select对应返回的类。 - select 执行一个其他映射的sql语句返回一个java实体类型。 ``` <!-- 具体可参考下面ResultMap --> <collection property="answerList" javaType="java.util.List" ofType="com.it.bean.QuestionAnswer" column="id" select="setlectQuestionAnswerByQuestionId"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> </association> ``` ##### resultMap标签 结果集映射,当字段名称和属性名称不一致的情况下,显示写出对应关系 标签属性 - id 唯一标识。 - type 返回类型。 - extends 继承别的resultMap,可选。 关联其它标签 - id标签 表示主键列对应的属性,设置主键使用,使用此标签配置映射关系(可能不止一个)。 - result标签 一般属性的配置映射关系,一般不止一个。 - property: 属性名称 - column: 字段名称 - association标签 关联一个对象使用。 - collection标签 关联一个集合使用。 ``` <!-- 返回关联查询的问题 --> <resultMap id="detail_result" type="com.it.bean.Question"> <id column="id" property="id" /> <result column="question" property="question" /> <result column="question_type" property="questionType" /> <collection property="answerList" javaType="java.util.List" ofType="com.it.bean.QuestionAnswer" column="id" select="setlectQuestionAnswerByQuestionId"/> </resultMap> <!-- 查询问题集 --> <select id="selectQuestions" parameterType="map" resultMap="detail_result"> select q.id, q.question, q.question_type from question q <where> <if test="cond.id != null"> q.id = #{cond.id} </if> <if test="cond.idList != null and cond.idList.size() != 0"> q.id in <foreach collection="cond.idList" item="id" open="(" separator="," close=")"> #{id} </foreach> </if> </where> </select> <!-- 查询对应问题的答案集 --> <select id="setlectQuestionAnswerByQuestionId" parameterType="long" resultType="com.it.bean.QuestionAnswer"> select a.id, a.answer from question_answer a where a.question_id = #{id} </select> ``` #### 动态SQL拼接标签 ##### forEach标签 标签属性 - collection 循环的集合。传的是集合为list,数组为array, 如果是map为java.util.HashMap。 - item 循环的key。 - index 循环的下表顺序。 - open 循环的开头。 - close 循环结束。 - separator 循环的分隔符。 ``` <sql id="base_column">id, question_id, answer</sql> <!-- oracle的批量插入 --> <insert id="insertBatchOracle" parameterType="list"> insert into question_answer ( <include refid="base_column" /> ) select question_answer_seq.NEXTVAL, A.* from ( <foreach collection="list" item="item" separator="union all"> select #{item.questionId}, #{item.answer} from dual </foreach> ) A </insert> ``` ##### if标签 基本都是用来判断值是否为空,注意Integer的判断,mybatis会默认把0变成单引号 “ ‘’ ”。 ``` <if test="item != null and item != ''"></if> ``` ##### choose标签、when标签和otherwise标签 有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。if是与(and)的关系,而choose是或(or)的关系。 ``` <select id="getUserList" resultType="com.it.bean.User" parameterType="com.it.bean.User"> SELECT <include refid="resultParam"></include> FROM User u <where> <choose> <when test="username !=null and username != ''"> u.username LIKE CONCAT(CONCAT('%', #{username}),'%') </when > <when test="sex != null"> AND u.sex = #{sex} </when > <when test="birthday != null "> AND u.birthday = #{birthday} </when > <otherwise> </otherwise> </choose> </where> </select> ``` #### 格式化输出标签 ##### where标签 where用来去掉多条件查询时,开头多余的and。 ``` <select id="selectUserList" parameterType="com.it.bean.User" resultType="com.it.bean.User"> *<!-- 引用Sql片段 -->* select <include refid="selectParam"> from user u <where> *<!--where 可以自动去掉条件中的第一个and-->* <if test="id != null"> and u.id = #{id} </if> <if test="name != null and name != ''"> and u.name = #{name} </if> </where> </select> ``` ##### set标签 set是mybatis提供的一个智能标记,当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。 ``` <update id="updateUser" parameterType="com.it.bean.user"> update user <set> <if test="username != null and username != ''"> username = #{username}, </if> <if test="sex != null and sex == 0 or sex == 1"> sex = #{sex}, </if> <if test="birthday != null "> birthday = #{birthday}, </if > <if test="address != null and address != ''"> address = #{address}, </if> <if test="lastModifiedBy != null and lastModifiedBy != ''"> last_modified_by = #{lastModifiedBy}, last_modified_date = SYSDATE, </if> </set> <where> id = #{id} </where> </update> ``` ##### trim标签 trim标签是一个格式化的标签,可以完成set或者是where标记的功能。 标签属性 - prefix、suffix 表示再trim标签包裹部分的前面或后面添加内容(注意:是没有prefixOverrides,suffixOverrides的情况下)。 - prefixOverrides、suffixOverrides 表示覆盖内容,如果只有这两个属性表示删除内容。 ``` <update id="test" parameterType="com.it.bean.User"> update user <!-- 开头加上set,结尾去除最后一个逗号 --> <trim prefix="set" suffixOverrides=","> <if test="username!=null and username != ''"> name= #{username}, </if> <if test="password!=null and password != ''"> password= #{password}, </if> </trim> <where> id = #{id} </where> </update> ``` #### 定义和引用SQL片段标签 ##### sql标签 定义一些常用的sql语句片段 ``` <sql id="selectParam"> id, username, password, sex, birthday, address </sql> ``` ##### include标签 引用其他的sql片段,通常和sql一起使用 ``` <select> select <include refid="selectParam"></include> from user </select> ``` ### 鸣谢 - [51CTO博客](https://blog.51cto.com/u_14987/12919801) 标签: 上一篇:vscode使用Community Server Connector运行JDK1.8项目报错 下一篇:windows使用minio 随便看看 2025-07-04 vscode使用Community Server Connector运行JDK1.8项目报错 2025-07-03 vscode中为maven项目指定java版本 2025-06-27 利用puppeteer将网页保存为pdf 2025-06-27 一天二十四时辰表 2025-06-27 家谱中儿子和父母的关系有哪些? 留言