一、标签
1 parameterType(输入类型)
在 MyBatis 中,“parameterType” 用于指定传递给 SQL 语句的参数类型。它告诉 MyBatis 应该将哪种类型的对象传递给 SQL 语句,以便在语句中使用。
基本类型参数: 可以直接指定 Java 中的基本数据类型或其包装类作为 “parameterType”。
<select id="selectUserById" parameterType="int" resultType="User">
SELECT * FROM users WHERE id = #{userId}
</select>
对象类型参数: 可以指定一个 Java 对象作为 “parameterType”,MyBatis 将会把该对象传递给 SQL 语句。例如:
public class User {
private int id;
private String username;
// getters and setters
}
<insert id="insertUser" parameterType="User">
INSERT INTO users (id, username) VALUES (#{id}, #{username})
</insert>
Map 对象参数: 可以指定 “parameterType” 为 “map”,传递一个 Map 对象作为参数,其中键值对应于参数名和参数值。在这个例子中,可以传递一个 Map 对象,其中键为 “userId”,值为对应的用户 ID。
<select id="selectUserById" parameterType="map" resultType="User">
SELECT * FROM users WHERE id = #{userId}
</select>
数组、集合或其它集合类型参数: 可以指定一些集合类型作为 “parameterType”,例如 List、Set、Array 等。
<select id="selectUsersByIds" parameterType="java.util.List" resultType="User">
SELECT * FROM users WHERE id IN
<foreach collection="list" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</select>
2 resultType(输出类型)
用于指定 SQL 语句的返回结果映射到的 Java 类型。它通常用于简单的映射场景,其中查询结果的列可以直接映射到 Java 对象的属性。
基本类型映射: 可以直接指定 Java 中的基本数据类型或其包装类,这里的 “resultType” 指定为 “int”,表示查询结果将会映射为一个整数值。
<select id="selectCount" resultType="int">
SELECT COUNT(*) FROM users
</select>
简单对象映射: 可以指定一个 Java 类作为 “resultType”,MyBatis 将根据查询结果中的列名自动映射到该类的属性。这里的 “resultType” 指定为 “User” 类型,表示查询结果中的 “id” 和 “username” 列将会自动映射到 “User” 类的属性。
public class User {
private int id;
private String username;
// getters and setters
}
<select id="selectUser" resultType="User">
SELECT id, username FROM users WHERE id = #{userId}
</select>
Map 对象映射: 可以指定 “resultType” 为 “map”,这样查询结果将会以 Map 的形式返回,其中键为列名,值为对应的列值。
<select id="selectUserMap" resultType="map">
SELECT id, username FROM users WHERE id = #{userId}
</select>
数组、集合或其它集合类型映射: 除了基本类型和对象类型外,还可以指定一些集合类型,例如 List、Set、Array 等。这里的 “resultType” 指定为 “java.util.List”,表示查询结果将会映射为一个字符串列表。
<select id="selectAllUsernames" resultType="java.util.List">
SELECT username FROM users
</select>
注:“resultType” 适用于简单的映射场景,如果查询结果包含复杂的嵌套结构或需要进行更复杂的映射,则应该使用 “resultMap” 进行配置。
3 resultMap
当sql查询字段名和pojo的属性名不一致,通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。
实体类中某个属性值为userId, SQL字段名为user_id
<select id="queryOrderAll" resultMap="orderResultMap">
SELECT id, user_id, number FROM order;
</select>
<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
<resultMap type="order" id="orderResultMap">
<!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
<!-- property:主键在pojo中的属性名, column:主键在数据库中的列 -->
<id property="id" column="id" />
<!-- 定义普通属性 -->
<result property="userId" column="user_id" />
<result property="number" column="number" />
</resultMap>
4. 动态SQL
在xml文件中,以标签的形式编写动态sql,完成一些逻辑判断的功能。
一共有9种:if、 when、 where、 otherwise、 choose、 trim、 bind、 set、 foreach
执行原理:
使用 ognl 从sql参数对象中计算表达式的值,然后动态拼接sql。
4.1 if 标签
SELECT * FROM user WHERE sex = #{sex} AND username LIKE '%${username}%'
如果没有提供set、username 条件,则结果不合理,可改为:
SELECT * FROM user WHERE 1=1
<if test="sex != null and sex != ''">
AND sex = #{sex}
</if>
<if test="username != null and username != ''">
AND username LIKE '%${username}%'
</if>
4.2 foreach标签
向sql传递数组或List,使用foreach解析
查询sql:
SELECT * FROM user WHERE id IN (1,10,24)
也可利用foreach标签
SELECT * FROM user
<where>
<!-- collection:遍历的集合,这里是QueryVo的ids属性 (数组或集合)-->
<!-- item:遍历的项目,可以随便写,但是和后面的#{}里面要一致 -->
<!-- open:在前面添加的sql片段 -->
<!-- close:在结尾处添加的sql片段 -->
<!-- separator:指定遍历的元素之间使用的分隔符 -->
<foreach collection="ids" item="item" open="id IN (" close=")" separator=",">
#{item}
</foreach>
</where>
4.3 include标签
将重复的sql提取出来,使用时用include引用即可
<sql id = "defalut">
select * from user
</sql>
<select id = "findAll" resultType = "user">
<include refid = "defalut"/>
</select>
5. 关联查询
数据模型:
5.1 一对一关系:标签
方法一:使用resultType
新建一个类orderUser,继承order类,并在该类中定义user类中的所有属性
<select id="queryOrderUser" resultType="orderUser">
SELECT o.*, u.* FROM order o LEFT JOIN user u ON o.user_id = u.id
</select>
方法二:使用resultMap
在Order类中加入User属性
<select id="queryOrderUser" resultMap="orderUserResultMap">
SELECT o.*, u.* FROM order o LEFT JOIN user u ON o.user_id = u.id
</select>
<resultMap type="order" id="orderUserResultMap">
<id property="id" column="id" />
<result property="userId" column="user_id" />
<result property="number" column="number" />
<!-- association :配置一对一属性 -->
<!-- property:order里面的User属性名 -->
<!-- javaType:属性类型 -->
<association property="user" javaType="user">
<!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
<id property="id" column="user_id" />
<result property="username" column="username" />
<result property="address" column="address" />
</association>
</resultMap>
5.2 一对多关联: 标签
在User类中加入List orders属性
<resultMap type="user" id="userOrderResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<!-- 配置一对多的关系 -->
<collection property="orders" javaType="list" ofType="order">
<!-- 配置主键,是关联Order的唯一标识 -->
<id property="id" column="oid" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
</collection>
</resultMap>
5.3 多对多关联
可看成双向的一对多关系
在sql语句中需要一个中间表,但标签内与一对多相同
eg. 一个角色表role,一个用户表user,查找所有对象和其对应的用户信息,新建一个表roleUser
<select id="findAll" resultMap="roleMap">
select r.*, u.* from role r join roleUser ru on r.id = ur.rid join user u on ru.uid = u.id;
</select>
二、注解
常见注解
@Results注解
@one、@Many注解
查询注解
查询操作,当列名与属性名不一致时
更新注解
示例如下
一对一延迟加载
一对多延迟加载