2.Mybatis SQL映射文件

条件查询

单条件查询

<select id="getUser" resultType="User" parameterType="string">
select * from User where userName like CONCAT('%',#{userName},'%')
//模糊查询
</select>

多条件查询

<select id="getUser" resultType="User" parameterType="string">
    select * from User where userName like CONCAT('%',#{userName},'%')
    and userRole=#{userRole}
    </select>

resultType跟resultMap的区别

resultType 直接表示返回类型,包括基础数据类型和复杂数据类型
resultMap 则对外部resultMap定义的引用,对应外部resultMap的id,表示返回结果映射代一个resultMap上。应用场景:数据库字段信息与对象属性不一致或者需要做复杂的联合查询已遍自由控制映射结果

<select id="selectUsers" parameterType="Student" resultMap="StudentMap">
		SELECT *FROM `student_info`
		<trim prefix="where" prefixOverrides="and||or">
			<if test="studentName!=null and studentName!='' ">`student_name` like CONCAT('%',#{studentName},'%')</if>
		</trim>
	</select>

对应下面的resultMap

<resultMap type="Student" id="StudentMap">
		<id property="id" column="id" />
		<result property="classId" column="class_id" />
		<result property="studentName" column="student_name" />
		<result property="birthday" column="birthday" />
	</resultMap>

注意:resultMap跟resultType
本质上是一样的,都是Map数据结构。但注意的是resultMap跟resultType不能同时存在,只能二者选一使用

resultMap的自动映射级别

默认的映射级别为PARTIAL 。若满足需要,则需要设置Mybatis对于resultMap的自动映射级别(autoMappingBehavior)为none,即禁止自动匹配。

使用MyBati实现增删改操作

insert 增加

<insert id="insert" parameterType="Student">
		insert into student_info
		values(#{id},#{classId},#{studentName},#{birthday})
	</insert>

注意:
insert,update,delete默认返回的影响行数,int类型
insert,update,delete中均没有resultType属性,只有查询操作需要返回对返回结果类型进行相应的指定

update 修改

<update id="update" parameterType="Student">
		update student_info
		class_id=#{classId},
		student_name=#{studentName},
			birthday=#{birthday},
		where id=#{id}
	</update>

delete 删除

<delete id="delete">
		delete from student_info where class_id=#{id}
	</delete>
	//#{id} 是使用注解的方式入参

resultMap实现高级结果映射

association(多对一,对象)

<resultMap type="Student" id="StudentInfo">
		<id property="id" column="id" />
		<result property="studentName" column="student_name" />
		<association property="grades" javaType="Grades">
			<result property="className" column="class_name" />
			<id property="id" column="id" />
		</association>
	</resultMap>

javaType 完整类名或者别名,会自动检查其类型
id :主键(建议使用)
查询语句

	<select id="selectGrades" resultMap="StudentInfo">
		SELECT s.*,c.*
		FROM
		`student_info` s,`class_info` c
		WHERE s.class_id=c.id AND s.id=#{id}
	</select>

collection(集合,一对多)

	<resultMap type="Grades" id="gradesInfo">
		<result property="id" column="id" />
		<result property="className" column="class_Name" />
		<collection property="students" ofType="Student">
			<result property="id" column="id" />
			<result property="studentName" column="student_name" />
		</collection>
	</resultMap>

ofType:完整类名或者别名
查询语句

	<select id="selectId" resultMap="gradesInfo">
		SELECT s.*,c.*
		FROM `student_info` s,`class_info` c
		WHERE s.class_id=c.id AND
		c.id=#{id}
	</select>

resultMap的自动映射级别

当没有设置autoMappingBehavior的时候,也就是默认情况下,若是普通类型数据类型的属性,会自动匹配所有,但是内部嵌套(association或者collection),那么输出结果就是null,也就是不自动匹配,除非设置value为Full(自动匹配所有)
三个级别:
none 禁止自动匹配
PARTIAL 自动匹配所有属性,有内部嵌套的除外
FULL 自动匹配所有

猜你喜欢

转载自blog.csdn.net/qq_43051879/article/details/84423366